From 1db5a2f6975eaf2cee5d2a8e59805ea21d9dc085 Mon Sep 17 00:00:00 2001 From: wenyongh Date: Mon, 22 Jul 2019 11:32:51 +0800 Subject: [PATCH] Re-org platform APIs: move most platform APIs of iwasm to shared-lib (#45) --- core/iwasm/products/alios-things/iwasm.mk | 5 +- core/iwasm/products/alios-things/src/main.c | 2 +- core/iwasm/products/linux/main.c | 6 +- core/iwasm/products/vxworks/main.c | 4 +- .../products/zephyr/simple/CMakeLists.txt | 3 +- core/iwasm/products/zephyr/simple/src/main.c | 2 +- .../wasm_platform.c => include/wasm_dlfcn.h} | 28 ++--- core/iwasm/runtime/include/wasm_hashmap.h | 2 +- core/iwasm/runtime/include/wasm_log.h | 2 +- core/iwasm/runtime/include/wasm_vector.h | 2 +- .../alios/{wasm-native.c => wasm_native.c} | 0 .../runtime/platform/alios/wasm_platform.h | 104 ----------------- .../runtime/platform/include/wasm_types.h | 38 ------- .../runtime/platform/linux/wasm_platform.c | 96 ---------------- .../runtime/platform/linux/wasm_platform.h | 104 ----------------- .../runtime/platform/vxworks/wasm_platform.c | 96 ---------------- .../runtime/platform/vxworks/wasm_platform.h | 104 ----------------- .../runtime/platform/zephyr/wasm_platform.c | 56 ---------- .../runtime/platform/zephyr/wasm_platform.h | 105 ------------------ core/iwasm/runtime/utils/wasm_dlfcn.c | 2 +- core/iwasm/runtime/vmcore-wasm/wasm.h | 2 +- .../runtime/vmcore-wasm/wasm_application.c | 11 +- core/iwasm/runtime/vmcore-wasm/wasm_loader.c | 12 +- core/iwasm/runtime/vmcore-wasm/wasm_runtime.c | 2 +- .../platform/alios/COPYRIGHT | 0 .../platform/alios/bh_math.c} | 66 +++++------ core/shared-lib/platform/alios/bh_platform.c | 6 + core/shared-lib/platform/alios/bh_platform.h | 28 ++++- core/shared-lib/platform/include/bh_types.h | 2 + core/shared-lib/platform/linux/bh_platform.c | 59 +++++++++- core/shared-lib/platform/linux/bh_platform.h | 79 ++++++------- .../shared-lib/platform/vxworks/bh_platform.c | 59 ++++++++++ .../shared-lib/platform/vxworks/bh_platform.h | 70 ++++++------ .../platform/zephyr/COPYRIGHT | 0 .../platform/zephyr/bh_math.c} | 66 +++++------ core/shared-lib/platform/zephyr/bh_platform.c | 22 ++++ core/shared-lib/platform/zephyr/bh_platform.h | 35 +++++- .../src/platform/zephyr/main.c | 2 +- .../zephyr-build/CMakeLists.txt | 4 +- 39 files changed, 387 insertions(+), 899 deletions(-) rename core/iwasm/runtime/{platform/alios/wasm_platform.c => include/wasm_dlfcn.h} (69%) rename core/iwasm/runtime/platform/alios/{wasm-native.c => wasm_native.c} (100%) delete mode 100644 core/iwasm/runtime/platform/alios/wasm_platform.h delete mode 100644 core/iwasm/runtime/platform/include/wasm_types.h delete mode 100644 core/iwasm/runtime/platform/linux/wasm_platform.c delete mode 100644 core/iwasm/runtime/platform/linux/wasm_platform.h delete mode 100644 core/iwasm/runtime/platform/vxworks/wasm_platform.c delete mode 100644 core/iwasm/runtime/platform/vxworks/wasm_platform.h delete mode 100644 core/iwasm/runtime/platform/zephyr/wasm_platform.c delete mode 100644 core/iwasm/runtime/platform/zephyr/wasm_platform.h rename core/{iwasm/runtime => shared-lib}/platform/alios/COPYRIGHT (100%) rename core/{iwasm/runtime/platform/alios/wasm_math.c => shared-lib/platform/alios/bh_math.c} (90%) rename core/{iwasm/runtime => shared-lib}/platform/zephyr/COPYRIGHT (100%) rename core/{iwasm/runtime/platform/zephyr/wasm_math.c => shared-lib/platform/zephyr/bh_math.c} (91%) diff --git a/core/iwasm/products/alios-things/iwasm.mk b/core/iwasm/products/alios-things/iwasm.mk index 030502ef0..803578c15 100644 --- a/core/iwasm/products/alios-things/iwasm.mk +++ b/core/iwasm/products/alios-things/iwasm.mk @@ -28,9 +28,7 @@ GLOBAL_INCLUDES += ${IWASM_ROOT}/runtime/include \ $(NAME)_SOURCES := ${IWASM_ROOT}/runtime/utils/wasm_hashmap.c \ ${IWASM_ROOT}/runtime/utils/wasm_log.c \ ${IWASM_ROOT}/runtime/utils/wasm_dlfcn.c \ - ${IWASM_ROOT}/runtime/platform/alios/wasm_math.c \ - ${IWASM_ROOT}/runtime/platform/alios/wasm_platform.c \ - ${IWASM_ROOT}/runtime/platform/alios/wasm-native.c \ + ${IWASM_ROOT}/runtime/platform/alios/wasm_native.c \ ${IWASM_ROOT}/runtime/vmcore-wasm/wasm_application.c \ ${IWASM_ROOT}/runtime/vmcore-wasm/wasm_interp.c \ ${IWASM_ROOT}/runtime/vmcore-wasm/wasm_loader.c \ @@ -41,6 +39,7 @@ $(NAME)_SOURCES := ${IWASM_ROOT}/runtime/utils/wasm_hashmap.c \ ${SHARED_LIB_ROOT}/platform/alios/bh_platform.c \ ${SHARED_LIB_ROOT}/platform/alios/bh_assert.c \ ${SHARED_LIB_ROOT}/platform/alios/bh_thread.c \ + ${SHARED_LIB_ROOT}/platform/alios/bh_math.c \ ${SHARED_LIB_ROOT}/mem-alloc/bh_memory.c \ ${SHARED_LIB_ROOT}/mem-alloc/mem_alloc.c \ ${SHARED_LIB_ROOT}/mem-alloc/ems/ems_kfc.c \ diff --git a/core/iwasm/products/alios-things/src/main.c b/core/iwasm/products/alios-things/src/main.c index f9b19c478..395c9b852 100644 --- a/core/iwasm/products/alios-things/src/main.c +++ b/core/iwasm/products/alios-things/src/main.c @@ -16,9 +16,9 @@ #include #include +#include "bh_platform.h" #include "wasm_assert.h" #include "wasm_log.h" -#include "wasm_platform.h" #include "wasm_platform_log.h" #include "wasm_thread.h" #include "wasm_export.h" diff --git a/core/iwasm/products/linux/main.c b/core/iwasm/products/linux/main.c index 22fe692c0..9b0dc08cc 100644 --- a/core/iwasm/products/linux/main.c +++ b/core/iwasm/products/linux/main.c @@ -19,9 +19,9 @@ #endif #include #include +#include "bh_platform.h" #include "wasm_assert.h" #include "wasm_log.h" -#include "wasm_platform.h" #include "wasm_platform_log.h" #include "wasm_thread.h" #include "wasm_export.h" @@ -188,8 +188,8 @@ int main(int argc, char *argv[]) wasm_log_set_verbose_level(log_verbose_level); /* load WASM byte buffer from WASM bin file */ - if (!(wasm_file_buf = (uint8*) wasm_read_file_to_buffer(wasm_file, - &wasm_file_size))) + if (!(wasm_file_buf = (uint8*) bh_read_file_to_buffer(wasm_file, + &wasm_file_size))) goto fail2; /* load WASM module */ diff --git a/core/iwasm/products/vxworks/main.c b/core/iwasm/products/vxworks/main.c index 22fe692c0..dc2630428 100644 --- a/core/iwasm/products/vxworks/main.c +++ b/core/iwasm/products/vxworks/main.c @@ -188,8 +188,8 @@ int main(int argc, char *argv[]) wasm_log_set_verbose_level(log_verbose_level); /* load WASM byte buffer from WASM bin file */ - if (!(wasm_file_buf = (uint8*) wasm_read_file_to_buffer(wasm_file, - &wasm_file_size))) + if (!(wasm_file_buf = (uint8*) bh_read_file_to_buffer(wasm_file, + &wasm_file_size))) goto fail2; /* load WASM module */ diff --git a/core/iwasm/products/zephyr/simple/CMakeLists.txt b/core/iwasm/products/zephyr/simple/CMakeLists.txt index d9e91e4cb..0363741ba 100644 --- a/core/iwasm/products/zephyr/simple/CMakeLists.txt +++ b/core/iwasm/products/zephyr/simple/CMakeLists.txt @@ -35,8 +35,6 @@ include_directories (${IWASM_ROOT}/runtime/include set (IWASM_SRCS ${IWASM_ROOT}/runtime/utils/wasm_hashmap.c ${IWASM_ROOT}/runtime/utils/wasm_log.c ${IWASM_ROOT}/runtime/utils/wasm_dlfcn.c - ${IWASM_ROOT}/runtime/platform/zephyr/wasm_math.c - ${IWASM_ROOT}/runtime/platform/zephyr/wasm_platform.c ${IWASM_ROOT}/runtime/platform/zephyr/wasm_native.c ${IWASM_ROOT}/runtime/vmcore-wasm/wasm_application.c ${IWASM_ROOT}/runtime/vmcore-wasm/wasm_interp.c @@ -48,6 +46,7 @@ set (IWASM_SRCS ${IWASM_ROOT}/runtime/utils/wasm_hashmap.c ${SHARED_LIB_ROOT}/platform/zephyr/bh_platform.c ${SHARED_LIB_ROOT}/platform/zephyr/bh_assert.c ${SHARED_LIB_ROOT}/platform/zephyr/bh_thread.c + ${SHARED_LIB_ROOT}/platform/zephyr/bh_math.c ${SHARED_LIB_ROOT}/mem-alloc/bh_memory.c ${SHARED_LIB_ROOT}/mem-alloc/mem_alloc.c ${SHARED_LIB_ROOT}/mem-alloc/ems/ems_kfc.c diff --git a/core/iwasm/products/zephyr/simple/src/main.c b/core/iwasm/products/zephyr/simple/src/main.c index fb183e387..a6a9260a7 100644 --- a/core/iwasm/products/zephyr/simple/src/main.c +++ b/core/iwasm/products/zephyr/simple/src/main.c @@ -16,9 +16,9 @@ #include #include +#include "bh_platform.h" #include "wasm_assert.h" #include "wasm_log.h" -#include "wasm_platform.h" #include "wasm_platform_log.h" #include "wasm_thread.h" #include "wasm_export.h" diff --git a/core/iwasm/runtime/platform/alios/wasm_platform.c b/core/iwasm/runtime/include/wasm_dlfcn.h similarity index 69% rename from core/iwasm/runtime/platform/alios/wasm_platform.c rename to core/iwasm/runtime/include/wasm_dlfcn.h index e643377cd..ce45de2b1 100644 --- a/core/iwasm/runtime/platform/alios/wasm_platform.c +++ b/core/iwasm/runtime/include/wasm_dlfcn.h @@ -14,27 +14,19 @@ * limitations under the License. */ -#include "wasm_platform.h" +#ifndef _WASM_DLFCN_H +#define _WASM_DLFCN_H -bool is_little_endian = false; +#ifdef __cplusplus +extern "C" { +#endif -bool __is_little_endian() -{ - union w - { - int a; - char b; - }c; +void * +wasm_dlsym(void *handle, const char *symbol); - c.a = 1; - return (c.b == 1); +#ifdef __cplusplus } +#endif -int wasm_platform_init() -{ - if (__is_little_endian()) - is_little_endian = true; - - return 0; -} +#endif /* end of _WASM_DLFCN_H */ diff --git a/core/iwasm/runtime/include/wasm_hashmap.h b/core/iwasm/runtime/include/wasm_hashmap.h index c9f719122..2296b1c3c 100644 --- a/core/iwasm/runtime/include/wasm_hashmap.h +++ b/core/iwasm/runtime/include/wasm_hashmap.h @@ -17,7 +17,7 @@ #ifndef WASM_HASHMAP_H #define WASM_HASHMAP_H -#include "wasm_platform.h" +#include "bh_platform.h" #ifdef __cplusplus diff --git a/core/iwasm/runtime/include/wasm_log.h b/core/iwasm/runtime/include/wasm_log.h index 112db2f14..cba03b044 100644 --- a/core/iwasm/runtime/include/wasm_log.h +++ b/core/iwasm/runtime/include/wasm_log.h @@ -30,7 +30,7 @@ #ifndef _WASM_LOG_H #define _WASM_LOG_H -#include "wasm_platform.h" +#include "bh_platform.h" #ifdef __cplusplus diff --git a/core/iwasm/runtime/include/wasm_vector.h b/core/iwasm/runtime/include/wasm_vector.h index 4723ff687..e11832ede 100644 --- a/core/iwasm/runtime/include/wasm_vector.h +++ b/core/iwasm/runtime/include/wasm_vector.h @@ -17,7 +17,7 @@ #ifndef _WASM_VECTOR_H #define _WASM_VECTOR_H -#include "wasm_platform.h" +#include "bh_platform.h" #ifdef __cplusplus diff --git a/core/iwasm/runtime/platform/alios/wasm-native.c b/core/iwasm/runtime/platform/alios/wasm_native.c similarity index 100% rename from core/iwasm/runtime/platform/alios/wasm-native.c rename to core/iwasm/runtime/platform/alios/wasm_native.c diff --git a/core/iwasm/runtime/platform/alios/wasm_platform.h b/core/iwasm/runtime/platform/alios/wasm_platform.h deleted file mode 100644 index 50bba6de0..000000000 --- a/core/iwasm/runtime/platform/alios/wasm_platform.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _WASM_PLATFORM_H -#define _WASM_PLATFORM_H - -#include "wasm_config.h" -#include "wasm_types.h" -#include -#include - -#include -typedef uint64_t uint64; -typedef int64_t int64; -typedef float float32; -typedef double float64; - -#ifndef NULL -# define NULL ((void*) 0) -#endif - -#define WASM_PLATFORM "AliOS" -#define __ALIOS__ 1 - -#include -#include -#include -#include -#include -#include - -/** - * Return the offset of the given field in the given type. - * - * @param Type the type containing the filed - * @param field the field in the type - * - * @return the offset of field in Type - */ -#ifndef offsetof -#define offsetof(Type, field) ((size_t)(&((Type *)0)->field)) -#endif - -typedef aos_task_t korp_thread; -typedef korp_thread *korp_tid; -typedef aos_mutex_t korp_mutex; - -int wasm_platform_init(); - -extern bool is_little_endian; - -#include - -/* The following operations declared in string.h may be defined as - macros on Linux, so don't declare them as functions here. */ -/* memset */ -/* memcpy */ -/* memmove */ - -/* #include */ - -/* Unit test framework is based on C++, where the declaration of - snprintf is different. */ -#ifndef __cplusplus -int snprintf(char *buffer, size_t count, const char *format, ...); -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* math functions */ -double sqrt(double x); -double floor(double x); -double ceil(double x); -double fmin(double x, double y); -double fmax(double x, double y); -double rint(double x); -double fabs(double x); -double trunc(double x); -int signbit(double x); -int isnan(double x); - -void* -wasm_dlsym(void *handle, const char *symbol); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/core/iwasm/runtime/platform/include/wasm_types.h b/core/iwasm/runtime/platform/include/wasm_types.h deleted file mode 100644 index a0bdbc169..000000000 --- a/core/iwasm/runtime/platform/include/wasm_types.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _WASM_TYPES_H -#define _WASM_TYPES_H - -#include "wasm_config.h" - -typedef unsigned char uint8; -typedef char int8; -typedef unsigned short uint16; -typedef short int16; -typedef unsigned int uint32; -typedef int int32; - -#include "wasm_platform.h" - -#ifndef __cplusplus -#define true 1 -#define false 0 -#define inline __inline -#endif - -#endif /* end of _WASM_TYPES_H */ - diff --git a/core/iwasm/runtime/platform/linux/wasm_platform.c b/core/iwasm/runtime/platform/linux/wasm_platform.c deleted file mode 100644 index 53600a25b..000000000 --- a/core/iwasm/runtime/platform/linux/wasm_platform.c +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "wasm_log.h" -#include "wasm_platform.h" -#include "wasm_memory.h" - -#include -#include -#include -#include -#include - -bool is_little_endian = false; - -bool __is_little_endian() -{ - union w - { - int a; - char b; - }c; - - c.a = 1; - return (c.b == 1); -} - -int -wasm_platform_init() -{ - if (__is_little_endian()) - is_little_endian = true; - - return 0; -} - -char* -wasm_read_file_to_buffer(const char *filename, int *ret_size) -{ - char *buffer; - int file; - int file_size, read_size; - struct stat stat_buf; - - if (!filename || !ret_size) { - LOG_ERROR("Read file to buffer failed: invalid filename or ret size.\n"); - return NULL; - } - - if ((file = open(filename, O_RDONLY, 0)) == -1) { - LOG_ERROR("Read file to buffer failed: open file %s failed.\n", - filename); - return NULL; - } - - if (fstat(file, &stat_buf) != 0) { - LOG_ERROR("Read file to buffer failed: fstat file %s failed.\n", - filename); - close(file); - return NULL; - } - - file_size = stat_buf.st_size; - - if (!(buffer = wasm_malloc(file_size))) { - LOG_ERROR("Read file to buffer failed: alloc memory failed.\n"); - close(file); - return NULL; - } - - read_size = read(file, buffer, file_size); - close(file); - - if (read_size < file_size) { - LOG_ERROR("Read file to buffer failed: read file content failed.\n"); - wasm_free(buffer); - return NULL; - } - - *ret_size = file_size; - return buffer; -} - diff --git a/core/iwasm/runtime/platform/linux/wasm_platform.h b/core/iwasm/runtime/platform/linux/wasm_platform.h deleted file mode 100644 index 7a6320957..000000000 --- a/core/iwasm/runtime/platform/linux/wasm_platform.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _WASM_PLATFORM_H -#define _WASM_PLATFORM_H - -#include "wasm_config.h" -#include "wasm_types.h" - -#include -#include -typedef uint64_t uint64; -typedef int64_t int64; -typedef float float32; -typedef double float64; - -#ifndef NULL -# define NULL ((void*) 0) -#endif - -#define WASM_PLATFORM "Linux" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/** - * Return the offset of the given field in the given type. - * - * @param Type the type containing the filed - * @param field the field in the type - * - * @return the offset of field in Type - */ -#ifndef offsetof -#define offsetof(Type, field) ((size_t)(&((Type *)0)->field)) -#endif - -typedef pthread_t korp_tid; -typedef pthread_mutex_t korp_mutex; - -int wasm_platform_init(); - -extern bool is_little_endian; - -#include - -/* The following operations declared in string.h may be defined as - macros on Linux, so don't declare them as functions here. */ -/* memset */ -/* memcpy */ -/* memmove */ - -/* #include */ - -/* Unit test framework is based on C++, where the declaration of - snprintf is different. */ -#ifndef __cplusplus -int snprintf(char *buffer, size_t count, const char *format, ...); -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* #include */ - -#ifndef __cplusplus -double sqrt(double x); -#endif - -#include -extern int fopen_s(FILE ** pFile, const char *filename, const char *mode); - -char* -wasm_read_file_to_buffer(const char *filename, int *ret_size); - -void* -wasm_dlsym(void *handle, const char *symbol); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/core/iwasm/runtime/platform/vxworks/wasm_platform.c b/core/iwasm/runtime/platform/vxworks/wasm_platform.c deleted file mode 100644 index 53600a25b..000000000 --- a/core/iwasm/runtime/platform/vxworks/wasm_platform.c +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "wasm_log.h" -#include "wasm_platform.h" -#include "wasm_memory.h" - -#include -#include -#include -#include -#include - -bool is_little_endian = false; - -bool __is_little_endian() -{ - union w - { - int a; - char b; - }c; - - c.a = 1; - return (c.b == 1); -} - -int -wasm_platform_init() -{ - if (__is_little_endian()) - is_little_endian = true; - - return 0; -} - -char* -wasm_read_file_to_buffer(const char *filename, int *ret_size) -{ - char *buffer; - int file; - int file_size, read_size; - struct stat stat_buf; - - if (!filename || !ret_size) { - LOG_ERROR("Read file to buffer failed: invalid filename or ret size.\n"); - return NULL; - } - - if ((file = open(filename, O_RDONLY, 0)) == -1) { - LOG_ERROR("Read file to buffer failed: open file %s failed.\n", - filename); - return NULL; - } - - if (fstat(file, &stat_buf) != 0) { - LOG_ERROR("Read file to buffer failed: fstat file %s failed.\n", - filename); - close(file); - return NULL; - } - - file_size = stat_buf.st_size; - - if (!(buffer = wasm_malloc(file_size))) { - LOG_ERROR("Read file to buffer failed: alloc memory failed.\n"); - close(file); - return NULL; - } - - read_size = read(file, buffer, file_size); - close(file); - - if (read_size < file_size) { - LOG_ERROR("Read file to buffer failed: read file content failed.\n"); - wasm_free(buffer); - return NULL; - } - - *ret_size = file_size; - return buffer; -} - diff --git a/core/iwasm/runtime/platform/vxworks/wasm_platform.h b/core/iwasm/runtime/platform/vxworks/wasm_platform.h deleted file mode 100644 index 95d0daad4..000000000 --- a/core/iwasm/runtime/platform/vxworks/wasm_platform.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _WASM_PLATFORM_H -#define _WASM_PLATFORM_H - -#include -#include -#include "wasm_config.h" -#include "wasm_types.h" - -typedef uint64_t uint64; -typedef int64_t int64; -typedef float float32; -typedef double float64; - -#ifndef NULL -# define NULL ((void*) 0) -#endif - -#define WASM_PLATFORM "VxWorks" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/** - * Return the offset of the given field in the given type. - * - * @param Type the type containing the filed - * @param field the field in the type - * - * @return the offset of field in Type - */ -#ifndef offsetof -#define offsetof(Type, field) ((size_t)(&((Type *)0)->field)) -#endif - -typedef pthread_t korp_tid; -typedef pthread_mutex_t korp_mutex; - -int wasm_platform_init(); - -extern bool is_little_endian; - -#include - -/* The following operations declared in string.h may be defined as - macros on Linux, so don't declare them as functions here. */ -/* memset */ -/* memcpy */ -/* memmove */ - -/* #include */ - -/* Unit test framework is based on C++, where the declaration of - snprintf is different. */ -#ifndef __cplusplus -int snprintf(char *buffer, size_t count, const char *format, ...); -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* #include */ - -#ifndef __cplusplus -double sqrt(double x); -#endif - -#include - - -char* -wasm_read_file_to_buffer(const char *filename, int *ret_size); - -void* -wasm_dlsym(void *handle, const char *symbol); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/core/iwasm/runtime/platform/zephyr/wasm_platform.c b/core/iwasm/runtime/platform/zephyr/wasm_platform.c deleted file mode 100644 index 08b1650eb..000000000 --- a/core/iwasm/runtime/platform/zephyr/wasm_platform.c +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "wasm_platform.h" - -#ifndef CONFIG_AEE_ENABLE -static int -_stdout_hook_iwasm(int c) -{ - printk("%c", (char)c); - return 1; -} - -extern void __stdout_hook_install(int (*hook)(int)); -#endif - -bool is_little_endian = false; - -bool __is_little_endian() -{ - union w - { - int a; - char b; - }c; - - c.a = 1; - return (c.b == 1); -} - -int wasm_platform_init() -{ - if (__is_little_endian()) - is_little_endian = true; - -#ifndef CONFIG_AEE_ENABLE - /* Enable printf() in Zephyr */ - __stdout_hook_install(_stdout_hook_iwasm); -#endif - - return 0; -} - diff --git a/core/iwasm/runtime/platform/zephyr/wasm_platform.h b/core/iwasm/runtime/platform/zephyr/wasm_platform.h deleted file mode 100644 index e14d213d7..000000000 --- a/core/iwasm/runtime/platform/zephyr/wasm_platform.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _WASM_PLATFORM_H -#define _WASM_PLATFORM_H - -#include "wasm_config.h" -#include "wasm_types.h" -#include -#include -#include -#include - -#include -typedef uint64_t uint64; -typedef int64_t int64; -typedef float float32; -typedef double float64; - -#ifndef NULL -# define NULL ((void*) 0) -#endif - -#define WASM_PLATFORM "Zephyr" - -#include -#include -#include -#include -#include -#include - -/** - * Return the offset of the given field in the given type. - * - * @param Type the type containing the filed - * @param field the field in the type - * - * @return the offset of field in Type - */ -#ifndef offsetof -#define offsetof(Type, field) ((size_t)(&((Type *)0)->field)) -#endif - -typedef struct k_thread korp_thread; -typedef korp_thread *korp_tid; -typedef struct k_mutex korp_mutex; - -int wasm_platform_init(); - -extern bool is_little_endian; - -#include - -/* The following operations declared in string.h may be defined as - macros on Linux, so don't declare them as functions here. */ -/* memset */ -/* memcpy */ -/* memmove */ - -/* #include */ - -/* Unit test framework is based on C++, where the declaration of - snprintf is different. */ -#ifndef __cplusplus -int snprintf(char *buffer, size_t count, const char *format, ...); -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* math functions */ -double sqrt(double x); -double floor(double x); -double ceil(double x); -double fmin(double x, double y); -double fmax(double x, double y); -double rint(double x); -double fabs(double x); -double trunc(double x); -int signbit(double x); -int isnan(double x); - -void* -wasm_dlsym(void *handle, const char *symbol); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/core/iwasm/runtime/utils/wasm_dlfcn.c b/core/iwasm/runtime/utils/wasm_dlfcn.c index de7123125..56439c71f 100644 --- a/core/iwasm/runtime/utils/wasm_dlfcn.c +++ b/core/iwasm/runtime/utils/wasm_dlfcn.c @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "wasm_platform.h" +#include "bh_platform.h" static bool sort_flag = false; diff --git a/core/iwasm/runtime/vmcore-wasm/wasm.h b/core/iwasm/runtime/vmcore-wasm/wasm.h index e493da990..06b655e29 100644 --- a/core/iwasm/runtime/vmcore-wasm/wasm.h +++ b/core/iwasm/runtime/vmcore-wasm/wasm.h @@ -17,7 +17,7 @@ #ifndef _WASM_H_ #define _WASM_H_ -#include "wasm_platform.h" +#include "bh_platform.h" #include "wasm_hashmap.h" #include "wasm_assert.h" diff --git a/core/iwasm/runtime/vmcore-wasm/wasm_application.c b/core/iwasm/runtime/vmcore-wasm/wasm_application.c index df67b3a09..5073ef2e3 100644 --- a/core/iwasm/runtime/vmcore-wasm/wasm_application.c +++ b/core/iwasm/runtime/vmcore-wasm/wasm_application.c @@ -160,6 +160,13 @@ union ieee754_double { } ieee; }; +static union { + int a; + char b; +} __ue = { .a = 1 }; + +#define is_little_endian() (__ue.b == 1) + bool wasm_application_execute_func(WASMModuleInstance *module_inst, char *name, int argc, char *argv[]) @@ -222,7 +229,7 @@ wasm_application_execute_func(WASMModuleInstance *module_inst, union ieee754_float u; sig = strtoul(endptr + 1, &endptr, 0); u.f = f32; - if (is_little_endian) + if (is_little_endian()) u.ieee.ieee_little_endian.mantissa = sig; else u.ieee.ieee_big_endian.mantissa = sig; @@ -245,7 +252,7 @@ wasm_application_execute_func(WASMModuleInstance *module_inst, union ieee754_double ud; sig = strtoull(endptr + 1, &endptr, 0); ud.d = u.val; - if (is_little_endian) { + if (is_little_endian()) { ud.ieee.ieee_little_endian.mantissa0 = sig >> 32; ud.ieee.ieee_little_endian.mantissa1 = sig; } diff --git a/core/iwasm/runtime/vmcore-wasm/wasm_loader.c b/core/iwasm/runtime/vmcore-wasm/wasm_loader.c index debdb256e..7b4733eff 100644 --- a/core/iwasm/runtime/vmcore-wasm/wasm_loader.c +++ b/core/iwasm/runtime/vmcore-wasm/wasm_loader.c @@ -21,6 +21,7 @@ #include "wasm_runtime.h" #include "wasm_log.h" #include "wasm_memory.h" +#include "wasm_dlfcn.h" /* Read a value of given type from the address pointed to by the given pointer and increase the pointer to the position just after the @@ -1347,6 +1348,13 @@ exchange32(uint8* p_data) *(p_data + 2) = value; } +static union { + int a; + char b; +} __ue = { .a = 1 }; + +#define is_little_endian() (__ue.b == 1) + static bool load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf, uint32 error_buf_size) @@ -1358,7 +1366,7 @@ load(const uint8 *buf, uint32 size, WASMModule *module, CHECK_BUF(p, p_end, sizeof(uint32)); magic_number = read_uint32(p); - if (!is_little_endian) + if (!is_little_endian()) exchange32((uint8*)&magic_number); if (magic_number != WASM_MAGIC_NUMBER) { @@ -1368,7 +1376,7 @@ load(const uint8 *buf, uint32 size, WASMModule *module, CHECK_BUF(p, p_end, sizeof(uint32)); version = read_uint32(p); - if (!is_little_endian) + if (!is_little_endian()) exchange32((uint8*)&version); if (version != WASM_CURRENT_VERSION) { diff --git a/core/iwasm/runtime/vmcore-wasm/wasm_runtime.c b/core/iwasm/runtime/vmcore-wasm/wasm_runtime.c index ebfe14b57..8f8de1ca7 100644 --- a/core/iwasm/runtime/vmcore-wasm/wasm_runtime.c +++ b/core/iwasm/runtime/vmcore-wasm/wasm_runtime.c @@ -35,7 +35,7 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) bool wasm_runtime_init() { - if (wasm_platform_init() != 0) + if (bh_platform_init() != 0) return false; if (wasm_log_init() != 0) diff --git a/core/iwasm/runtime/platform/alios/COPYRIGHT b/core/shared-lib/platform/alios/COPYRIGHT similarity index 100% rename from core/iwasm/runtime/platform/alios/COPYRIGHT rename to core/shared-lib/platform/alios/COPYRIGHT diff --git a/core/iwasm/runtime/platform/alios/wasm_math.c b/core/shared-lib/platform/alios/bh_math.c similarity index 90% rename from core/iwasm/runtime/platform/alios/wasm_math.c rename to core/shared-lib/platform/alios/bh_math.c index bad81eac3..27e953111 100644 --- a/core/iwasm/runtime/platform/alios/wasm_math.c +++ b/core/shared-lib/platform/alios/bh_math.c @@ -28,10 +28,7 @@ * $FreeBSD$ */ -#include "wasm_log.h" -#include "wasm_platform.h" -#include "wasm_platform_log.h" -#include "wasm_memory.h" +#include "bh_platform.h" #define __FDLIBM_STDC__ @@ -99,6 +96,13 @@ typedef union { } bits; } IEEEd2bits_B; +static union { + int a; + char b; +} __ue = { .a = 1 }; + +#define is_little_endian() (__ue.b == 1) + #define __HIL(x) *(1+pdouble2pint(&x)) #define __LOL(x) *(pdouble2pint(&x)) #define __HIB(x) *(int*)&x @@ -179,39 +183,39 @@ do { \ } while (0) /* Macro wrappers. */ -#define EXTRACT_WORDS(ix0,ix1,d) do { \ - if (is_little_endian) \ - EXTRACT_WORDS_L(ix0,ix1,d); \ - else \ - EXTRACT_WORDS_B(ix0,ix1,d); \ +#define EXTRACT_WORDS(ix0,ix1,d) do { \ + if (is_little_endian()) \ + EXTRACT_WORDS_L(ix0,ix1,d); \ + else \ + EXTRACT_WORDS_B(ix0,ix1,d); \ } while (0) -#define INSERT_WORDS(d,ix0,ix1) do { \ - if (is_little_endian) \ - INSERT_WORDS_L(d,ix0,ix1); \ - else \ - INSERT_WORDS_B(d,ix0,ix1); \ +#define INSERT_WORDS(d,ix0,ix1) do { \ + if (is_little_endian()) \ + INSERT_WORDS_L(d,ix0,ix1); \ + else \ + INSERT_WORDS_B(d,ix0,ix1); \ } while (0) -#define GET_HIGH_WORD(i,d) \ -do { \ - if (is_little_endian) \ - GET_HIGH_WORD_L(i,d); \ - else \ - GET_HIGH_WORD_B(i,d); \ -} while (0) +#define GET_HIGH_WORD(i,d) \ + do { \ + if (is_little_endian()) \ + GET_HIGH_WORD_L(i,d); \ + else \ + GET_HIGH_WORD_B(i,d); \ + } while (0) -#define SET_HIGH_WORD(d,v) \ -do { \ - if (is_little_endian) \ - SET_HIGH_WORD_L(d,v); \ - else \ - SET_HIGH_WORD_B(d,v); \ -} while (0) +#define SET_HIGH_WORD(d,v) \ + do { \ + if (is_little_endian()) \ + SET_HIGH_WORD_L(d,v); \ + else \ + SET_HIGH_WORD_B(d,v); \ + } while (0) -#define __HI(x) (is_little_endian ? __HIL(x) : __HIB(x)) +#define __HI(x) (is_little_endian() ? __HIL(x) : __HIB(x)) -#define __LO(x) (is_little_endian ? __LOL(x) : __LOB(x)) +#define __LO(x) (is_little_endian() ? __LOL(x) : __LOB(x)) /* * Attempt to get strict C99 semantics for assignment with non-C99 compilers. @@ -509,7 +513,7 @@ static double freebsd_rint(double x) static int freebsd_isnan(double d) { - if (is_little_endian) { + if (is_little_endian()) { IEEEd2bits_L u; u.d = d; return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0)); diff --git a/core/shared-lib/platform/alios/bh_platform.c b/core/shared-lib/platform/alios/bh_platform.c index fe05e8bd1..f8ca5735f 100644 --- a/core/shared-lib/platform/alios/bh_platform.c +++ b/core/shared-lib/platform/alios/bh_platform.c @@ -25,3 +25,9 @@ char *bh_strdup(const char *s) memcpy(s1, s, strlen(s) + 1); return s1; } + +int bh_platform_init() +{ + return 0; +} + diff --git a/core/shared-lib/platform/alios/bh_platform.h b/core/shared-lib/platform/alios/bh_platform.h index 15fd614fb..e23958c95 100644 --- a/core/shared-lib/platform/alios/bh_platform.h +++ b/core/shared-lib/platform/alios/bh_platform.h @@ -43,8 +43,6 @@ /* Invalid thread tid */ #define INVALID_THREAD_ID NULL -#define INVALID_SEM_ID NULL - #define BH_WAIT_FOREVER AOS_WAIT_FOREVER typedef uint64_t uint64; @@ -79,6 +77,18 @@ int snprintf(char *buffer, size_t count, const char *format, ...); #define NULL ((void*)0) #endif +/** + * Return the offset of the given field in the given type. + * + * @param Type the type containing the filed + * @param field the field in the type + * + * @return the offset of field in Type + */ +#ifndef offsetof +#define offsetof(Type, field) ((size_t)(&((Type *)0)->field)) +#endif + extern void bh_assert_internal(int v, const char *file_name, int line_number, const char *expr_string); #define bh_assert(expr) bh_assert_internal((int)(expr), __FILE__, __LINE__, # expr) @@ -86,5 +96,19 @@ extern int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned i extern int b_strcat_s(char * s1, size_t s1max, const char * s2); extern int b_strcpy_s(char * s1, size_t s1max, const char * s2); +/* math functions */ +double sqrt(double x); +double floor(double x); +double ceil(double x); +double fmin(double x, double y); +double fmax(double x, double y); +double rint(double x); +double fabs(double x); +double trunc(double x); +int signbit(double x); +int isnan(double x); + +int bh_platform_init(); + #endif /* end of _BH_PLATFORM_H */ diff --git a/core/shared-lib/platform/include/bh_types.h b/core/shared-lib/platform/include/bh_types.h index ff561b244..da26c7dc5 100644 --- a/core/shared-lib/platform/include/bh_types.h +++ b/core/shared-lib/platform/include/bh_types.h @@ -25,6 +25,8 @@ typedef unsigned short uint16; typedef short int16; typedef unsigned int uint32; typedef int int32; +typedef float float32; +typedef double float64; #define BYTES_OF_UINT8 1 #define BYTES_OF_UINT16 2 diff --git a/core/shared-lib/platform/linux/bh_platform.c b/core/shared-lib/platform/linux/bh_platform.c index fe05e8bd1..3ff600cfc 100755 --- a/core/shared-lib/platform/linux/bh_platform.c +++ b/core/shared-lib/platform/linux/bh_platform.c @@ -15,8 +15,10 @@ */ #include "bh_platform.h" -#include -#include + +#include +#include +#include char *bh_strdup(const char *s) { @@ -25,3 +27,56 @@ char *bh_strdup(const char *s) memcpy(s1, s, strlen(s) + 1); return s1; } + +int bh_platform_init() +{ + return 0; +} + +char* +bh_read_file_to_buffer(const char *filename, int *ret_size) +{ + char *buffer; + int file; + int file_size, read_size; + struct stat stat_buf; + + if (!filename || !ret_size) { + printf("Read file to buffer failed: invalid filename or ret size.\n"); + return NULL; + } + + if ((file = open(filename, O_RDONLY, 0)) == -1) { + printf("Read file to buffer failed: open file %s failed.\n", + filename); + return NULL; + } + + if (fstat(file, &stat_buf) != 0) { + printf("Read file to buffer failed: fstat file %s failed.\n", + filename); + close(file); + return NULL; + } + + file_size = stat_buf.st_size; + + if (!(buffer = bh_malloc(file_size))) { + printf("Read file to buffer failed: alloc memory failed.\n"); + close(file); + return NULL; + } + + read_size = read(file, buffer, file_size); + close(file); + + if (read_size < file_size) { + printf("Read file to buffer failed: read file content failed.\n"); + bh_free(buffer); + return NULL; + } + + *ret_size = file_size; + return buffer; +} + diff --git a/core/shared-lib/platform/linux/bh_platform.h b/core/shared-lib/platform/linux/bh_platform.h index 00c25b492..47172c1f6 100644 --- a/core/shared-lib/platform/linux/bh_platform.h +++ b/core/shared-lib/platform/linux/bh_platform.h @@ -26,11 +26,16 @@ #include #include #include -#include - -#ifndef __cplusplus -int snprintf(char *buffer, size_t count, const char *format, ...); -#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #ifdef __cplusplus extern "C" { @@ -47,46 +52,19 @@ extern void DEBUGME(void); /* NEED qsort */ -#include -#include -#include -#include -#include -#include -#include -#include - #define _STACK_SIZE_ADJUSTMENT (32 * 1024) -/* Stack size of applet manager thread. */ -#define BH_APPLET_MANAGER_THREAD_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT) - -/* Stack size of HMC thread. */ -#define BH_HMC_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT) - -/* Stack size of watchdog thread. */ -#define BH_WATCHDOG_THREAD_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT) - /* Stack size of applet threads's native part. */ #define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT) -/* Stack size of remote invoke listen thread. */ -#define BH_REMOTE_INVOKE_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT) - -/* Stack size of remote post listen thread. */ -#define BH_REMOTE_POST_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT) - -/* Maximal recursion depth of interpreter. */ -#define BH_MAX_INTERP_RECURSION_DEPTH 8 - /* Default thread priority */ #define BH_THREAD_DEFAULT_PRIORITY 0 #define BH_ROUTINE_MODIFIER + #define BHT_TIMEDOUT ETIMEDOUT #define INVALID_THREAD_ID 0xFFffFFff -#define INVALID_SEM_ID SEM_FAILED typedef pthread_t korp_tid; typedef pthread_mutex_t korp_mutex; @@ -99,28 +77,43 @@ typedef void* (*thread_start_routine_t)(void*); #define wa_free bh_free #define wa_strdup bh_strdup +int snprintf(char *buffer, size_t count, const char *format, ...); double fmod(double x, double y); float fmodf(float x, float y); +double sqrt(double x); -/* Definitions for applet debugging */ -#define APPLET_DEBUG_LISTEN_PORT 8000 -#define BH_SOCKET_INVALID_SOCK -1 #define BH_WAIT_FOREVER 0xFFFFFFFF -typedef int bh_socket_t; #ifndef NULL # define NULL ((void*) 0) #endif +/** + * Return the offset of the given field in the given type. + * + * @param Type the type containing the filed + * @param field the field in the type + * + * @return the offset of field in Type + */ +#ifndef offsetof +#define offsetof(Type, field) ((size_t)(&((Type *)0)->field)) +#endif + #define bh_assert assert -extern int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, - unsigned int n); -extern int b_strcat_s(char * s1, size_t s1max, const char * s2); -extern int b_strcpy_s(char * s1, size_t s1max, const char * s2); -extern int fopen_s(FILE ** pFile, const char *filename, const char *mode); +int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, + unsigned int n); +int b_strcat_s(char * s1, size_t s1max, const char * s2); +int b_strcpy_s(char * s1, size_t s1max, const char * s2); -extern char *bh_strdup(const char *s); +int fopen_s(FILE ** pFile, const char *filename, const char *mode); + +char *bh_read_file_to_buffer(const char *filename, int *ret_size); + +char *bh_strdup(const char *s); + +int bh_platform_init(); #ifdef __cplusplus } diff --git a/core/shared-lib/platform/vxworks/bh_platform.c b/core/shared-lib/platform/vxworks/bh_platform.c index fe05e8bd1..981124228 100644 --- a/core/shared-lib/platform/vxworks/bh_platform.c +++ b/core/shared-lib/platform/vxworks/bh_platform.c @@ -17,6 +17,12 @@ #include "bh_platform.h" #include #include +#include +#include +#include +#include +#include + char *bh_strdup(const char *s) { @@ -25,3 +31,56 @@ char *bh_strdup(const char *s) memcpy(s1, s, strlen(s) + 1); return s1; } + +int bh_platform_init() +{ + return 0; +} + +char* +bh_read_file_to_buffer(const char *filename, int *ret_size) +{ + char *buffer; + int file; + int file_size, read_size; + struct stat stat_buf; + + if (!filename || !ret_size) { + printf("Read file to buffer failed: invalid filename or ret size.\n"); + return NULL; + } + + if ((file = open(filename, O_RDONLY, 0)) == -1) { + printf("Read file to buffer failed: open file %s failed.\n", + filename); + return NULL; + } + + if (fstat(file, &stat_buf) != 0) { + printf("Read file to buffer failed: fstat file %s failed.\n", + filename); + close(file); + return NULL; + } + + file_size = stat_buf.st_size; + + if (!(buffer = wasm_malloc(file_size))) { + printf("Read file to buffer failed: alloc memory failed.\n"); + close(file); + return NULL; + } + + read_size = read(file, buffer, file_size); + close(file); + + if (read_size < file_size) { + printf("Read file to buffer failed: read file content failed.\n"); + wasm_free(buffer); + return NULL; + } + + *ret_size = file_size; + return buffer; +} + diff --git a/core/shared-lib/platform/vxworks/bh_platform.h b/core/shared-lib/platform/vxworks/bh_platform.h index 57fea9a85..142583e5b 100644 --- a/core/shared-lib/platform/vxworks/bh_platform.h +++ b/core/shared-lib/platform/vxworks/bh_platform.h @@ -27,10 +27,13 @@ #include #include #include +#include +#include +#include +#include +#include +#include -#ifndef __cplusplus -int snprintf(char *buffer, size_t count, const char *format, ...); -#endif #ifdef __cplusplus extern "C" { @@ -43,48 +46,23 @@ extern void DEBUGME(void); #define DIE do{bh_debug("Die here\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); DEBUGME(void); while(1);}while(0) -#define BH_PLATFORM "Linux" +#define BH_PLATFORM "VxWorks" /* NEED qsort */ -#include -#include -#include -#include -#include -#include - #define _STACK_SIZE_ADJUSTMENT (32 * 1024) -/* Stack size of applet manager thread. */ -#define BH_APPLET_MANAGER_THREAD_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT) - -/* Stack size of HMC thread. */ -#define BH_HMC_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT) - -/* Stack size of watchdog thread. */ -#define BH_WATCHDOG_THREAD_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT) - /* Stack size of applet threads's native part. */ #define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT) -/* Stack size of remote invoke listen thread. */ -#define BH_REMOTE_INVOKE_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT) - -/* Stack size of remote post listen thread. */ -#define BH_REMOTE_POST_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT) - -/* Maximal recursion depth of interpreter. */ -#define BH_MAX_INTERP_RECURSION_DEPTH 8 - /* Default thread priority */ #define BH_THREAD_DEFAULT_PRIORITY 0 #define BH_ROUTINE_MODIFIER + #define BHT_TIMEDOUT ETIMEDOUT #define INVALID_THREAD_ID 0xFFffFFff -#define INVALID_SEM_ID SEM_FAILED typedef pthread_t korp_tid; typedef pthread_mutex_t korp_mutex; @@ -97,27 +75,41 @@ typedef void* (*thread_start_routine_t)(void*); #define wa_free bh_free #define wa_strdup bh_strdup +int snprintf(char *buffer, size_t count, const char *format, ...); double fmod(double x, double y); float fmodf(float x, float y); +double sqrt(double x); -/* Definitions for applet debugging */ -#define APPLET_DEBUG_LISTEN_PORT 8000 -#define BH_SOCKET_INVALID_SOCK -1 #define BH_WAIT_FOREVER 0xFFFFFFFF -typedef int bh_socket_t; #ifndef NULL # define NULL ((void*) 0) #endif +/** + * Return the offset of the given field in the given type. + * + * @param Type the type containing the filed + * @param field the field in the type + * + * @return the offset of field in Type + */ +#ifndef offsetof +#define offsetof(Type, field) ((size_t)(&((Type *)0)->field)) +#endif + #define bh_assert assert -extern int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, - unsigned int n); -extern int b_strcat_s(char * s1, size_t s1max, const char * s2); -extern int b_strcpy_s(char * s1, size_t s1max, const char * s2); +int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, + unsigned int n); +int b_strcat_s(char * s1, size_t s1max, const char * s2); +int b_strcpy_s(char * s1, size_t s1max, const char * s2); -extern char *bh_strdup(const char *s); +char *bh_read_file_to_buffer(const char *filename, int *ret_size); + +char *bh_strdup(const char *s); + +int bh_platform_init(); #ifdef __cplusplus } diff --git a/core/iwasm/runtime/platform/zephyr/COPYRIGHT b/core/shared-lib/platform/zephyr/COPYRIGHT similarity index 100% rename from core/iwasm/runtime/platform/zephyr/COPYRIGHT rename to core/shared-lib/platform/zephyr/COPYRIGHT diff --git a/core/iwasm/runtime/platform/zephyr/wasm_math.c b/core/shared-lib/platform/zephyr/bh_math.c similarity index 91% rename from core/iwasm/runtime/platform/zephyr/wasm_math.c rename to core/shared-lib/platform/zephyr/bh_math.c index ebea5c490..a27947043 100644 --- a/core/iwasm/runtime/platform/zephyr/wasm_math.c +++ b/core/shared-lib/platform/zephyr/bh_math.c @@ -28,10 +28,7 @@ * $FreeBSD$ */ -#include "wasm_log.h" -#include "wasm_platform.h" -#include "wasm_platform_log.h" -#include "wasm_memory.h" +#include "bh_platform.h" #define __FDLIBM_STDC__ @@ -99,6 +96,13 @@ typedef union { } bits; } IEEEd2bits_B; +static union { + int a; + char b; +} __ue = { .a = 1 }; + +#define is_little_endian() (__ue.b == 1) + #define __HIL(x) *(1+pdouble2pint(&x)) #define __LOL(x) *(pdouble2pint(&x)) #define __HIB(x) *(int*)&x @@ -179,39 +183,39 @@ typedef union { } while (0) /* Macro wrappers. */ -#define EXTRACT_WORDS(ix0,ix1,d) do { \ - if (is_little_endian) \ - EXTRACT_WORDS_L(ix0,ix1,d); \ - else \ - EXTRACT_WORDS_B(ix0,ix1,d); \ +#define EXTRACT_WORDS(ix0,ix1,d) do { \ + if (is_little_endian()) \ + EXTRACT_WORDS_L(ix0,ix1,d); \ + else \ + EXTRACT_WORDS_B(ix0,ix1,d); \ } while (0) -#define INSERT_WORDS(d,ix0,ix1) do { \ - if (is_little_endian) \ - INSERT_WORDS_L(d,ix0,ix1); \ - else \ - INSERT_WORDS_B(d,ix0,ix1); \ +#define INSERT_WORDS(d,ix0,ix1) do { \ + if (is_little_endian()) \ + INSERT_WORDS_L(d,ix0,ix1); \ + else \ + INSERT_WORDS_B(d,ix0,ix1); \ } while (0) -#define GET_HIGH_WORD(i,d) \ - do { \ - if (is_little_endian) \ - GET_HIGH_WORD_L(i,d); \ - else \ - GET_HIGH_WORD_B(i,d); \ - } while (0) +#define GET_HIGH_WORD(i,d) \ + do { \ + if (is_little_endian()) \ + GET_HIGH_WORD_L(i,d); \ + else \ + GET_HIGH_WORD_B(i,d); \ + } while (0) -#define SET_HIGH_WORD(d,v) \ - do { \ - if (is_little_endian) \ - SET_HIGH_WORD_L(d,v); \ - else \ - SET_HIGH_WORD_B(d,v); \ - } while (0) +#define SET_HIGH_WORD(d,v) \ + do { \ + if (is_little_endian()) \ + SET_HIGH_WORD_L(d,v); \ + else \ + SET_HIGH_WORD_B(d,v); \ + } while (0) -#define __HI(x) (is_little_endian ? __HIL(x) : __HIB(x)) +#define __HI(x) (is_little_endian() ? __HIL(x) : __HIB(x)) -#define __LO(x) (is_little_endian ? __LOL(x) : __LOB(x)) +#define __LO(x) (is_little_endian() ? __LOL(x) : __LOB(x)) /* * Attempt to get strict C99 semantics for assignment with non-C99 compilers. @@ -509,7 +513,7 @@ static double freebsd_rint(double x) static int freebsd_isnan(double d) { - if (is_little_endian) { + if (is_little_endian()) { IEEEd2bits_L u; u.d = d; return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0)); diff --git a/core/shared-lib/platform/zephyr/bh_platform.c b/core/shared-lib/platform/zephyr/bh_platform.c index fe05e8bd1..7c4263b31 100755 --- a/core/shared-lib/platform/zephyr/bh_platform.c +++ b/core/shared-lib/platform/zephyr/bh_platform.c @@ -25,3 +25,25 @@ char *bh_strdup(const char *s) memcpy(s1, s, strlen(s) + 1); return s1; } + +#ifndef CONFIG_AEE_ENABLE +static int +_stdout_hook_iwasm(int c) +{ + printk("%c", (char)c); + return 1; +} + +int bh_platform_init() +{ + extern void __stdout_hook_install(int (*hook)(int)); + /* Enable printf() in Zephyr */ + __stdout_hook_install(_stdout_hook_iwasm); + return 0; +} +#else +int bh_platform_init() +{ + return 0; +} +#endif diff --git a/core/shared-lib/platform/zephyr/bh_platform.h b/core/shared-lib/platform/zephyr/bh_platform.h index 4e22d9049..2602bee31 100644 --- a/core/shared-lib/platform/zephyr/bh_platform.h +++ b/core/shared-lib/platform/zephyr/bh_platform.h @@ -52,7 +52,6 @@ /* Invalid thread tid */ #define INVALID_THREAD_ID NULL -#define INVALID_SEM_ID SEM_FAILED #define BH_WAIT_FOREVER K_FOREVER typedef uint64_t uint64; @@ -89,6 +88,18 @@ int snprintf(char *buffer, size_t count, const char *format, ...); #define NULL ((void*)0) #endif +/** + * Return the offset of the given field in the given type. + * + * @param Type the type containing the filed + * @param field the field in the type + * + * @return the offset of field in Type + */ +#ifndef offsetof +#define offsetof(Type, field) ((size_t)(&((Type *)0)->field)) +#endif + #define bh_assert(x) \ do { \ if (!(x)) { \ @@ -96,9 +107,23 @@ int snprintf(char *buffer, size_t count, const char *format, ...); } \ } while (0) -extern int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, - unsigned int n); -extern int b_strcat_s(char * s1, size_t s1max, const char * s2); -extern int b_strcpy_s(char * s1, size_t s1max, const char * s2); +int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, + unsigned int n); +int b_strcat_s(char * s1, size_t s1max, const char * s2); +int b_strcpy_s(char * s1, size_t s1max, const char * s2); + +/* math functions */ +double sqrt(double x); +double floor(double x); +double ceil(double x); +double fmin(double x, double y); +double fmax(double x, double y); +double rint(double x); +double fabs(double x); +double trunc(double x); +int signbit(double x); +int isnan(double x); + +int bh_platform_init(); #endif diff --git a/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/main.c b/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/main.c index a4313df91..2b91f0b4c 100644 --- a/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/main.c +++ b/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/main.c @@ -16,9 +16,9 @@ #include #include +#include "bh_platform.h" #include "wasm_assert.h" #include "wasm_log.h" -#include "wasm_platform.h" #include "wasm_platform_log.h" #include "wasm_thread.h" #include "wasm_export.h" diff --git a/samples/littlevgl/vgl-wasm-runtime/zephyr-build/CMakeLists.txt b/samples/littlevgl/vgl-wasm-runtime/zephyr-build/CMakeLists.txt index bd7c01698..a564c6aab 100644 --- a/samples/littlevgl/vgl-wasm-runtime/zephyr-build/CMakeLists.txt +++ b/samples/littlevgl/vgl-wasm-runtime/zephyr-build/CMakeLists.txt @@ -53,9 +53,8 @@ target_include_directories(app PRIVATE ${IWASM_ROOT}/runtime/include ${CMAKE_CURRENT_SOURCE_DIR}/../src/platform/zephyr ) -set (IWASM_SRCS ${IWASM_ROOT}/runtime/platform/zephyr/wasm_math.c +set (IWASM_SRCS ${IWASM_ROOT}/runtime/platform/zephyr/wasm_native.c - ${IWASM_ROOT}/runtime/platform/zephyr/wasm_platform.c ${IWASM_ROOT}/runtime/utils/wasm_dlfcn.c ${IWASM_ROOT}/runtime/utils/wasm_hashmap.c ${IWASM_ROOT}/runtime/utils/wasm_log.c @@ -92,6 +91,7 @@ set (IWASM_SRCS ${IWASM_ROOT}/runtime/platform/zephyr/wasm_math.c ${SHARED_LIB_ROOT}/platform/zephyr/bh_platform_log.c ${SHARED_LIB_ROOT}/platform/zephyr/bh_thread.c ${SHARED_LIB_ROOT}/platform/zephyr/bh_time.c + ${SHARED_LIB_ROOT}/platform/zephyr/bh_math.c ${SHARED_LIB_ROOT}/mem-alloc/bh_memory.c ${SHARED_LIB_ROOT}/mem-alloc/mem_alloc.c ${SHARED_LIB_ROOT}/mem-alloc/ems/ems_alloc.c