Apply clang-format for more src files and update spec test script (#775)

Apply clang-format for core/iwasm/include, core/iwasm/common and
core/iwasm/aot files.

Update spec cases test script:
- Checkout latest commit of https://github.com/WebAssembly/spec
- Checkout main branch but not master of https://github.com/WebAssembly/threads
- Update wabt to latest version

And update source debugging document.

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang
2021-10-08 17:47:11 +08:00
committed by GitHub
parent 6415e1b006
commit 52b6c73d9c
37 changed files with 2835 additions and 2875 deletions

View File

@ -6,11 +6,12 @@
#include "../wasm_runtime_common.h"
#include "../wasm_exec_env.h"
void invokeNative(void (*native_code)(), uint32 argv[], uint32 argc)
void
invokeNative(void (*native_code)(), uint32 argv[], uint32 argc)
{
bh_assert(argc >= sizeof(WASMExecEnv*)/sizeof(uint32));
bh_assert(argc >= sizeof(WASMExecEnv *) / sizeof(uint32));
switch(argc) {
switch (argc) {
case 0:
native_code();
break;
@ -33,53 +34,80 @@ void invokeNative(void (*native_code)(), uint32 argv[], uint32 argc)
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
break;
case 7:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6]);
break;
case 8:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7]);
break;
case 9:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8]);
break;
case 10:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9]);
break;
case 11:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9], argv[10]);
break;
case 12:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]);
break;
case 13:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9], argv[10], argv[11],
argv[12]);
break;
case 14:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9], argv[10], argv[11],
argv[12], argv[13]);
break;
case 15:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9], argv[10], argv[11],
argv[12], argv[13], argv[14]);
break;
case 16:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9], argv[10], argv[11],
argv[12], argv[13], argv[14], argv[15]);
break;
case 17:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9], argv[10], argv[11],
argv[12], argv[13], argv[14], argv[15], argv[16]);
break;
case 18:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9], argv[10], argv[11],
argv[12], argv[13], argv[14], argv[15], argv[16],
argv[17]);
break;
case 19:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9], argv[10], argv[11],
argv[12], argv[13], argv[14], argv[15], argv[16],
argv[17], argv[18]);
break;
case 20:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19]);
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9], argv[10], argv[11],
argv[12], argv[13], argv[14], argv[15], argv[16],
argv[17], argv[18], argv[19]);
break;
default:
{
/* FIXME: If this happen, add more cases. */
WASMExecEnv *exec_env = *(WASMExecEnv**)argv;
WASMExecEnv *exec_env = *(WASMExecEnv **)argv;
WASMModuleInstanceCommon *module_inst = exec_env->module_inst;
wasm_runtime_set_exception(module_inst, "the argument number of native function exceeds maximum");
wasm_runtime_set_exception(
module_inst,
"the argument number of native function exceeds maximum");
return;
}
}

View File

@ -24,15 +24,12 @@ runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
{
void *mem;
if (size >= UINT32_MAX
|| !(mem = wasm_runtime_malloc((uint32)size))) {
if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
if (module_inst != NULL) {
wasm_runtime_set_exception(module_inst,
"allocate memory failed");
wasm_runtime_set_exception(module_inst, "allocate memory failed");
}
else if (error_buf != NULL) {
set_error_buf(error_buf, error_buf_size,
"allocate memory failed");
set_error_buf(error_buf, error_buf_size, "allocate memory failed");
}
return NULL;
}
@ -52,29 +49,31 @@ static union {
* Implementation of wasm_application_execute_main()
*/
static WASMFunctionInstanceCommon*
resolve_function(const WASMModuleInstanceCommon *module_inst,
const char *name);
static WASMFunctionInstanceCommon *
resolve_function(const WASMModuleInstanceCommon *module_inst, const char *name);
static bool
check_main_func_type(const WASMType *type)
{
if (!(type->param_count == 0 || type->param_count == 2)
||type->result_count > 1) {
LOG_ERROR("WASM execute application failed: invalid main function type.\n");
|| type->result_count > 1) {
LOG_ERROR(
"WASM execute application failed: invalid main function type.\n");
return false;
}
if (type->param_count == 2
&& !(type->types[0] == VALUE_TYPE_I32
&& type->types[1] == VALUE_TYPE_I32)) {
LOG_ERROR("WASM execute application failed: invalid main function type.\n");
&& type->types[1] == VALUE_TYPE_I32)) {
LOG_ERROR(
"WASM execute application failed: invalid main function type.\n");
return false;
}
if (type->result_count
&& type->types[type->param_count] != VALUE_TYPE_I32) {
LOG_ERROR("WASM execute application failed: invalid main function type.\n");
LOG_ERROR(
"WASM execute application failed: invalid main function type.\n");
return false;
}
@ -82,8 +81,8 @@ check_main_func_type(const WASMType *type)
}
bool
wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
int32 argc, char *argv[])
wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
char *argv[])
{
WASMFunctionInstanceCommon *func;
WASMType *func_type = NULL;
@ -103,9 +102,9 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
the actual main function. Directly call main function
may cause exception thrown. */
if ((func = wasm_runtime_lookup_wasi_start_function(module_inst)))
return wasm_runtime_create_exec_env_and_call_wasm(
module_inst, func, 0, NULL);
/* if no start function is found, we execute
return wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
0, NULL);
/* If no start function was found, we execute
the main function as normal */
}
#endif /* end of WASM_ENABLE_LIBC_WASI */
@ -113,25 +112,23 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
if (!(func = resolve_function(module_inst, "main"))
&& !(func = resolve_function(module_inst, "__main_argc_argv"))
&& !(func = resolve_function(module_inst, "_main"))) {
wasm_runtime_set_exception(module_inst,
"lookup main function failed");
wasm_runtime_set_exception(module_inst, "lookup main function failed");
return false;
}
#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode) {
is_import_func = ((WASMFunctionInstance*)func)->is_import_func;
is_import_func = ((WASMFunctionInstance *)func)->is_import_func;
}
#endif
#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT) {
is_import_func = ((AOTFunctionInstance*)func)->is_import_func;
is_import_func = ((AOTFunctionInstance *)func)->is_import_func;
}
#endif
if (is_import_func) {
wasm_runtime_set_exception(module_inst,
"lookup main function failed");
wasm_runtime_set_exception(module_inst, "lookup main function failed");
return false;
}
@ -157,34 +154,34 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
if (total_size >= UINT32_MAX
|| !(argv_buf_offset =
wasm_runtime_module_malloc(module_inst, (uint32)total_size,
(void**)&argv_buf))) {
wasm_runtime_set_exception(module_inst,
"allocate memory failed");
|| !(argv_buf_offset = wasm_runtime_module_malloc(
module_inst, (uint32)total_size, (void **)&argv_buf))) {
wasm_runtime_set_exception(module_inst, "allocate memory failed");
return false;
}
p = argv_buf;
argv_offsets = (uint32*)(p + total_argv_size);
argv_offsets = (uint32 *)(p + total_argv_size);
p_end = p + total_size;
for (i = 0; i < argc; i++) {
bh_memcpy_s(p, (uint32)(p_end - p), argv[i], (uint32)(strlen(argv[i]) + 1));
bh_memcpy_s(p, (uint32)(p_end - p), argv[i],
(uint32)(strlen(argv[i]) + 1));
argv_offsets[i] = argv_buf_offset + (uint32)(p - argv_buf);
p += strlen(argv[i]) + 1;
}
argc1 = 2;
argv1[0] = (uint32)argc;
argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
argv1[1] =
(uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
}
ret = wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
argc1, argv1);
ret = wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, argc1,
argv1);
if (ret && func_type->result_count > 0 && argc > 0 && argv)
/* copy the return value */
*(int*)argv = (int)argv1[0];
*(int *)argv = (int)argv1[0];
if (argv_buf_offset)
wasm_runtime_module_free(module_inst, argv_buf_offset);
@ -197,7 +194,7 @@ get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
const char *sub_module_name)
{
WASMSubModInstNode *node =
bh_list_first_elem(parent_module_inst->sub_module_inst_list);
bh_list_first_elem(parent_module_inst->sub_module_inst_list);
while (node && strcmp(node->module_name, sub_module_name)) {
node = bh_list_elem_next(node);
@ -241,9 +238,8 @@ parse_function_name(char *orig_function_name, char **p_module_name,
* Implementation of wasm_application_execute_func()
*/
static WASMFunctionInstanceCommon*
resolve_function(const WASMModuleInstanceCommon *module_inst,
const char *name)
static WASMFunctionInstanceCommon *
resolve_function(const WASMModuleInstanceCommon *module_inst, const char *name)
{
uint32 i = 0;
WASMFunctionInstanceCommon *ret = NULL;
@ -268,8 +264,8 @@ resolve_function(const WASMModuleInstanceCommon *module_inst,
LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);
if (sub_module_name) {
sub_module_inst = get_sub_module_inst(
(WASMModuleInstance *)module_inst, sub_module_name);
sub_module_inst = get_sub_module_inst((WASMModuleInstance *)module_inst,
sub_module_name);
if (!sub_module_inst) {
LOG_DEBUG("can not find a sub module named %s", sub_module_name);
goto LEAVE;
@ -281,26 +277,26 @@ resolve_function(const WASMModuleInstanceCommon *module_inst,
#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode) {
WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst;
WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;
#if WASM_ENABLE_MULTI_MODULE != 0
wasm_inst = sub_module_inst ? sub_module_inst : wasm_inst;
#endif /* WASM_ENABLE_MULTI_MODULE */
for (i = 0; i < wasm_inst->export_func_count; i++) {
if (!strcmp(wasm_inst->export_functions[i].name, function_name)) {
if (!strcmp(wasm_inst->export_functions[i].name, function_name)) {
ret = wasm_inst->export_functions[i].function;
break;
}
}
}
}
#endif /* WASM_ENABLE_INTERP */
#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT) {
AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst;
AOTFunctionInstance *export_funcs = (AOTFunctionInstance *)
aot_inst->export_funcs.ptr;
AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
AOTFunctionInstance *export_funcs =
(AOTFunctionInstance *)aot_inst->export_funcs.ptr;
for (i = 0; i < aot_inst->export_func_count; i++) {
if (!strcmp(export_funcs[i].func_name, function_name)) {
ret = &export_funcs[i];
@ -323,14 +319,14 @@ union ieee754_float {
/* This is the IEEE 754 single-precision format. */
union {
struct {
unsigned int negative:1;
unsigned int exponent:8;
unsigned int mantissa:23;
unsigned int negative : 1;
unsigned int exponent : 8;
unsigned int mantissa : 23;
} ieee_big_endian;
struct {
unsigned int mantissa:23;
unsigned int exponent:8;
unsigned int negative:1;
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int negative : 1;
} ieee_little_endian;
} ieee;
};
@ -341,19 +337,19 @@ union ieee754_double {
/* This is the IEEE 754 double-precision format. */
union {
struct {
unsigned int negative:1;
unsigned int exponent:11;
unsigned int negative : 1;
unsigned int exponent : 11;
/* Together these comprise the mantissa. */
unsigned int mantissa0:20;
unsigned int mantissa1:32;
unsigned int mantissa0 : 20;
unsigned int mantissa1 : 32;
} ieee_big_endian;
struct {
/* Together these comprise the mantissa. */
unsigned int mantissa1:32;
unsigned int mantissa0:20;
unsigned int exponent:11;
unsigned int negative:1;
unsigned int mantissa1 : 32;
unsigned int mantissa0 : 20;
unsigned int exponent : 11;
unsigned int negative : 1;
} ieee_little_endian;
} ieee;
};
@ -382,7 +378,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode) {
WASMFunctionInstance *wasm_func = (WASMFunctionInstance*)func;
WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)func;
if (wasm_func->is_import_func
#if WASM_ENABLE_MULTI_MODULE != 0
&& !wasm_func->import_func_inst
@ -404,8 +400,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
}
if (type->param_count != (uint32)argc) {
wasm_runtime_set_exception(module_inst,
"invalid input argument count");
wasm_runtime_set_exception(module_inst, "invalid input argument count");
goto fail;
}
@ -413,8 +408,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
cell_num = (argc1 > type->ret_cell_num) ? argc1 : type->ret_cell_num;
total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst,
NULL, 0)))) {
if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) {
goto fail;
}
@ -433,7 +427,10 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
break;
case VALUE_TYPE_I64:
{
union { uint64 val; uint32 parts[2]; } u;
union {
uint64 val;
uint32 parts[2];
} u;
u.val = strtoull(argv[i], &endptr, 0);
argv1[p++] = u.parts[0];
argv1[p++] = u.parts[1];
@ -469,7 +466,10 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
}
case VALUE_TYPE_F64:
{
union { float64 val; uint32 parts[2]; } u;
union {
float64 val;
uint32 parts[2];
} u;
u.val = strtod(argv[i], &endptr);
if (isnan(u.val)) {
if (argv[i][0] == '-') {
@ -506,11 +506,11 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
{
/* it likes 0x123\0x234 or 123\234 */
/* retrive first i64 */
*(uint64*)(argv1 + p) = strtoull(argv[i], &endptr, 0);
*(uint64 *)(argv1 + p) = strtoull(argv[i], &endptr, 0);
/* skip \ */
endptr++;
/* retrive second i64 */
*(uint64*)(argv1 + p + 2) = strtoull(endptr, &endptr, 0);
*(uint64 *)(argv1 + p + 2) = strtoull(endptr, &endptr, 0);
p += 4;
break;
}
@ -541,7 +541,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
if (!wasm_externref_obj2ref(module_inst, extern_obj,
&externref_idx)) {
wasm_runtime_set_exception(
module_inst, "map extern object to ref failed");
module_inst, "map extern object to ref failed");
goto fail;
}
argv1[p++] = externref_idx;
@ -554,8 +554,8 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
break;
}
if (endptr && *endptr != '\0' && *endptr != '_') {
snprintf(buf, sizeof(buf), "invalid input argument %d: %s",
i, argv[i]);
snprintf(buf, sizeof(buf), "invalid input argument %d: %s", i,
argv[i]);
wasm_runtime_set_exception(module_inst, buf);
goto fail;
}
@ -563,8 +563,8 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
bh_assert(p == (int32)argc1);
wasm_runtime_set_exception(module_inst, NULL);
if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
argc1, argv1)) {
if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, argc1,
argv1)) {
goto fail;
}
@ -579,12 +579,15 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
}
case VALUE_TYPE_I64:
{
union { uint64 val; uint32 parts[2]; } u;
union {
uint64 val;
uint32 parts[2];
} u;
u.parts[0] = argv1[k];
u.parts[1] = argv1[k + 1];
k += 2;
#ifdef PRIx64
os_printf("0x%"PRIx64":i64", u.val);
os_printf("0x%" PRIx64 ":i64", u.val);
#else
char buf[16];
if (sizeof(long) == 4)
@ -597,13 +600,16 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
}
case VALUE_TYPE_F32:
{
os_printf("%.7g:f32", *(float32*)(argv1 + k));
os_printf("%.7g:f32", *(float32 *)(argv1 + k));
k++;
break;
}
case VALUE_TYPE_F64:
{
union { float64 val; uint32 parts[2]; } u;
union {
float64 val;
uint32 parts[2];
} u;
u.parts[0] = argv1[k];
u.parts[1] = argv1[k + 1];
k += 2;
@ -638,9 +644,10 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
#if WASM_ENABLE_SIMD != 0
case VALUE_TYPE_V128:
{
uint64 *v = (uint64*)(argv1 + k);
uint64 *v = (uint64 *)(argv1 + k);
#if defined(PRIx64)
os_printf("<0x%016"PRIx64" 0x%016"PRIx64">:v128", *v, *(v + 1));
os_printf("<0x%016" PRIx64 " 0x%016" PRIx64 ">:v128", *v,
*(v + 1));
#else
if (4 == sizeof(long)) {
os_printf("<0x%016llx 0x%016llx>:v128", *v, *(v + 1));

File diff suppressed because it is too large Load Diff

View File

@ -213,33 +213,27 @@ struct wasm_instance_t {
};
wasm_ref_t *
wasm_ref_new_internal(wasm_store_t *store,
enum wasm_reference_kind kind,
wasm_ref_new_internal(wasm_store_t *store, enum wasm_reference_kind kind,
uint32 obj_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_foreign_t *
wasm_foreign_new_internal(wasm_store_t *store,
uint32 foreign_idx_rt,
wasm_foreign_new_internal(wasm_store_t *store, uint32 foreign_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_func_t *
wasm_func_new_internal(wasm_store_t *store,
uint16 func_idx_rt,
wasm_func_new_internal(wasm_store_t *store, uint16 func_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_global_t *
wasm_global_new_internal(wasm_store_t *store,
uint16 global_idx_rt,
wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_memory_t *
wasm_memory_new_internal(wasm_store_t *store,
uint16 memory_idx_rt,
wasm_memory_new_internal(wasm_store_t *store, uint16 memory_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_table_t *
wasm_table_new_internal(wasm_store_t *store,
uint16 table_idx_rt,
wasm_table_new_internal(wasm_store_t *store, uint16 table_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
#endif /* _WASM_C_API_INTERNAL_H */

View File

@ -27,8 +27,8 @@ WASMExecEnv *
wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
uint32 stack_size)
{
uint64 total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
+ (uint64)stack_size;
uint64 total_size =
offsetof(WASMExecEnv, wasm_stack.s.bottom) + (uint64)stack_size;
WASMExecEnv *exec_env;
if (total_size >= UINT32_MAX
@ -122,22 +122,23 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
if (!exec_env)
return NULL;
/* Set the aux_stack_boundary and aux_stack_bottom */
#if WASM_ENABLE_INTERP != 0
/* Set the aux_stack_boundary and aux_stack_bottom */
if (module_inst->module_type == Wasm_Module_Bytecode) {
WASMModule *module = ((WASMModuleInstance *)module_inst)->module;
exec_env->aux_stack_bottom.bottom = module->aux_stack_bottom;
exec_env->aux_stack_boundary.boundary = module->aux_stack_bottom
- module->aux_stack_size;
exec_env->aux_stack_boundary.boundary =
module->aux_stack_bottom - module->aux_stack_size;
}
#endif
#if WASM_ENABLE_AOT != 0
/* Set the aux_stack_boundary and aux_stack_bottom */
if (module_inst->module_type == Wasm_Module_AoT) {
AOTModule *module =
(AOTModule *)(((AOTModuleInstance *)module_inst)->aot_module.ptr);
exec_env->aux_stack_bottom.bottom = module->aux_stack_bottom;
exec_env->aux_stack_boundary.boundary = module->aux_stack_bottom
- module->aux_stack_size;
exec_env->aux_stack_boundary.boundary =
module->aux_stack_bottom - module->aux_stack_size;
}
#endif
@ -150,8 +151,8 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_debug_instance_create(cluster);
#endif
#endif /* end of WASM_ENABLE_THREAD_MGR */
#endif
return exec_env;
}
@ -162,16 +163,15 @@ wasm_exec_env_destroy(WASMExecEnv *exec_env)
/* Terminate all sub-threads */
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
if (cluster) {
#if WASM_ENABLE_THREAD_MGR != 0
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_cluster_thread_exited(exec_env);
wasm_debug_instance_destroy(cluster);
#endif
#endif
wasm_cluster_terminate_all_except_self(cluster, exec_env);
wasm_cluster_del_exec_env(cluster, exec_env);
}
#endif
#endif /* end of WASM_ENABLE_THREAD_MGR */
wasm_exec_env_destroy_internal(exec_env);
}
@ -224,4 +224,3 @@ wasm_exec_env_pop_jmpbuf(WASMExecEnv *exec_env)
return NULL;
}
#endif

View File

@ -89,7 +89,7 @@ typedef struct WASMExecEnv {
void *thread_ret_value;
/* Must be provided by thread library */
void* (*thread_start_routine)(void *);
void *(*thread_start_routine)(void *);
void *thread_arg;
/* pointer to the cluster */
@ -192,8 +192,8 @@ wasm_exec_env_alloc_wasm_frame(WASMExecEnv *exec_env, unsigned size)
#if WASM_ENABLE_MEMORY_PROFILING != 0
{
uint32 wasm_stack_used = exec_env->wasm_stack.s.top
- exec_env->wasm_stack.s.bottom;
uint32 wasm_stack_used =
exec_env->wasm_stack.s.top - exec_env->wasm_stack.s.bottom;
if (wasm_stack_used > exec_env->max_wasm_stack_used)
exec_env->max_wasm_stack_used = wasm_stack_used;
}
@ -215,7 +215,7 @@ wasm_exec_env_free_wasm_frame(WASMExecEnv *exec_env, void *prev_top)
*
* @return the current WASM stack top pointer
*/
static inline void*
static inline void *
wasm_exec_env_wasm_stack_top(WASMExecEnv *exec_env)
{
return exec_env->wasm_stack.s.top;
@ -241,7 +241,7 @@ wasm_exec_env_set_cur_frame(WASMExecEnv *exec_env,
*
* @return the current frame pointer
*/
static inline struct WASMInterpFrame*
static inline struct WASMInterpFrame *
wasm_exec_env_get_cur_frame(WASMExecEnv *exec_env)
{
return exec_env->cur_frame;
@ -253,7 +253,6 @@ wasm_exec_env_get_module_inst(WASMExecEnv *exec_env);
void
wasm_exec_env_set_thread_info(WASMExecEnv *exec_env);
#if WASM_ENABLE_THREAD_MGR != 0
void *
wasm_exec_env_get_thread_arg(WASMExecEnv *exec_env);

View File

@ -39,8 +39,7 @@ wasm_memory_init_with_pool(void *mem, unsigned int bytes)
}
static bool
wasm_memory_init_with_allocator(void *_malloc_func,
void *_realloc_func,
wasm_memory_init_with_allocator(void *_malloc_func, void *_realloc_func,
void *_free_func)
{
if (_malloc_func && _free_func && _malloc_func != _free_func) {
@ -50,8 +49,8 @@ wasm_memory_init_with_allocator(void *_malloc_func,
free_func = _free_func;
return true;
}
LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n",
_malloc_func, _realloc_func, _free_func);
LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n", _malloc_func,
_realloc_func, _free_func);
return false;
}
@ -63,9 +62,10 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
return wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
alloc_option->pool.heap_size);
else if (mem_alloc_type == Alloc_With_Allocator)
return wasm_memory_init_with_allocator(alloc_option->allocator.malloc_func,
alloc_option->allocator.realloc_func,
alloc_option->allocator.free_func);
return wasm_memory_init_with_allocator(
alloc_option->allocator.malloc_func,
alloc_option->allocator.realloc_func,
alloc_option->allocator.free_func);
else if (mem_alloc_type == Alloc_With_System_Allocator)
return wasm_memory_init_with_allocator(os_malloc, os_realloc, os_free);
else
@ -93,7 +93,8 @@ static inline void *
wasm_runtime_malloc_internal(unsigned int size)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
LOG_WARNING("wasm_runtime_malloc failed: memory hasn't been initialize.\n");
LOG_WARNING(
"wasm_runtime_malloc failed: memory hasn't been initialize.\n");
return NULL;
}
else if (memory_mode == MEMORY_MODE_POOL) {
@ -108,7 +109,8 @@ static inline void *
wasm_runtime_realloc_internal(void *ptr, unsigned int size)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
LOG_WARNING("wasm_runtime_realloc failed: memory hasn't been initialize.\n");
LOG_WARNING(
"wasm_runtime_realloc failed: memory hasn't been initialize.\n");
return NULL;
}
else if (memory_mode == MEMORY_MODE_POOL) {

View File

@ -25,4 +25,3 @@ wasm_runtime_memory_destroy();
#endif
#endif /* end of _WASM_MEMORY_H */

View File

@ -17,7 +17,7 @@
#define ENABLE_SORT_DEBUG 0
#if ENABLE_SORT_DEBUG != 0
#include<sys/time.h>
#include <sys/time.h>
#endif
static NativeSymbolsList g_native_symbols_list = NULL;
@ -80,7 +80,7 @@ check_symbol_signature(const WASMType *type, const char *signature)
#if WASM_ENABLE_REF_TYPES != 0
|| (sig == 'i' && type->types[i] == VALUE_TYPE_EXTERNREF)
#endif
)
)
/* normal parameter */
continue;
@ -91,8 +91,7 @@ check_symbol_signature(const WASMType *type, const char *signature)
if (sig == '*') {
/* it is a pointer */
if (i + 1 < type->param_count
&& type->types[i + 1] == VALUE_TYPE_I32
&& *p == '~') {
&& type->types[i + 1] == VALUE_TYPE_I32 && *p == '~') {
/* pointer length followed */
i++;
p++;
@ -132,8 +131,8 @@ sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols)
for (i = 0; i < n_native_symbols - 1; i++) {
for (j = i + 1; j < n_native_symbols; j++) {
if (strcmp(native_symbols[i].symbol,
native_symbols[j].symbol) > 0) {
if (strcmp(native_symbols[i].symbol, native_symbols[j].symbol)
> 0) {
temp = native_symbols[i];
native_symbols[i] = native_symbols[j];
native_symbols[j] = temp;
@ -143,7 +142,7 @@ sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols)
}
#else
static void
swap_symbol(NativeSymbol* left, NativeSymbol* right)
swap_symbol(NativeSymbol *left, NativeSymbol *right)
{
NativeSymbol temp = *left;
*left = *right;
@ -151,7 +150,7 @@ swap_symbol(NativeSymbol* left, NativeSymbol* right)
}
static void
quick_sort_symbols(NativeSymbol* native_symbols, int left, int right)
quick_sort_symbols(NativeSymbol *native_symbols, int left, int right)
{
NativeSymbol base_symbol;
int pin_left = left;
@ -164,8 +163,8 @@ quick_sort_symbols(NativeSymbol* native_symbols, int left, int right)
base_symbol = native_symbols[left];
while (left < right) {
while (left < right
&& strcmp(native_symbols[right].symbol,
base_symbol.symbol) > 0) {
&& strcmp(native_symbols[right].symbol, base_symbol.symbol)
> 0) {
right--;
}
@ -175,8 +174,7 @@ quick_sort_symbols(NativeSymbol* native_symbols, int left, int right)
}
while (left < right
&& strcmp(native_symbols[left].symbol,
base_symbol.symbol) < 0) {
&& strcmp(native_symbols[left].symbol, base_symbol.symbol) < 0) {
left++;
}
@ -216,7 +214,7 @@ lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
return NULL;
}
void*
void *
wasm_native_resolve_symbol(const char *module_name, const char *field_name,
const WASMType *func_type, const char **p_signature,
void **p_attachment, bool *p_call_conv_raw)
@ -229,15 +227,14 @@ wasm_native_resolve_symbol(const char *module_name, const char *field_name,
while (node) {
node_next = node->next;
if (!strcmp(node->module_name, module_name)) {
if ((func_ptr = lookup_symbol(node->native_symbols,
node->n_native_symbols,
field_name, &signature, &attachment))
if ((func_ptr =
lookup_symbol(node->native_symbols, node->n_native_symbols,
field_name, &signature, &attachment))
|| (field_name[0] == '_'
&& (func_ptr = lookup_symbol(node->native_symbols,
node->n_native_symbols,
field_name + 1,
&signature, &attachment))))
break;
&& (func_ptr = lookup_symbol(
node->native_symbols, node->n_native_symbols,
field_name + 1, &signature, &attachment))))
break;
}
node = node_next;
}
@ -246,7 +243,8 @@ wasm_native_resolve_symbol(const char *module_name, const char *field_name,
if (signature && signature[0] != '\0') {
/* signature is not empty, check its format */
if (!check_symbol_signature(func_type, signature)) {
#if WASM_ENABLE_WAMR_COMPILER == 0 /* Output warning except running aot compiler */
#if WASM_ENABLE_WAMR_COMPILER == 0
/* Output warning except running aot compiler */
LOG_WARNING("failed to check signature '%s' and resolve "
"pointer params for import function (%s %s)\n",
signature, module_name, field_name);
@ -270,10 +268,8 @@ wasm_native_resolve_symbol(const char *module_name, const char *field_name,
}
static bool
register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols,
bool call_conv_raw)
register_natives(const char *module_name, NativeSymbol *native_symbols,
uint32 n_native_symbols, bool call_conv_raw)
{
NativeSymbolsNode *node;
#if ENABLE_SORT_DEBUG != 0
@ -309,10 +305,10 @@ register_natives(const char *module_name,
#if ENABLE_SORT_DEBUG != 0
gettimeofday(&end, NULL);
timer = 1000000 * (end.tv_sec - start.tv_sec)
+ (end.tv_usec - start.tv_usec);
LOG_ERROR("module_name: %s, nums: %d, sorted used: %ld us",
module_name, n_native_symbols, timer);
timer =
1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
LOG_ERROR("module_name: %s, nums: %d, sorted used: %ld us", module_name,
n_native_symbols, timer);
#endif
return true;
}
@ -322,7 +318,8 @@ wasm_native_register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols)
{
return register_natives(module_name, native_symbols, n_native_symbols, false);
return register_natives(module_name, native_symbols, n_native_symbols,
false);
}
bool
@ -330,7 +327,8 @@ wasm_native_register_natives_raw(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols)
{
return register_natives(module_name, native_symbols, n_native_symbols, true);
return register_natives(module_name, native_symbols, n_native_symbols,
true);
}
bool
@ -341,41 +339,40 @@ wasm_native_init()
#if WASM_ENABLE_LIBC_BUILTIN != 0
n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
if (!wasm_native_register_natives("env",
native_symbols, n_native_symbols))
if (!wasm_native_register_natives("env", native_symbols, n_native_symbols))
return false;
#endif /* WASM_ENABLE_LIBC_BUILTIN */
#if WASM_ENABLE_SPEC_TEST
n_native_symbols = get_spectest_export_apis(&native_symbols);
if (!wasm_native_register_natives("spectest",
native_symbols, n_native_symbols))
if (!wasm_native_register_natives("spectest", native_symbols,
n_native_symbols))
return false;
#endif /* WASM_ENABLE_SPEC_TEST */
#if WASM_ENABLE_LIBC_WASI != 0
n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
if (!wasm_native_register_natives("wasi_unstable",
native_symbols, n_native_symbols))
if (!wasm_native_register_natives("wasi_unstable", native_symbols,
n_native_symbols))
return false;
if (!wasm_native_register_natives("wasi_snapshot_preview1",
native_symbols, n_native_symbols))
if (!wasm_native_register_natives("wasi_snapshot_preview1", native_symbols,
n_native_symbols))
return false;
#endif
#if WASM_ENABLE_BASE_LIB != 0
n_native_symbols = get_base_lib_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env",
native_symbols, n_native_symbols))
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
#endif
#if WASM_ENABLE_APP_FRAMEWORK != 0
n_native_symbols = get_ext_lib_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env",
native_symbols, n_native_symbols))
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
#endif
@ -385,16 +382,16 @@ wasm_native_init()
n_native_symbols = get_lib_pthread_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env",
native_symbols, n_native_symbols))
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
#endif
#if WASM_ENABLE_LIBC_EMCC != 0
n_native_symbols = get_libc_emcc_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env",
native_symbols, n_native_symbols))
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
#endif /* WASM_ENABLE_LIBC_EMCC */

View File

@ -49,7 +49,7 @@ wasm_native_lookup_libc_builtin_global(const char *module_name,
*
* @return the native function pointer if success, NULL otherwise
*/
void*
void *
wasm_native_resolve_symbol(const char *module_name, const char *field_name,
const WASMType *func_type, const char **p_signature,
void **p_attachment, bool *p_call_conv_raw);
@ -75,4 +75,3 @@ wasm_native_destroy();
#endif
#endif /* end of _WASM_NATIVE_H */

File diff suppressed because it is too large Load Diff

View File

@ -27,151 +27,187 @@ extern "C" {
#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 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))
#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)
#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 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 STORE_PTR(addr, ptr) \
do { \
*(void **)addr = (void *)ptr; \
} while (0)
#else /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
#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)
#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;
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)
GET_F64_FROM_ADDR(uint32 *addr)
{
union { float64 val; uint32 parts[2]; } u;
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_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_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)
#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;
union {
int64 val;
uint32 u32[2];
uint16 u16[4];
uint8 u8[8];
} u;
if ((addr1 & (uintptr_t)7) == 0)
return *(int64*)addr;
return *(int64 *)addr;
if ((addr1 & (uintptr_t)3) == 0) {
u.u32[0] = ((uint32*)addr)[0];
u.u32[1] = ((uint32*)addr)[1];
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];
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];
u.u8[t] = ((uint8 *)addr)[t];
}
return u.val;
}
@ -180,25 +216,29 @@ 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;
union {
float64 val;
uint32 u32[2];
uint16 u16[4];
uint8 u8[8];
} u;
if ((addr1 & (uintptr_t)7) == 0)
return *(float64*)addr;
return *(float64 *)addr;
if ((addr1 & (uintptr_t)3) == 0) {
u.u32[0] = ((uint32*)addr)[0];
u.u32[1] = ((uint32*)addr)[1];
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];
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];
u.u8[t] = ((uint8 *)addr)[t];
}
return u.val;
}
@ -207,19 +247,23 @@ static inline int32
LOAD_I32(void *addr)
{
uintptr_t addr1 = (uintptr_t)addr;
union { int32 val; uint16 u16[2]; uint8 u8[4]; } u;
union {
int32 val;
uint16 u16[2];
uint8 u8[4];
} u;
if ((addr1 & (uintptr_t)3) == 0)
return *(int32*)addr;
return *(int32 *)addr;
if ((addr1 & (uintptr_t)1) == 0) {
u.u16[0] = ((uint16*)addr)[0];
u.u16[1] = ((uint16*)addr)[1];
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];
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;
}
@ -228,13 +272,16 @@ static inline int16
LOAD_I16(void *addr)
{
uintptr_t addr1 = (uintptr_t)addr;
union { int16 val; uint8 u8[2]; } u;
union {
int16 val;
uint8 u8[2];
} u;
if ((addr1 & (uintptr_t)1)) {
u.u8[0] = ((uint8*)addr)[0];
u.u8[1] = ((uint8*)addr)[1];
u.u8[0] = ((uint8 *)addr)[0];
u.u8[1] = ((uint8 *)addr)[1];
return u.val;
}
return *(int16*)addr;
return *(int16 *)addr;
}
#define LOAD_U32(addr) ((uint32)LOAD_I32(addr))
@ -246,7 +293,7 @@ LOAD_I16(void *addr)
#define STORE_PTR(addr, ptr) STORE_I64(addr, (uintptr_t)ptr)
#endif
#endif /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
#endif /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
typedef struct WASMModuleCommon {
/* Module type, for module loaded from WASM bytecode binary,
@ -361,8 +408,8 @@ get_package_type(const uint8 *buf, uint32 size);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN WASMModuleCommon *
wasm_runtime_load(const uint8 *buf, uint32 size,
char *error_buf, uint32 error_buf_size);
wasm_runtime_load(const uint8 *buf, uint32 size, char *error_buf,
uint32 error_buf_size);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN WASMModuleCommon *
@ -386,9 +433,9 @@ wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
wasm_runtime_instantiate(WASMModuleCommon *module,
uint32 stack_size, uint32 heap_size,
char *error_buf, uint32 error_buf_size);
wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
uint32 heap_size, char *error_buf,
uint32 error_buf_size);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN void
@ -396,7 +443,7 @@ wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
const char *name, const char *signature);
/* Internal API */
@ -432,8 +479,8 @@ wasm_runtime_get_user_data(WASMExecEnv *exec_env);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_call_wasm(WASMExecEnv *exec_env,
WASMFunctionInstanceCommon *function,
uint32 argc, uint32 argv[]);
WASMFunctionInstanceCommon *function, uint32 argc,
uint32 argv[]);
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
@ -465,14 +512,13 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
* the caller can call wasm_runtime_get_exception to get exception info.
*/
bool
wasm_runtime_call_indirect(WASMExecEnv *exec_env,
uint32 element_indices,
wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices,
uint32 argc, uint32 argv[]);
bool
wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst,
WASMFunctionInstanceCommon *function,
uint32 argc, uint32 argv[]);
wasm_runtime_create_exec_env_and_call_wasm(
WASMModuleInstanceCommon *module_inst, WASMFunctionInstanceCommon *function,
uint32 argc, uint32 argv[]);
bool
wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
@ -482,8 +528,8 @@ wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN bool
wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
int32 argc, char *argv[]);
wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
char *argv[]);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN bool
@ -559,8 +605,7 @@ wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
uint32 app_offset,
uint32 *p_app_start_offset,
uint32 app_offset, uint32 *p_app_start_offset,
uint32 *p_app_end_offset);
/* See wasm_export.h for description */
@ -600,8 +645,7 @@ wasm_runtime_register_module_internal(const char *module_name,
WASMModuleCommon *module,
uint8 *orig_file_buf,
uint32 orig_file_buf_size,
char *error_buf,
uint32 error_buf_size);
char *error_buf, uint32 error_buf_size);
void
wasm_runtime_unregister_module(const WASMModuleCommon *module);
@ -610,8 +654,8 @@ bool
wasm_runtime_is_module_registered(const char *module_name);
bool
wasm_runtime_add_loading_module(const char *module_name,
char *error_buf, uint32 error_buf_size);
wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
uint32 error_buf_size);
void
wasm_runtime_delete_loading_module(const char *module_name);
@ -628,30 +672,28 @@ wasm_runtime_is_built_in_module(const char *module_name);
#if WASM_ENABLE_THREAD_MGR != 0
bool
wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env,
uint32 *start_offset, uint32 *size);
wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
uint32 *size);
bool
wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env,
uint32 start_offset, uint32 size);
wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
uint32 size);
#endif
#if WASM_ENABLE_LIBC_WASI != 0
WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module,
const char *dir_list[], uint32 dir_count,
const char *map_dir_list[], uint32 map_dir_count,
const char *env_list[], uint32 env_count,
char *argv[], int argc,
int stdinfd, int stdoutfd, int stderrfd);
wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
uint32 dir_count, const char *map_dir_list[],
uint32 map_dir_count, const char *env_list[],
uint32 env_count, char *argv[], int argc,
int stdinfd, int stdoutfd, int stderrfd);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_wasi_args(WASMModuleCommon *module,
const char *dir_list[], uint32 dir_count,
const char *map_dir_list[], uint32 map_dir_count,
const char *env_list[], uint32 env_count,
char *argv[], int argc);
wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
uint32 dir_count, const char *map_dir_list[],
uint32 map_dir_count, const char *env_list[],
uint32 env_count, char *argv[], int argc);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN bool
@ -665,9 +707,8 @@ bool
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
const char *dir_list[], uint32 dir_count,
const char *map_dir_list[], uint32 map_dir_count,
const char *env[], uint32 env_count,
char *argv[], uint32 argc,
int stdinfd, int stdoutfd, int stderrfd,
const char *env[], uint32 env_count, char *argv[],
uint32 argc, int stdinfd, int stdoutfd, int stderrfd,
char *error_buf, uint32 error_buf_size);
void
@ -685,8 +726,8 @@ wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
#if WASM_ENABLE_REF_TYPES != 0
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN bool
wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst,
void *extern_obj, uint32 *p_externref_idx);
wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
uint32 *p_externref_idx);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN bool
@ -711,7 +752,7 @@ wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
#endif /* end of WASM_ENABLE_REF_TYPES */
/* Get module of the current exec_env */
WASMModuleCommon*
WASMModuleCommon *
wasm_exec_env_get_module(WASMExecEnv *exec_env);
/**
@ -722,7 +763,8 @@ wasm_exec_env_get_module(WASMExecEnv *exec_env);
* @return return true if enlarge successfully, false otherwise
*/
bool
wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, uint32 inc_page_count);
wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
uint32 inc_page_count);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN bool
@ -739,14 +781,14 @@ wasm_runtime_register_natives_raw(const char *module_name,
bool
wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
const WASMType *func_type, const char *signature,
void *attachment,
uint32 *argv, uint32 argc, uint32 *ret);
void *attachment, uint32 *argv, uint32 argc,
uint32 *ret);
bool
wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
const WASMType *func_type, const char *signature,
void *attachment,
uint32 *argv, uint32 argc, uint32 *ret);
void *attachment, uint32 *argv, uint32 argc,
uint32 *ret);
void
wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
@ -755,8 +797,8 @@ void
wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
void
wasm_runtime_dump_module_inst_mem_consumption(const WASMModuleInstanceCommon
*module_inst);
wasm_runtime_dump_module_inst_mem_consumption(
const WASMModuleInstanceCommon *module_inst);
void
wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
@ -773,37 +815,32 @@ wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
bool
wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
const WASMExport *export_,
WASMType **out);
const WASMExport *export_, WASMType **out);
bool
wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
const WASMExport *export_,
uint8 *out_val_type,
bool *out_mutability);
uint8 *out_val_type, bool *out_mutability);
bool
wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
const WASMExport *export_,
uint32 *out_min_page,
uint32 *out_max_page);
uint32 *out_min_page, uint32 *out_max_page);
bool
wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
const WASMExport *export_,
uint8 *out_elem_type,
uint32 *out_min_size,
uint8 *out_elem_type, uint32 *out_min_size,
uint32 *out_max_size);
bool
wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
void *func_ptr, WASMType *func_type,
uint32 argc, uint32 *argv,
bool with_env, void *wasm_c_api_env);
uint32 argc, uint32 *argv, bool with_env,
void *wasm_c_api_env);
#ifdef __cplusplus
}
#endif
#endif /* end of _WASM_COMMON_H */

View File

@ -10,9 +10,12 @@ static bh_list shared_memory_list_head;
static bh_list *const shared_memory_list = &shared_memory_list_head;
static korp_mutex shared_memory_list_lock;
/* clang-format off */
enum {
S_WAITING, S_NOTIFIED
S_WAITING,
S_NOTIFIED
};
/* clang-format on */
typedef struct AtomicWaitInfo {
korp_mutex wait_list_lock;
@ -45,11 +48,9 @@ wasm_shared_memory_init()
if (os_mutex_init(&shared_memory_list_lock) != 0)
return false;
/* wait map not exists, create new map */
if (!(wait_map =
bh_hash_map_create(32, true,
(HashFunc)wait_address_hash,
(KeyEqualFunc)wait_address_equal,
NULL, destroy_wait_info))) {
if (!(wait_map = bh_hash_map_create(32, true, (HashFunc)wait_address_hash,
(KeyEqualFunc)wait_address_equal, NULL,
destroy_wait_info))) {
os_mutex_destroy(&shared_memory_list_lock);
return false;
}
@ -66,7 +67,7 @@ wasm_shared_memory_destroy()
}
}
static WASMSharedMemNode*
static WASMSharedMemNode *
search_module(WASMModuleCommon *module)
{
WASMSharedMemNode *node;
@ -86,7 +87,7 @@ search_module(WASMModuleCommon *module)
return NULL;
}
WASMSharedMemNode*
WASMSharedMemNode *
wasm_module_get_shared_memory(WASMModuleCommon *module)
{
return search_module(module);
@ -128,13 +129,13 @@ shared_memory_dec_reference(WASMModuleCommon *module)
return -1;
}
WASMMemoryInstanceCommon*
WASMMemoryInstanceCommon *
shared_memory_get_memory_inst(WASMSharedMemNode *node)
{
return node->memory_inst;
}
WASMSharedMemNode*
WASMSharedMemNode *
shared_memory_set_memory_inst(WASMModuleCommon *module,
WASMMemoryInstanceCommon *memory)
{
@ -223,16 +224,15 @@ acquire_wait_info(void *address, bool create)
AtomicWaitInfo *wait_info = NULL;
bh_list_status ret;
wait_info = (AtomicWaitInfo *)
bh_hash_map_find(wait_map, address);
wait_info = (AtomicWaitInfo *)bh_hash_map_find(wait_map, address);
if (!create)
return wait_info;
/* No wait info on this address, create new info */
if (!wait_info) {
if (!(wait_info =
(AtomicWaitInfo *)wasm_runtime_malloc(sizeof(AtomicWaitInfo))))
if (!(wait_info = (AtomicWaitInfo *)wasm_runtime_malloc(
sizeof(AtomicWaitInfo))))
return NULL;
memset(wait_info, 0, sizeof(AtomicWaitInfo));
@ -247,8 +247,7 @@ acquire_wait_info(void *address, bool create)
return NULL;
}
if (!bh_hash_map_insert(wait_map, address,
(void *)wait_info)) {
if (!bh_hash_map_insert(wait_map, address, (void *)wait_info)) {
os_mutex_destroy(&wait_info->wait_list_lock);
wasm_runtime_free(wait_info);
return NULL;
@ -283,8 +282,7 @@ destroy_wait_info(void *wait_info)
}
static void
release_wait_info(HashMap *wait_map,
AtomicWaitInfo *wait_info, void *address)
release_wait_info(HashMap *wait_map, AtomicWaitInfo *wait_info, void *address)
{
if (wait_info->wait_list->len == 0) {
bh_hash_map_remove(wait_map, address, NULL, NULL);
@ -333,8 +331,8 @@ wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address,
os_mutex_lock(&wait_info->wait_list_lock);
if ((!wait64 && *(uint32*)address != (uint32)expect)
|| (wait64 && *(uint64*)address != expect)) {
if ((!wait64 && *(uint32 *)address != (uint32)expect)
|| (wait64 && *(uint64 *)address != expect)) {
os_mutex_unlock(&wait_info->wait_list_lock);
return 1;
}
@ -375,8 +373,7 @@ wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address,
if (timeout < 0)
timeout = BHT_WAIT_FOREVER;
os_cond_reltimedwait(&wait_node->wait_cond,
&wait_node->wait_lock, timeout);
os_cond_reltimedwait(&wait_node->wait_cond, &wait_node->wait_lock, timeout);
os_mutex_unlock(&wait_node->wait_lock);
@ -400,8 +397,8 @@ wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address,
}
uint32
wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module,
void *address, uint32 count)
wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, void *address,
uint32 count)
{
uint32 notify_result;
AtomicWaitInfo *wait_info;

View File

@ -37,7 +37,7 @@ wasm_shared_memory_init();
void
wasm_shared_memory_destroy();
WASMSharedMemNode*
WASMSharedMemNode *
wasm_module_get_shared_memory(WASMModuleCommon *module);
int32
@ -46,10 +46,10 @@ shared_memory_inc_reference(WASMModuleCommon *module);
int32
shared_memory_dec_reference(WASMModuleCommon *module);
WASMMemoryInstanceCommon*
WASMMemoryInstanceCommon *
shared_memory_get_memory_inst(WASMSharedMemNode *node);
WASMSharedMemNode*
WASMSharedMemNode *
shared_memory_set_memory_inst(WASMModuleCommon *module,
WASMMemoryInstanceCommon *memory);
@ -58,8 +58,8 @@ wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address,
uint64 expect, int64 timeout, bool wait64);
uint32
wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module,
void *address, uint32 count);
wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, void *address,
uint32 count);
#ifdef __cplusplus
}