Просмотр исходного кода

Re-org address unalignment access for fast-interp (#597)

And clear some compile warnings on wasm loader, add ${UV_A_LIBS} for some CMakeLists.txt.
Wenyong Huang 4 лет назад
Родитель
Сommit
7db2221ad9

+ 8 - 6
core/config.h

@@ -131,18 +131,23 @@
 #define WASM_ENABLE_LOG 1
 #endif
 
-#if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_X86_64)
-#define WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS 1
+#ifndef WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS
+#if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_X86_64) \
+    || defined(BUILD_TARGET_AARCH64)
+#define WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS 1
 #else
-#define WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS 0
+#define WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS 0
+#endif
 #endif
 
 /* WASM Interpreter labels-as-values feature */
+#ifndef WASM_ENABLE_LABELS_AS_VALUES
 #ifdef __GNUC__
 #define WASM_ENABLE_LABELS_AS_VALUES 1
 #else
 #define WASM_ENABLE_LABELS_AS_VALUES 0
 #endif
+#endif
 
 /* Enable fast interpreter or not */
 #ifndef WASM_ENABLE_FAST_INTERP
@@ -150,10 +155,7 @@
 #endif
 
 #if WASM_ENABLE_FAST_INTERP != 0
-#define WASM_ENABLE_ABS_LABEL_ADDR 1
 #define WASM_DEBUG_PREPROCESSOR 0
-#else
-#define WASM_ENABLE_ABS_LABEL_ADDR 0
 #endif
 
 /* Enable opcode counter or not */

+ 0 - 15
core/iwasm/aot/aot_runtime.c

@@ -1001,21 +1001,6 @@ aot_lookup_function(const AOTModuleInstance *module_inst,
     return NULL;
 }
 
-#define PUT_I64_TO_ADDR(addr, value) do {       \
-    union { int64 val; uint32 parts[2]; } u;    \
-    u.val = (value);                            \
-    (addr)[0] = u.parts[0];                     \
-    (addr)[1] = u.parts[1];                     \
-  } while (0)
-
-#define PUT_F64_TO_ADDR(addr, value) do {       \
-    union { float64 val; uint32 parts[2]; } u;  \
-    u.val = (value);                            \
-    (addr)[0] = u.parts[0];                     \
-    (addr)[1] = u.parts[1];                     \
-  } while (0)
-
-
 #ifdef OS_ENABLE_HW_BOUND_CHECK
 
 #define STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT 3

+ 0 - 14
core/iwasm/common/wasm_runtime_common.c

@@ -2825,20 +2825,6 @@ fail:
  * Implementation of wasm_runtime_invoke_native()
  */
 
-#define PUT_I64_TO_ADDR(addr, value) do {       \
-    union { int64 val; uint32 parts[2]; } u;    \
-    u.val = (value);                            \
-    (addr)[0] = u.parts[0];                     \
-    (addr)[1] = u.parts[1];                     \
-  } while (0)
-
-#define PUT_F64_TO_ADDR(addr, value) do {       \
-    union { float64 val; uint32 parts[2]; } u;  \
-    u.val = (value);                            \
-    (addr)[0] = u.parts[0];                     \
-    (addr)[1] = u.parts[1];                     \
-  } while (0)
-
 /* The invoke native implementation on ARM platform with VFP co-processor */
 #if defined(BUILD_TARGET_ARM_VFP) \
     || defined(BUILD_TARGET_THUMB_VFP) \

+ 224 - 0
core/iwasm/common/wasm_runtime_common.h

@@ -25,6 +25,230 @@
 extern "C" {
 #endif
 
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+
+#define PUT_I64_TO_ADDR(addr, value) do {       \
+    *(int64*)(addr) = (int64)(value);           \
+  } while (0)
+#define PUT_F64_TO_ADDR(addr, value) do {       \
+    *(float64*)(addr) = (float64)(value);       \
+  } while (0)
+
+#define GET_I64_FROM_ADDR(addr) (*(int64*)(addr))
+#define GET_F64_FROM_ADDR(addr) (*(float64*)(addr))
+
+/* For STORE opcodes */
+#define STORE_I64 PUT_I64_TO_ADDR
+#define STORE_U32(addr, value) do {             \
+    *(uint32*)(addr) = (uint32)(value);         \
+  } while (0)
+#define STORE_U16(addr, value) do {             \
+    *(uint16*)(addr) = (uint16)(value);         \
+  } while (0)
+
+/* For LOAD opcodes */
+#define LOAD_I64(addr) (*(int64*)(addr))
+#define LOAD_F64(addr) (*(float64*)(addr))
+#define LOAD_I32(addr) (*(int32*)(addr))
+#define LOAD_U32(addr) (*(uint32*)(addr))
+#define LOAD_I16(addr) (*(int16*)(addr))
+#define LOAD_U16(addr) (*(uint16*)(addr))
+
+#define STORE_PTR(addr, ptr) do {               \
+    *(void**)addr = (void*)ptr;                 \
+  } while (0)
+#define LOAD_PTR(addr) (*(void**)(addr))
+
+#else  /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
+
+#define PUT_I64_TO_ADDR(addr, value) do {       \
+    uint32 *addr_u32 = (uint32*)(addr);         \
+    union { int64 val; uint32 parts[2]; } u;    \
+    u.val = (int64)(value);                     \
+    addr_u32[0] = u.parts[0];                   \
+    addr_u32[1] = u.parts[1];                   \
+  } while (0)
+#define PUT_F64_TO_ADDR(addr, value) do {       \
+    uint32 *addr_u32 = (uint32*)(addr);         \
+    union { float64 val; uint32 parts[2]; } u;  \
+    u.val = (value);                            \
+    addr_u32[0] = u.parts[0];                   \
+    addr_u32[1] = u.parts[1];                   \
+  } while (0)
+
+static inline int64
+GET_I64_FROM_ADDR(uint32 *addr)
+{
+    union { int64 val; uint32 parts[2]; } u;
+    u.parts[0] = addr[0];
+    u.parts[1] = addr[1];
+    return u.val;
+}
+
+static inline float64
+GET_F64_FROM_ADDR (uint32 *addr)
+{
+    union { float64 val; uint32 parts[2]; } u;
+    u.parts[0] = addr[0];
+    u.parts[1] = addr[1];
+    return u.val;
+}
+
+/* For STORE opcodes */
+#define STORE_I64(addr, value) do {             \
+    uintptr_t addr1 = (uintptr_t)(addr);        \
+    union { int64 val; uint32 u32[2];           \
+            uint16 u16[4]; uint8 u8[8]; } u;    \
+    if ((addr1 & (uintptr_t)7) == 0)            \
+      *(int64*)(addr) = (int64)(value);         \
+    else {                                      \
+        u.val = (int64)(value);                 \
+        if ((addr1 & (uintptr_t)3) == 0) {      \
+            ((uint32*)(addr))[0] = u.u32[0];    \
+            ((uint32*)(addr))[1] = u.u32[1];    \
+        }                                       \
+        else if ((addr1 & (uintptr_t)1) == 0) { \
+            ((uint16*)(addr))[0] = u.u16[0];    \
+            ((uint16*)(addr))[1] = u.u16[1];    \
+            ((uint16*)(addr))[2] = u.u16[2];    \
+            ((uint16*)(addr))[3] = u.u16[3];    \
+        }                                       \
+        else {                                  \
+            int32 t;                            \
+            for (t = 0; t < 8; t++)             \
+                ((uint8*)(addr))[t] = u.u8[t];  \
+        }                                       \
+    }                                           \
+  } while (0)
+
+#define STORE_U32(addr, value) do {             \
+    uintptr_t addr1 = (uintptr_t)(addr);        \
+    union { uint32 val;                         \
+            uint16 u16[2]; uint8 u8[4]; } u;    \
+    if ((addr1 & (uintptr_t)3) == 0)            \
+      *(uint32*)(addr) = (uint32)(value);       \
+    else {                                      \
+        u.val = (uint32)(value);                \
+        if ((addr1 & (uintptr_t)1) == 0) {      \
+            ((uint16*)(addr))[0] = u.u16[0];    \
+            ((uint16*)(addr))[1] = u.u16[1];    \
+        }                                       \
+        else {                                  \
+            ((uint8*)(addr))[0] = u.u8[0];      \
+            ((uint8*)(addr))[1] = u.u8[1];      \
+            ((uint8*)(addr))[2] = u.u8[2];      \
+            ((uint8*)(addr))[3] = u.u8[3];      \
+        }                                       \
+    }                                           \
+  } while (0)
+
+#define STORE_U16(addr, value) do {             \
+    union { uint16 val; uint8 u8[2]; } u;       \
+    u.val = (uint16)(value);                    \
+    ((uint8*)(addr))[0] = u.u8[0];              \
+    ((uint8*)(addr))[1] = u.u8[1];              \
+  } while (0)
+
+/* For LOAD opcodes */
+static inline int64
+LOAD_I64(void *addr)
+{
+    uintptr_t addr1 = (uintptr_t)addr;
+    union { int64 val; uint32 u32[2];
+            uint16 u16[4]; uint8 u8[8]; } u;
+    if ((addr1 & (uintptr_t)7) == 0)
+        return *(int64*)addr;
+
+    if ((addr1 & (uintptr_t)3) == 0) {
+        u.u32[0] = ((uint32*)addr)[0];
+        u.u32[1] = ((uint32*)addr)[1];
+    }
+    else if ((addr1 & (uintptr_t)1) == 0) {
+        u.u16[0] = ((uint16*)addr)[0];
+        u.u16[1] = ((uint16*)addr)[1];
+        u.u16[2] = ((uint16*)addr)[2];
+        u.u16[3] = ((uint16*)addr)[3];
+    }
+    else {
+        int32 t;
+        for (t = 0; t < 8; t++)
+            u.u8[t] = ((uint8*)addr)[t];
+    }
+    return u.val;
+}
+
+static inline float64
+LOAD_F64(void *addr)
+{
+    uintptr_t addr1 = (uintptr_t)addr;
+    union { float64 val; uint32 u32[2];
+            uint16 u16[4]; uint8 u8[8]; } u;
+    if ((addr1 & (uintptr_t)7) == 0)
+        return *(float64*)addr;
+
+    if ((addr1 & (uintptr_t)3) == 0) {
+        u.u32[0] = ((uint32*)addr)[0];
+        u.u32[1] = ((uint32*)addr)[1];
+    }
+    else if ((addr1 & (uintptr_t)1) == 0) {
+        u.u16[0] = ((uint16*)addr)[0];
+        u.u16[1] = ((uint16*)addr)[1];
+        u.u16[2] = ((uint16*)addr)[2];
+        u.u16[3] = ((uint16*)addr)[3];
+    }
+    else {
+        int32 t;
+        for (t = 0; t < 8; t++)
+            u.u8[t] = ((uint8*)addr)[t];
+    }
+    return u.val;
+}
+
+static inline int32
+LOAD_I32(void *addr)
+{
+    uintptr_t addr1 = (uintptr_t)addr;
+    union { int32 val; uint16 u16[2]; uint8 u8[4]; } u;
+    if ((addr1 & (uintptr_t)3) == 0)
+        return *(int32*)addr;
+
+    if ((addr1 & (uintptr_t)1) == 0) {
+        u.u16[0] = ((uint16*)addr)[0];
+        u.u16[1] = ((uint16*)addr)[1];
+    }
+    else {
+        u.u8[0] = ((uint8*)addr)[0];
+        u.u8[1] = ((uint8*)addr)[1];
+        u.u8[2] = ((uint8*)addr)[2];
+        u.u8[3] = ((uint8*)addr)[3];
+    }
+    return u.val;
+}
+
+static inline int16
+LOAD_I16(void *addr)
+{
+    uintptr_t addr1 = (uintptr_t)addr;
+    union { int16 val; uint8 u8[2]; } u;
+    if ((addr1 & (uintptr_t)1)) {
+        u.u8[0] = ((uint8*)addr)[0];
+        u.u8[1] = ((uint8*)addr)[1];
+        return u.val;
+    }
+    return *(int16*)addr;
+}
+
+#define LOAD_U32(addr) ((uint32)LOAD_I32(addr))
+#define LOAD_U16(addr) ((uint16)LOAD_I16(addr))
+
+#if UINTPTR_MAX == UINT32_MAX
+#define STORE_PTR(addr, ptr) STORE_U32(addr, (uintptr_t)ptr)
+#elif UINTPTR_MAX == UINT64_MAX
+#define STORE_PTR(addr, ptr) STORE_I64(addr, (uintptr_t)ptr)
+#endif
+
+#endif  /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
+
 typedef struct WASMModuleCommon {
     /* Module type, for module loaded from WASM bytecode binary,
        this field is Wasm_Module_Bytecode, and this structure should

+ 1 - 207
core/iwasm/interpreter/wasm_interp_classic.c

@@ -20,212 +20,6 @@ typedef float64 CellType_F64;
 
 #define BR_TABLE_TMP_BUF_LEN 32
 
-/* 64-bit Memory accessors. */
-#if WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0
-#define PUT_I64_TO_ADDR(addr, value) do {       \
-    *(int64*)(addr) = (int64)(value);           \
-  } while (0)
-#define PUT_F64_TO_ADDR(addr, value) do {       \
-    *(float64*)(addr) = (float64)(value);       \
-  } while (0)
-
-#define GET_I64_FROM_ADDR(addr) (*(int64*)(addr))
-#define GET_F64_FROM_ADDR(addr) (*(float64*)(addr))
-
-/* For STORE opcodes */
-#define STORE_I64 PUT_I64_TO_ADDR
-#define STORE_U32(addr, value) do {             \
-    *(uint32*)(addr) = (uint32)(value);         \
-  } while (0)
-#define STORE_U16(addr, value) do {             \
-    *(uint16*)(addr) = (uint16)(value);         \
-  } while (0)
-
-/* For LOAD opcodes */
-#define LOAD_I64(addr) (*(int64*)(addr))
-#define LOAD_F64(addr) (*(float64*)(addr))
-#define LOAD_I32(addr) (*(int32*)(addr))
-#define LOAD_U32(addr) (*(uint32*)(addr))
-#define LOAD_I16(addr) (*(int16*)(addr))
-#define LOAD_U16(addr) (*(uint16*)(addr))
-
-#else  /* WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0 */
-#define PUT_I64_TO_ADDR(addr, value) do {       \
-    union { int64 val; uint32 parts[2]; } u;    \
-    u.val = (int64)(value);                     \
-    (addr)[0] = u.parts[0];                     \
-    (addr)[1] = u.parts[1];                     \
-  } while (0)
-#define PUT_F64_TO_ADDR(addr, value) do {       \
-    union { float64 val; uint32 parts[2]; } u;  \
-    u.val = (value);                            \
-    (addr)[0] = u.parts[0];                     \
-    (addr)[1] = u.parts[1];                     \
-  } while (0)
-
-static inline int64
-GET_I64_FROM_ADDR(uint32 *addr)
-{
-    union { int64 val; uint32 parts[2]; } u;
-    u.parts[0] = addr[0];
-    u.parts[1] = addr[1];
-    return u.val;
-}
-
-static inline float64
-GET_F64_FROM_ADDR (uint32 *addr)
-{
-    union { float64 val; uint32 parts[2]; } u;
-    u.parts[0] = addr[0];
-    u.parts[1] = addr[1];
-    return u.val;
-}
-
-/* For STORE opcodes */
-#define STORE_I64(addr, value) do {             \
-    uintptr_t addr1 = (uintptr_t)(addr);        \
-    union { int64 val; uint32 u32[2];           \
-            uint16 u16[4]; uint8 u8[8]; } u;    \
-    if ((addr1 & (uintptr_t)7) == 0)            \
-      *(int64*)(addr) = (int64)(value);         \
-    else {                                      \
-        u.val = (int64)(value);                 \
-        if ((addr1 & (uintptr_t)3) == 0) {      \
-            ((uint32*)(addr))[0] = u.u32[0];    \
-            ((uint32*)(addr))[1] = u.u32[1];    \
-        }                                       \
-        else if ((addr1 & (uintptr_t)1) == 0) { \
-            ((uint16*)(addr))[0] = u.u16[0];    \
-            ((uint16*)(addr))[1] = u.u16[1];    \
-            ((uint16*)(addr))[2] = u.u16[2];    \
-            ((uint16*)(addr))[3] = u.u16[3];    \
-        }                                       \
-        else {                                  \
-            int32 t;                            \
-            for (t = 0; t < 8; t++)             \
-                ((uint8*)(addr))[t] = u.u8[t];  \
-        }                                       \
-    }                                           \
-  } while (0)
-
-#define STORE_U32(addr, value) do {             \
-    uintptr_t addr1 = (uintptr_t)(addr);        \
-    union { uint32 val;                         \
-            uint16 u16[2]; uint8 u8[4]; } u;    \
-    if ((addr1 & (uintptr_t)3) == 0)            \
-      *(uint32*)(addr) = (uint32)(value);       \
-    else {                                      \
-        u.val = (uint32)(value);                \
-        if ((addr1 & (uintptr_t)1) == 0) {      \
-            ((uint16*)(addr))[0] = u.u16[0];    \
-            ((uint16*)(addr))[1] = u.u16[1];    \
-        }                                       \
-        else {                                  \
-            ((uint8*)(addr))[0] = u.u8[0];      \
-            ((uint8*)(addr))[1] = u.u8[1];      \
-            ((uint8*)(addr))[2] = u.u8[2];      \
-            ((uint8*)(addr))[3] = u.u8[3];      \
-        }                                       \
-    }                                           \
-  } while (0)
-
-#define STORE_U16(addr, value) do {             \
-    union { uint16 val; uint8 u8[2]; } u;       \
-    u.val = (uint16)(value);                    \
-    ((uint8*)(addr))[0] = u.u8[0];              \
-    ((uint8*)(addr))[1] = u.u8[1];              \
-  } while (0)
-
-/* For LOAD opcodes */
-static inline int64
-LOAD_I64(void *addr)
-{
-    uintptr_t addr1 = (uintptr_t)addr;
-    union { int64 val; uint32 u32[2];
-            uint16 u16[4]; uint8 u8[8]; } u;
-    if ((addr1 & (uintptr_t)7) == 0)
-        return *(int64*)addr;
-
-    if ((addr1 & (uintptr_t)3) == 0) {
-        u.u32[0] = ((uint32*)addr)[0];
-        u.u32[1] = ((uint32*)addr)[1];
-    }
-    else if ((addr1 & (uintptr_t)1) == 0) {
-        u.u16[0] = ((uint16*)addr)[0];
-        u.u16[1] = ((uint16*)addr)[1];
-        u.u16[2] = ((uint16*)addr)[2];
-        u.u16[3] = ((uint16*)addr)[3];
-    }
-    else {
-        int32 t;
-        for (t = 0; t < 8; t++)
-            u.u8[t] = ((uint8*)addr)[t];
-    }
-    return u.val;
-}
-
-static inline float64
-LOAD_F64(void *addr)
-{
-    uintptr_t addr1 = (uintptr_t)addr;
-    union { float64 val; uint32 u32[2];
-            uint16 u16[4]; uint8 u8[8]; } u;
-    if ((addr1 & (uintptr_t)7) == 0)
-        return *(float64*)addr;
-
-    if ((addr1 & (uintptr_t)3) == 0) {
-        u.u32[0] = ((uint32*)addr)[0];
-        u.u32[1] = ((uint32*)addr)[1];
-    }
-    else if ((addr1 & (uintptr_t)1) == 0) {
-        u.u16[0] = ((uint16*)addr)[0];
-        u.u16[1] = ((uint16*)addr)[1];
-        u.u16[2] = ((uint16*)addr)[2];
-        u.u16[3] = ((uint16*)addr)[3];
-    }
-    else {
-        int32 t;
-        for (t = 0; t < 8; t++)
-            u.u8[t] = ((uint8*)addr)[t];
-    }
-    return u.val;
-}
-
-static inline int32
-LOAD_I32(void *addr)
-{
-    uintptr_t addr1 = (uintptr_t)addr;
-    union { int32 val; uint16 u16[2]; uint8 u8[4]; } u;
-    if ((addr1 & (uintptr_t)3) == 0)
-        return *(int32*)addr;
-
-    if ((addr1 & (uintptr_t)1) == 0) {
-        u.u16[0] = ((uint16*)addr)[0];
-        u.u16[1] = ((uint16*)addr)[1];
-    }
-    else {
-        u.u8[0] = ((uint8*)addr)[0];
-        u.u8[1] = ((uint8*)addr)[1];
-        u.u8[2] = ((uint8*)addr)[2];
-        u.u8[3] = ((uint8*)addr)[3];
-    }
-    return u.val;
-}
-
-static inline int16
-LOAD_I16(void *addr)
-{
-    union { int16 val; uint8 u8[2]; } u;
-    u.u8[0] = ((uint8*)addr)[0];
-    u.u8[1] = ((uint8*)addr)[1];
-    return u.val;
-}
-
-#define LOAD_U32(addr) ((uint32)LOAD_I32(addr))
-#define LOAD_U16(addr) ((uint16)LOAD_I16(addr))
-
-#endif  /* WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0 */
-
 #define CHECK_MEMORY_OVERFLOW(bytes) do {                                   \
     uint64 offset1 = (uint64)offset + (uint64)addr;                         \
     if (offset1 + bytes <= (uint64)linear_mem_size)                         \
@@ -579,7 +373,7 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
     *(src_type2*)(frame_sp);                                                \
   } while (0)
 
-#if WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
 #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
 #else
 #define DEF_OP_NUMERIC_64(src_type1, src_type2, src_op_type, operation) do {\

Разница между файлами не показана из-за своего большого размера
+ 231 - 370
core/iwasm/interpreter/wasm_interp_fast.c


+ 120 - 51
core/iwasm/interpreter/wasm_loader.c

@@ -2533,7 +2533,7 @@ static bool
 wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
                              char *error_buf, uint32 error_buf_size);
 
-#if WASM_ENABLE_FAST_INTERP != 0
+#if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
 void **
 wasm_interp_get_handle_table();
 
@@ -2836,7 +2836,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
         }
     }
 
-#if WASM_ENABLE_FAST_INTERP != 0
+#if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
     handle_table = wasm_interp_get_handle_table();
 #endif
 
@@ -3859,7 +3859,7 @@ static bool
 check_offset_push(WASMLoaderContext *ctx,
                   char *error_buf, uint32 error_buf_size)
 {
-    uint32 cell_num = (ctx->frame_offset - ctx->frame_offset_bottom);
+    uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
     if (ctx->frame_offset >= ctx->frame_offset_boundary) {
         MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
                     ctx->frame_offset_size + 16);
@@ -4209,22 +4209,20 @@ fail:
 
 #if WASM_ENABLE_FAST_INTERP != 0
 
-#if WASM_ENABLE_ABS_LABEL_ADDR != 0
-
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
 #define emit_label(opcode) do {                                     \
     wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]);         \
     LOG_OP("\nemit_op [%02x]\t", opcode);                           \
   } while (0)
-
 #define skip_label() do {                                           \
     wasm_loader_emit_backspace(loader_ctx, sizeof(void *));         \
     LOG_OP("\ndelete last op\n");                                   \
   } while (0)
-
-#else
-
+#else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
 #define emit_label(opcode) do {                                     \
-    int32 offset = (int32)(handle_table[opcode] - handle_table[0]); \
+    int32 offset = (int32)((uint8*)handle_table[opcode]             \
+                           - (uint8*)handle_table[0]);              \
     if (!(offset >= INT16_MIN && offset < INT16_MAX)) {             \
         set_error_buf(error_buf, error_buf_size,                    \
                       "pre-compiled label offset out of range");    \
@@ -4233,14 +4231,21 @@ fail:
     wasm_loader_emit_int16(loader_ctx, offset);                     \
     LOG_OP("\nemit_op [%02x]\t", opcode);                           \
   } while (0)
-
-/* drop local.get / const / block / loop / end */
 #define skip_label() do {                                           \
     wasm_loader_emit_backspace(loader_ctx, sizeof(int16));          \
     LOG_OP("\ndelete last op\n");                                   \
   } while (0)
-
-#endif /* WASM_ENABLE_ABS_LABEL_ADDR */
+#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+#else /* else of WASM_ENABLE_LABELS_AS_VALUES */
+#define emit_label(opcode) do {                                     \
+    wasm_loader_emit_uint8(loader_ctx, opcode);                     \
+    LOG_OP("\nemit_op [%02x]\t", opcode);                           \
+  } while (0)
+#define skip_label() do {                                           \
+    wasm_loader_emit_backspace(loader_ctx, sizeof(uint8));          \
+    LOG_OP("\ndelete last op\n");                                   \
+  } while (0)
+#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
 
 #define emit_empty_label_addr_and_frame_ip(type) do {               \
     if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type,   \
@@ -4361,22 +4366,36 @@ static void
 wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
 {
     if (ctx->p_code_compiled) {
-        *(uint32*)(ctx->p_code_compiled) = value;
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
+#endif
+        STORE_U32(ctx->p_code_compiled, value);
         ctx->p_code_compiled += sizeof(uint32);
     }
-    else
+    else {
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
         ctx->code_compiled_size += sizeof(uint32);
+    }
 }
 
 static void
 wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
 {
     if (ctx->p_code_compiled) {
-        *(int16*)(ctx->p_code_compiled) = value;
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
+#endif
+        STORE_U16(ctx->p_code_compiled, (uint16)value);
         ctx->p_code_compiled += sizeof(int16);
     }
-    else
+    else {
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
         ctx->code_compiled_size += sizeof(int16);
+    }
 }
 
 static void
@@ -4385,20 +4404,36 @@ wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
     if (ctx->p_code_compiled) {
         *(ctx->p_code_compiled) = value;
         ctx->p_code_compiled += sizeof(uint8);
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        ctx->p_code_compiled++;
+        bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
+#endif
     }
-    else
+    else {
         ctx->code_compiled_size += sizeof(uint8);
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        ctx->code_compiled_size++;
+        bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
+    }
 }
 
 static void
 wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
 {
     if (ctx->p_code_compiled) {
-        *(uint8**)(ctx->p_code_compiled) = value;
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
+#endif
+        STORE_PTR(ctx->p_code_compiled, value);
         ctx->p_code_compiled += sizeof(void *);
     }
-    else
+    else {
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
         ctx->code_compiled_size += sizeof(void *);
+    }
 }
 
 static void
@@ -4406,9 +4441,22 @@ wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
 {
     if (ctx->p_code_compiled) {
         ctx->p_code_compiled -= size;
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        if (size == sizeof(uint8)) {
+            ctx->p_code_compiled--;
+            bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
+        }
+#endif
     }
-    else
+    else {
         ctx->code_compiled_size -= size;
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        if (size == sizeof(uint8)) {
+            ctx->code_compiled_size--;
+            bh_assert((ctx->code_compiled_size & 1) == 0);
+        }
+#endif
+    }
 }
 
 static bool
@@ -4458,11 +4506,12 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
     }
 
     return true;
-
-#if WASM_ENABLE_ABS_LABEL_ADDR == 0
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
 fail:
     return false;
 #endif
+#endif
 }
 
 static bool
@@ -4534,7 +4583,7 @@ apply_label_patch(WASMLoaderContext *ctx, uint8 depth,
     while (node) {
         node_next = node->next;
         if (node->patch_type == patch_type) {
-            *((uint8**)node->code_compiled) = ctx->p_code_compiled;
+            STORE_PTR(node->code_compiled, ctx->p_code_compiled);
             if (node_prev == NULL) {
                 frame_csp->patch_list = node_next;
             }
@@ -4588,12 +4637,12 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
         emit_uint32(ctx, wasm_get_cell_num(types, arity));
         /* Part c */
         for (i = (int32)arity - 1; i >= 0; i--) {
-            cell = wasm_value_type_cell_num(types[i]);
+            cell = (uint8)wasm_value_type_cell_num(types[i]);
             emit_byte(ctx, cell);
         }
         /* Part d */
         for (i = (int32)arity - 1; i >= 0; i--) {
-            cell = wasm_value_type_cell_num(types[i]);
+            cell = (uint8)wasm_value_type_cell_num(types[i]);
             frame_offset -= cell;
             emit_operand(ctx, *(int16*)(frame_offset));
         }
@@ -4601,7 +4650,7 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
         dynamic_offset = frame_csp->dynamic_offset
                          + wasm_get_cell_num(types, arity);
         for (i = (int32)arity - 1; i >= 0; i--) {
-            cell = wasm_value_type_cell_num(types[i]);
+            cell = (uint8)wasm_value_type_cell_num(types[i]);
             dynamic_offset -= cell;
             emit_operand(ctx, dynamic_offset);
         }
@@ -5056,7 +5105,7 @@ reserve_block_ret(WASMLoaderContext *loader_ctx,
     /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
      * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
     if (return_count == 1) {
-        uint8 cell = wasm_value_type_cell_num(return_types[0]);
+        uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
         if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
             /* insert op_copy before else opcode */
             if (opcode == WASM_OP_ELSE)
@@ -5095,7 +5144,7 @@ reserve_block_ret(WASMLoaderContext *loader_ctx,
 
     /* First traversal to get the count of values needed to be copied. */
     for (i = (int32)return_count - 1; i >= 0; i--) {
-        uint8 cells = wasm_value_type_cell_num(return_types[i]);
+        uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
 
         frame_offset -= cells;
         dynamic_offset -= cells;
@@ -5135,7 +5184,7 @@ reserve_block_ret(WASMLoaderContext *loader_ctx,
         frame_offset = frame_offset_org;
         dynamic_offset = dynamic_offset_org;
         for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
-            uint8 cell = wasm_value_type_cell_num(return_types[i]);
+            uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
             frame_offset -= cell;
             dynamic_offset -= cell;
             if (dynamic_offset != *frame_offset) {
@@ -5614,7 +5663,7 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
 
     /* Get each param's cell num and src offset */
     for (i = 0; i < param_count; i++) {
-        cell = wasm_value_type_cell_num(wasm_type->types[i]);
+        cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
         cells[i] = cell;
         src_offsets[i] = *frame_offset;
         frame_offset += cell;
@@ -5753,6 +5802,7 @@ re_scan:
         }
         p = func->code;
         func->code_compiled = loader_ctx->p_code_compiled;
+        func->code_compiled_size = loader_ctx->code_compiled_size;
     }
 #endif
 
@@ -5901,9 +5951,9 @@ handle_op_block_and_loop:
                                     loader_malloc(size, error_buf, error_buf_size)))
                             goto fail;
                         bh_memcpy_s(block->param_frame_offsets,
-                                    size,
+                                    (uint32)size,
                                     loader_ctx->frame_offset - size/sizeof(int16),
-                                    size);
+                                    (uint32)size);
                     }
 
                     emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
@@ -6383,15 +6433,34 @@ handle_op_block_and_loop:
 #endif
 #if WASM_ENABLE_FAST_INTERP != 0
                             if (loader_ctx->p_code_compiled) {
-#if WASM_ENABLE_ABS_LABEL_ADDR != 0
-                                *(void**)(loader_ctx->p_code_compiled - 2 - sizeof(void*)) =
-                                    handle_table[WASM_OP_SELECT_64];
+                                uint8 opcode_tmp = WASM_OP_SELECT_64;
+                                uint8 *p_code_compiled_tmp =
+                                    loader_ctx->p_code_compiled - 2;
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+                                *(void**)(p_code_compiled_tmp - sizeof(void*)) =
+                                                        handle_table[opcode_tmp];
 #else
-                                *((int16*)loader_ctx->p_code_compiled - 2) = (int16)
-                                    (handle_table[WASM_OP_SELECT_64] - handle_table[0]);
-#endif
+                                int32 offset = (int32)
+                                               ((uint8*)handle_table[opcode_tmp]
+                                                - (uint8*)handle_table[0]);
+                                if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
+                                    set_error_buf(error_buf, error_buf_size,
+                                        "pre-compiled label offset out of range");
+                                    goto fail;
+                                }
+                                *(int16*)(p_code_compiled_tmp - sizeof(int16)) =
+                                                                    (int16)offset;
+#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+#else /* else of WASM_ENABLE_LABELS_AS_VALUES */
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+                                *(p_code_compiled_tmp - 1) = opcode_tmp;
+#else
+                                *(p_code_compiled_tmp - 2) = opcode_tmp;
+#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
                             }
-#endif
+#endif /* end of WASM_ENABLE_FAST_INTERP */
                             break;
                         default:
                             bh_assert(0);
@@ -6466,13 +6535,13 @@ handle_op_block_and_loop:
                     skip_label();
                     if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
                         if (loader_ctx->p_code_compiled)
-                            *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
+                            STORE_U16(loader_ctx->p_code_compiled - 2, local_offset);
                         loader_ctx->frame_offset --;
                         loader_ctx->dynamic_offset --;
                     }
                     else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
                         if (loader_ctx->p_code_compiled)
-                            *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
+                            STORE_U16(loader_ctx->p_code_compiled - 2, local_offset);
                         loader_ctx->frame_offset -= 2;
                         loader_ctx->dynamic_offset -= 2;
                     }
@@ -6480,11 +6549,11 @@ handle_op_block_and_loop:
                         if (local_type == VALUE_TYPE_I32
                             || local_type == VALUE_TYPE_F32) {
                             emit_label(EXT_OP_SET_LOCAL_FAST);
-                            emit_byte(loader_ctx, local_offset);
+                            emit_byte(loader_ctx, (uint8)local_offset);
                         }
                         else {
                             emit_label(EXT_OP_SET_LOCAL_FAST_I64);
-                            emit_byte(loader_ctx, local_offset);
+                            emit_byte(loader_ctx, (uint8)local_offset);
                         }
                         POP_OFFSET_TYPE(local_type);
                     }
@@ -6538,11 +6607,11 @@ handle_op_block_and_loop:
                     if (local_type == VALUE_TYPE_I32
                         || local_type == VALUE_TYPE_F32) {
                         emit_label(EXT_OP_TEE_LOCAL_FAST);
-                        emit_byte(loader_ctx, local_offset);
+                        emit_byte(loader_ctx, (uint8)local_offset);
                     }
                     else {
                         emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
-                        emit_byte(loader_ctx, local_offset);
+                        emit_byte(loader_ctx, (uint8)local_offset);
                     }
                 }
                 else {  /* local index larger than 255, reserve leb */
@@ -7737,12 +7806,12 @@ fail_data_cnt_sec_require:
         Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const));
         if (c->value_type == VALUE_TYPE_F64
             || c->value_type == VALUE_TYPE_I64) {
-            bh_memcpy_s(func_const, func_const_end - func_const,
-                        &(c->value.f64), sizeof(int64));
+            bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
+                        &(c->value.f64), (uint32)sizeof(int64));
             func_const += sizeof(int64);
         } else {
-            bh_memcpy_s(func_const, func_const_end - func_const,
-                        &(c->value.f32), sizeof(int32));
+            bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
+                        &(c->value.f32), (uint32)sizeof(int32));
             func_const += sizeof(int32);
         }
     }

+ 124 - 50
core/iwasm/interpreter/wasm_mini_loader.c

@@ -1441,7 +1441,7 @@ static bool
 wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
                              char *error_buf, uint32 error_buf_size);
 
-#if WASM_ENABLE_FAST_INTERP != 0
+#if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
 void **
 wasm_interp_get_handle_table();
 
@@ -1732,7 +1732,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
         }
     }
 
-#if WASM_ENABLE_FAST_INTERP != 0
+#if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
     handle_table = wasm_interp_get_handle_table();
 #endif
 
@@ -2639,7 +2639,7 @@ static bool
 check_offset_push(WASMLoaderContext *ctx,
                   char *error_buf, uint32 error_buf_size)
 {
-    uint32 cell_num = (ctx->frame_offset - ctx->frame_offset_bottom);
+    uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
     if (ctx->frame_offset >= ctx->frame_offset_boundary) {
         MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
                     ctx->frame_offset_size + 16);
@@ -2931,34 +2931,43 @@ wasm_loader_pop_frame_csp(WASMLoaderContext *ctx,
 
 #if WASM_ENABLE_FAST_INTERP != 0
 
-#if WASM_ENABLE_ABS_LABEL_ADDR != 0
-
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
 #define emit_label(opcode) do {                                     \
     wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]);         \
     LOG_OP("\nemit_op [%02x]\t", opcode);                           \
   } while (0)
-
 #define skip_label() do {                                           \
     wasm_loader_emit_backspace(loader_ctx, sizeof(void *));         \
     LOG_OP("\ndelete last op\n");                                   \
   } while (0)
-
-#else
-
+#else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
 #define emit_label(opcode) do {                                     \
-    int32 offset = (int32)(handle_table[opcode] - handle_table[0]); \
-    bh_assert(offset >= INT16_MIN && offset < INT16_MAX);           \
+    int32 offset = (int32)((uint8*)handle_table[opcode]             \
+                           - (uint8*)handle_table[0]);              \
+    if (!(offset >= INT16_MIN && offset < INT16_MAX)) {             \
+        set_error_buf(error_buf, error_buf_size,                    \
+                      "pre-compiled label offset out of range");    \
+        goto fail;                                                  \
+    }                                                               \
     wasm_loader_emit_int16(loader_ctx, offset);                     \
     LOG_OP("\nemit_op [%02x]\t", opcode);                           \
   } while (0)
-
-/* drop local.get / const / block / loop / end */
 #define skip_label() do {                                           \
     wasm_loader_emit_backspace(loader_ctx, sizeof(int16));          \
     LOG_OP("\ndelete last op\n");                                   \
   } while (0)
-
-#endif /* WASM_ENABLE_ABS_LABEL_ADDR */
+#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+#else /* else of WASM_ENABLE_LABELS_AS_VALUES */
+#define emit_label(opcode) do {                                     \
+    wasm_loader_emit_uint8(loader_ctx, opcode);                     \
+    LOG_OP("\nemit_op [%02x]\t", opcode);                           \
+  } while (0)
+#define skip_label() do {                                           \
+    wasm_loader_emit_backspace(loader_ctx, sizeof(uint8));          \
+    LOG_OP("\ndelete last op\n");                                   \
+  } while (0)
+#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
 
 #define emit_empty_label_addr_and_frame_ip(type) do {               \
     if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type,   \
@@ -3079,22 +3088,36 @@ static void
 wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
 {
     if (ctx->p_code_compiled) {
-        *(uint32*)(ctx->p_code_compiled) = value;
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
+#endif
+        STORE_U32(ctx->p_code_compiled, value);
         ctx->p_code_compiled += sizeof(uint32);
     }
-    else
+    else {
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
         ctx->code_compiled_size += sizeof(uint32);
+    }
 }
 
 static void
 wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
 {
     if (ctx->p_code_compiled) {
-        *(int16*)(ctx->p_code_compiled) = value;
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
+#endif
+        STORE_U16(ctx->p_code_compiled, (uint16)value);
         ctx->p_code_compiled += sizeof(int16);
     }
-    else
+    else {
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
         ctx->code_compiled_size += sizeof(int16);
+    }
 }
 
 static void
@@ -3103,20 +3126,36 @@ wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
     if (ctx->p_code_compiled) {
         *(ctx->p_code_compiled) = value;
         ctx->p_code_compiled += sizeof(uint8);
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        ctx->p_code_compiled++;
+        bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
+#endif
     }
-    else
+    else {
         ctx->code_compiled_size += sizeof(uint8);
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        ctx->code_compiled_size++;
+        bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
+    }
 }
 
 static void
 wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
 {
     if (ctx->p_code_compiled) {
-        *(uint8**)(ctx->p_code_compiled) = value;
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
+#endif
+        STORE_PTR(ctx->p_code_compiled, value);
         ctx->p_code_compiled += sizeof(void *);
     }
-    else
+    else {
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
         ctx->code_compiled_size += sizeof(void *);
+    }
 }
 
 static void
@@ -3124,9 +3163,22 @@ wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
 {
     if (ctx->p_code_compiled) {
         ctx->p_code_compiled -= size;
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        if (size == sizeof(uint8)) {
+            ctx->p_code_compiled--;
+            bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
+        }
+#endif
     }
-    else
+    else {
         ctx->code_compiled_size -= size;
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+        if (size == sizeof(uint8)) {
+            ctx->code_compiled_size--;
+            bh_assert((ctx->code_compiled_size & 1) == 0);
+        }
+#endif
+    }
 }
 
 static bool
@@ -3177,10 +3229,12 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
 
     return true;
 
-#if WASM_ENABLE_ABS_LABEL_ADDR == 0
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
 fail:
     return false;
 #endif
+#endif
 }
 
 static bool
@@ -3252,7 +3306,7 @@ apply_label_patch(WASMLoaderContext *ctx, uint8 depth,
     while (node) {
         node_next = node->next;
         if (node->patch_type == patch_type) {
-            *((uint8**)node->code_compiled) = ctx->p_code_compiled;
+            STORE_PTR(node->code_compiled, ctx->p_code_compiled);
             if (node_prev == NULL) {
                 frame_csp->patch_list = node_next;
             }
@@ -3307,12 +3361,12 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
 
         /* Part c */
         for (i = (int32)arity - 1; i >= 0; i--) {
-            cell = wasm_value_type_cell_num(types[i]);
+            cell = (uint8)wasm_value_type_cell_num(types[i]);
             emit_byte(ctx, cell);
         }
         /* Part d */
         for (i = (int32)arity - 1; i >= 0; i--) {
-            cell = wasm_value_type_cell_num(types[i]);
+            cell = (uint8)wasm_value_type_cell_num(types[i]);
             frame_offset -= cell;
             emit_operand(ctx, *(int16*)(frame_offset));
         }
@@ -3320,7 +3374,7 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
         dynamic_offset = frame_csp->dynamic_offset
                          + wasm_get_cell_num(types, arity);
         for (i = (int32)arity - 1; i >= 0; i--) {
-            cell = wasm_value_type_cell_num(types[i]);
+            cell = (uint8)wasm_value_type_cell_num(types[i]);
             dynamic_offset -= cell;
             emit_operand(ctx, dynamic_offset);
         }
@@ -3750,7 +3804,7 @@ reserve_block_ret(WASMLoaderContext *loader_ctx,
     /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
      * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
     if (return_count == 1) {
-        uint8 cell = wasm_value_type_cell_num(return_types[0]);
+        uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
         if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
             /* insert op_copy before else opcode */
             if (opcode == WASM_OP_ELSE)
@@ -3789,7 +3843,7 @@ reserve_block_ret(WASMLoaderContext *loader_ctx,
 
     /* First traversal to get the count of values needed to be copied. */
     for (i = (int32)return_count - 1; i >= 0; i--) {
-        uint8 cells = wasm_value_type_cell_num(return_types[i]);
+        uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
 
         frame_offset -= cells;
         dynamic_offset -= cells;
@@ -3829,7 +3883,7 @@ reserve_block_ret(WASMLoaderContext *loader_ctx,
         frame_offset = frame_offset_org;
         dynamic_offset = dynamic_offset_org;
         for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
-            uint8 cell = wasm_value_type_cell_num(return_types[i]);
+            uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
             frame_offset -= cell;
             dynamic_offset -= cell;
             if (dynamic_offset != *frame_offset) {
@@ -4152,7 +4206,7 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
 
     /* Get each param's cell num and src offset */
     for (i = 0; i < param_count; i++) {
-        cell = wasm_value_type_cell_num(wasm_type->types[i]);
+        cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
         cells[i] = cell;
         src_offsets[i] = *frame_offset;
         frame_offset += cell;
@@ -4291,6 +4345,7 @@ re_scan:
         }
         p = func->code;
         func->code_compiled = loader_ctx->p_code_compiled;
+        func->code_compiled_size = loader_ctx->code_compiled_size;
     }
 #endif
 
@@ -4435,9 +4490,9 @@ handle_op_block_and_loop:
                                     loader_malloc(size, error_buf, error_buf_size)))
                             goto fail;
                         bh_memcpy_s(block->param_frame_offsets,
-                                    size,
+                                    (uint32)size,
                                     loader_ctx->frame_offset - size/sizeof(int16),
-                                    size);
+                                    (uint32)size);
                     }
 
                     emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
@@ -4827,13 +4882,32 @@ handle_op_block_and_loop:
 #endif
 #if WASM_ENABLE_FAST_INTERP != 0
                             if (loader_ctx->p_code_compiled) {
-#if WASM_ENABLE_ABS_LABEL_ADDR != 0
-                                *(void**)(loader_ctx->p_code_compiled - 2 - sizeof(void*)) =
-                                    handle_table[WASM_OP_SELECT_64];
+                                uint8 opcode_tmp = WASM_OP_SELECT_64;
+                                uint8 *p_code_compiled_tmp =
+                                    loader_ctx->p_code_compiled - 2;
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+                                *(void**)(p_code_compiled_tmp - sizeof(void*)) =
+                                                        handle_table[opcode_tmp];
 #else
-                                *((int16*)loader_ctx->p_code_compiled - 2) = (int16)
-                                    (handle_table[WASM_OP_SELECT_64] - handle_table[0]);
-#endif
+                                int32 offset = (int32)
+                                               ((uint8*)handle_table[opcode_tmp]
+                                                - (uint8*)handle_table[0]);
+                                if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
+                                    set_error_buf(error_buf, error_buf_size,
+                                        "pre-compiled label offset out of range");
+                                    goto fail;
+                                }
+                                *(int16*)(p_code_compiled_tmp - sizeof(int16)) =
+                                                                    (int16)offset;
+#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+#else /* else of WASM_ENABLE_LABELS_AS_VALUES */
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+                                *(p_code_compiled_tmp - 1) = opcode_tmp;
+#else
+                                *(p_code_compiled_tmp - 2) = opcode_tmp;
+#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
                             }
 #endif
                             break;
@@ -4907,13 +4981,13 @@ handle_op_block_and_loop:
                     skip_label();
                     if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
                         if (loader_ctx->p_code_compiled)
-                            *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
+                            STORE_U16(loader_ctx->p_code_compiled - 2, local_offset);
                         loader_ctx->frame_offset --;
                         loader_ctx->dynamic_offset --;
                     }
                     else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
                         if (loader_ctx->p_code_compiled)
-                            *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
+                            STORE_U16(loader_ctx->p_code_compiled - 2, local_offset);
                         loader_ctx->frame_offset -= 2;
                         loader_ctx->dynamic_offset -= 2;
                     }
@@ -4921,11 +4995,11 @@ handle_op_block_and_loop:
                         if (local_type == VALUE_TYPE_I32
                             || local_type == VALUE_TYPE_F32) {
                             emit_label(EXT_OP_SET_LOCAL_FAST);
-                            emit_byte(loader_ctx, local_offset);
+                            emit_byte(loader_ctx, (uint8)local_offset);
                         }
                         else {
                             emit_label(EXT_OP_SET_LOCAL_FAST_I64);
-                            emit_byte(loader_ctx, local_offset);
+                            emit_byte(loader_ctx, (uint8)local_offset);
                         }
                         POP_OFFSET_TYPE(local_type);
                     }
@@ -4979,11 +5053,11 @@ handle_op_block_and_loop:
                     if (local_type == VALUE_TYPE_I32
                         || local_type == VALUE_TYPE_F32) {
                         emit_label(EXT_OP_TEE_LOCAL_FAST);
-                        emit_byte(loader_ctx, local_offset);
+                        emit_byte(loader_ctx, (uint8)local_offset);
                     }
                     else {
                         emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
-                        emit_byte(loader_ctx, local_offset);
+                        emit_byte(loader_ctx, (uint8)local_offset);
                     }
                 }
                 else {  /* local index larger than 255, reserve leb */
@@ -5738,12 +5812,12 @@ handle_op_block_and_loop:
         Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const));
         if (c->value_type == VALUE_TYPE_F64
             || c->value_type == VALUE_TYPE_I64) {
-            bh_memcpy_s(func_const, func_const_end - func_const,
-                        &(c->value.f64), sizeof(int64));
+            bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
+                        &(c->value.f64), (uint32)sizeof(int64));
             func_const += sizeof(int64);
         } else {
-            bh_memcpy_s(func_const, func_const_end - func_const,
-                        &(c->value.f32), sizeof(int32));
+            bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
+                        &(c->value.f32), (uint32)sizeof(int32));
             func_const += sizeof(int32);
         }
     }

+ 2 - 2
product-mini/platforms/android/CMakeLists.txt

@@ -95,9 +95,9 @@ endif ()
 
 add_library (iwasm SHARED ${WAMR_RUNTIME_LIB_SOURCE})
 if (CMAKE_BUILD_TYPE STREQUAL Release)
-target_link_libraries (iwasm ${LLVM_AVAILABLE_LIBS} -lm -ldl -landroid -llog -s)
+target_link_libraries (iwasm ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -landroid -llog -s)
 else()
-target_link_libraries (iwasm ${LLVM_AVAILABLE_LIBS} -lm -ldl -landroid -llog)
+target_link_libraries (iwasm ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -landroid -llog)
 endif()
 
 set (distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build/distribution)

+ 2 - 2
product-mini/platforms/darwin/CMakeLists.txt

@@ -98,7 +98,7 @@ add_executable (iwasm main.c ${UNCOMMON_SHARED_SOURCE})
 
 install (TARGETS iwasm DESTINATION bin)
 
-target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS} -lm -ldl -lpthread)
+target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
 
 add_library (libiwasm SHARED ${WAMR_RUNTIME_LIB_SOURCE})
 
@@ -106,5 +106,5 @@ install (TARGETS libiwasm DESTINATION lib)
 
 set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm)
 
-target_link_libraries (libiwasm ${LLVM_AVAILABLE_LIBS} -lm -ldl -lpthread)
+target_link_libraries (libiwasm ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
 

+ 1 - 1
product-mini/platforms/linux/CMakeLists.txt

@@ -127,4 +127,4 @@ install (TARGETS libiwasm DESTINATION lib)
 
 set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm)
 
-target_link_libraries (libiwasm ${LLVM_AVAILABLE_LIBS} -lm -ldl -lpthread)
+target_link_libraries (libiwasm ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)

+ 2 - 2
product-mini/platforms/windows/CMakeLists.txt

@@ -58,7 +58,7 @@ endif ()
 
 if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
   # Enable fast interpreter
-  set (WAMR_BUILD_FAST_INTERP 0)
+  set (WAMR_BUILD_FAST_INTERP 1)
 endif ()
 
 if (NOT DEFINED WAMR_BUILD_MULTI_MODULE)
@@ -91,7 +91,7 @@ include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
 add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
 
 set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
-set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
+set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO")
 
 # set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
 # set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion")

Некоторые файлы не были показаны из-за большого количества измененных файлов