Răsfoiți Sursa

Re-org platform APIs: move most platform APIs of iwasm to shared-lib (#45)

wenyongh 6 ani în urmă
părinte
comite
1db5a2f697
39 a modificat fișierele cu 387 adăugiri și 899 ștergeri
  1. 2 3
      core/iwasm/products/alios-things/iwasm.mk
  2. 1 1
      core/iwasm/products/alios-things/src/main.c
  3. 3 3
      core/iwasm/products/linux/main.c
  4. 2 2
      core/iwasm/products/vxworks/main.c
  5. 1 2
      core/iwasm/products/zephyr/simple/CMakeLists.txt
  6. 1 1
      core/iwasm/products/zephyr/simple/src/main.c
  7. 10 18
      core/iwasm/runtime/include/wasm_dlfcn.h
  8. 1 1
      core/iwasm/runtime/include/wasm_hashmap.h
  9. 1 1
      core/iwasm/runtime/include/wasm_log.h
  10. 1 1
      core/iwasm/runtime/include/wasm_vector.h
  11. 0 0
      core/iwasm/runtime/platform/alios/wasm_native.c
  12. 0 104
      core/iwasm/runtime/platform/alios/wasm_platform.h
  13. 0 38
      core/iwasm/runtime/platform/include/wasm_types.h
  14. 0 96
      core/iwasm/runtime/platform/linux/wasm_platform.c
  15. 0 104
      core/iwasm/runtime/platform/linux/wasm_platform.h
  16. 0 96
      core/iwasm/runtime/platform/vxworks/wasm_platform.c
  17. 0 104
      core/iwasm/runtime/platform/vxworks/wasm_platform.h
  18. 0 56
      core/iwasm/runtime/platform/zephyr/wasm_platform.c
  19. 0 105
      core/iwasm/runtime/platform/zephyr/wasm_platform.h
  20. 1 1
      core/iwasm/runtime/utils/wasm_dlfcn.c
  21. 1 1
      core/iwasm/runtime/vmcore-wasm/wasm.h
  22. 9 2
      core/iwasm/runtime/vmcore-wasm/wasm_application.c
  23. 10 2
      core/iwasm/runtime/vmcore-wasm/wasm_loader.c
  24. 1 1
      core/iwasm/runtime/vmcore-wasm/wasm_runtime.c
  25. 0 0
      core/shared-lib/platform/alios/COPYRIGHT
  26. 35 31
      core/shared-lib/platform/alios/bh_math.c
  27. 6 0
      core/shared-lib/platform/alios/bh_platform.c
  28. 26 2
      core/shared-lib/platform/alios/bh_platform.h
  29. 2 0
      core/shared-lib/platform/include/bh_types.h
  30. 57 2
      core/shared-lib/platform/linux/bh_platform.c
  31. 36 43
      core/shared-lib/platform/linux/bh_platform.h
  32. 59 0
      core/shared-lib/platform/vxworks/bh_platform.c
  33. 31 39
      core/shared-lib/platform/vxworks/bh_platform.h
  34. 0 0
      core/shared-lib/platform/zephyr/COPYRIGHT
  35. 35 31
      core/shared-lib/platform/zephyr/bh_math.c
  36. 22 0
      core/shared-lib/platform/zephyr/bh_platform.c
  37. 30 5
      core/shared-lib/platform/zephyr/bh_platform.h
  38. 1 1
      samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/main.c
  39. 2 2
      samples/littlevgl/vgl-wasm-runtime/zephyr-build/CMakeLists.txt

+ 2 - 3
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 \

+ 1 - 1
core/iwasm/products/alios-things/src/main.c

@@ -16,9 +16,9 @@
 
 #include <stdlib.h>
 #include <string.h>
+#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"

+ 3 - 3
core/iwasm/products/linux/main.c

@@ -19,9 +19,9 @@
 #endif
 #include <stdlib.h>
 #include <string.h>
+#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 */

+ 2 - 2
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 */

+ 1 - 2
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

+ 1 - 1
core/iwasm/products/zephyr/simple/src/main.c

@@ -16,9 +16,9 @@
 
 #include <stdlib.h>
 #include <string.h>
+#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"

+ 10 - 18
core/iwasm/runtime/platform/alios/wasm_platform.c → 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 */
 

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 0 - 0
core/iwasm/runtime/platform/alios/wasm-native.c → core/iwasm/runtime/platform/alios/wasm_native.c


+ 0 - 104
core/iwasm/runtime/platform/alios/wasm_platform.h

@@ -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 <aos/kernel.h>
-#include <inttypes.h>
-
-#include <stdbool.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 "AliOS"
-#define __ALIOS__ 1
-
-#include <stdarg.h>
-#include <ctype.h>
-#include <limits.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-/**
- * 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 <string.h>
-
-/* 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 <stdio.h> */
-
-/* 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

+ 0 - 38
core/iwasm/runtime/platform/include/wasm_types.h

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

+ 0 - 96
core/iwasm/runtime/platform/linux/wasm_platform.c

@@ -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 <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <dlfcn.h>
-
-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;
-}
-

+ 0 - 104
core/iwasm/runtime/platform/linux/wasm_platform.h

@@ -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 <inttypes.h>
-#include <stdbool.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 "Linux"
-
-#include <stdarg.h>
-#include <ctype.h>
-#include <pthread.h>
-#include <limits.h>
-#include <semaphore.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <math.h>
-
-/**
- * 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 <string.h>
-
-/* 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 <stdio.h> */
-
-/* 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 <math.h> */
-
-#ifndef __cplusplus
-double sqrt(double x);
-#endif
-
-#include <stdio.h>
-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

+ 0 - 96
core/iwasm/runtime/platform/vxworks/wasm_platform.c

@@ -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 <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <dlfcn.h>
-
-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;
-}
-

+ 0 - 104
core/iwasm/runtime/platform/vxworks/wasm_platform.h

@@ -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 <inttypes.h>
-#include <stdbool.h>
-#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 <stdarg.h>
-#include <ctype.h>
-#include <pthread.h>
-#include <limits.h>
-#include <semaphore.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <math.h>
-
-/**
- * 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 <string.h>
-
-/* 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 <stdio.h> */
-
-/* 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 <math.h> */
-
-#ifndef __cplusplus
-double sqrt(double x);
-#endif
-
-#include <stdio.h>
-
-
-char*
-wasm_read_file_to_buffer(const char *filename, int *ret_size);
-
-void*
-wasm_dlsym(void *handle, const char *symbol);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif

+ 0 - 56
core/iwasm/runtime/platform/zephyr/wasm_platform.c

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

+ 0 - 105
core/iwasm/runtime/platform/zephyr/wasm_platform.h

@@ -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 <autoconf.h>
-#include <zephyr.h>
-#include <kernel.h>
-#include <inttypes.h>
-
-#include <stdbool.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 "Zephyr"
-
-#include <stdarg.h>
-#include <ctype.h>
-#include <limits.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-/**
- * 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 <string.h>
-
-/* 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 <stdio.h> */
-
-/* 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

+ 1 - 1
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;

+ 1 - 1
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"
 

+ 9 - 2
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;
                         }

+ 10 - 2
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) {

+ 1 - 1
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)

+ 0 - 0
core/iwasm/runtime/platform/alios/COPYRIGHT → core/shared-lib/platform/alios/COPYRIGHT


+ 35 - 31
core/iwasm/runtime/platform/alios/wasm_math.c → 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));

+ 6 - 0
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;
+}
+

+ 26 - 2
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 */
 

+ 2 - 0
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

+ 57 - 2
core/shared-lib/platform/linux/bh_platform.c

@@ -15,8 +15,10 @@
  */
 
 #include "bh_platform.h"
-#include <stdlib.h>
-#include <string.h>
+
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 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;
+}
+

+ 36 - 43
core/shared-lib/platform/linux/bh_platform.h

@@ -26,11 +26,16 @@
 #include <time.h>
 #include <string.h>
 #include <stdio.h>
-#include <assert.h>
-
-#ifndef __cplusplus
-int snprintf(char *buffer, size_t count, const char *format, ...);
-#endif
+#include <stdlib.h>
+#include <math.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <pthread.h>
+#include <limits.h>
+#include <semaphore.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -47,46 +52,19 @@ extern void DEBUGME(void);
 
 /* NEED qsort */
 
-#include <stdarg.h>
-#include <ctype.h>
-#include <pthread.h>
-#include <limits.h>
-#include <semaphore.h>
-#include <errno.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-
 #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);
+
+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);
 
-extern char *bh_strdup(const char *s);
+int bh_platform_init();
 
 #ifdef __cplusplus
 }

+ 59 - 0
core/shared-lib/platform/vxworks/bh_platform.c

@@ -17,6 +17,12 @@
 #include "bh_platform.h"
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <dlfcn.h>
+
 
 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;
+}
+

+ 31 - 39
core/shared-lib/platform/vxworks/bh_platform.h

@@ -27,10 +27,13 @@
 #include <string.h>
 #include <stdio.h>
 #include <assert.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <pthread.h>
+#include <limits.h>
+#include <semaphore.h>
+#include <errno.h>
 
-#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 <stdarg.h>
-#include <ctype.h>
-#include <pthread.h>
-#include <limits.h>
-#include <semaphore.h>
-#include <errno.h>
-
 #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);
+
+char *bh_read_file_to_buffer(const char *filename, int *ret_size);
+
+char *bh_strdup(const char *s);
 
-extern char *bh_strdup(const char *s);
+int bh_platform_init();
 
 #ifdef __cplusplus
 }

+ 0 - 0
core/iwasm/runtime/platform/zephyr/COPYRIGHT → core/shared-lib/platform/zephyr/COPYRIGHT


+ 35 - 31
core/iwasm/runtime/platform/zephyr/wasm_math.c → 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));

+ 22 - 0
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

+ 30 - 5
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

+ 1 - 1
samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/main.c

@@ -16,9 +16,9 @@
 
 #include <stdlib.h>
 #include <string.h>
+#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"

+ 2 - 2
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