Implement more wasm-c-apis and enable Envoy integration (#622)
Implement more wasm-c-api APIs to support Envoy integration: - sync up with latest c-api definition - change CMakeLists to export necessary headers and install the static library of iwasm - enable to export tables and memories - support memorytype and tabletype APIs - update wasm-c-api sampels - enable to export importtype APIs And refine bazel scripts for sample XNNPACK workload, add license headers for sample simple. Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@ -24,17 +24,9 @@ WASM_DECLARE_VEC(module, *)
|
||||
WASM_DECLARE_VEC(instance, *)
|
||||
|
||||
/* Runtime Environment */
|
||||
typedef enum runtime_mode_e {
|
||||
INTERP_MODE = 0,
|
||||
JIT_MODE,
|
||||
AOT_MODE
|
||||
} runtime_mode_e;
|
||||
|
||||
struct wasm_engine_t {
|
||||
/* support one store for now */
|
||||
wasm_store_vec_t *stores;
|
||||
/* Interpreter by deault */
|
||||
runtime_mode_e mode;
|
||||
};
|
||||
|
||||
struct wasm_store_t {
|
||||
@ -64,13 +56,13 @@ struct wasm_globaltype_t {
|
||||
struct wasm_tabletype_t {
|
||||
uint32 extern_kind;
|
||||
/* always be WASM_FUNCREF */
|
||||
wasm_valtype_t *type;
|
||||
wasm_limits_t *limits;
|
||||
wasm_valtype_t *val_type;
|
||||
wasm_limits_t limits;
|
||||
};
|
||||
|
||||
struct wasm_memorytype_t {
|
||||
uint32 extern_kind;
|
||||
wasm_limits_t *limits;
|
||||
wasm_limits_t limits;
|
||||
};
|
||||
|
||||
struct wasm_externtype_t {
|
||||
@ -78,16 +70,15 @@ struct wasm_externtype_t {
|
||||
uint8 data[1];
|
||||
};
|
||||
|
||||
struct wasm_import_type_t {
|
||||
uint32 extern_kind;
|
||||
struct wasm_importtype_t {
|
||||
wasm_name_t *module_name;
|
||||
wasm_name_t *name;
|
||||
wasm_externtype_t *extern_type;
|
||||
};
|
||||
|
||||
struct wasm_export_type_t {
|
||||
uint32 extern_kind;
|
||||
wasm_name_t *module_name;
|
||||
struct wasm_exporttype_t {
|
||||
wasm_name_t *name;
|
||||
wasm_externtype_t *extern_type;
|
||||
};
|
||||
|
||||
/* Runtime Objects */
|
||||
@ -104,7 +95,7 @@ struct wasm_func_t {
|
||||
wasm_name_t *name;
|
||||
uint16 kind;
|
||||
|
||||
wasm_functype_t *func_type;
|
||||
wasm_functype_t *type;
|
||||
|
||||
bool with_env;
|
||||
union {
|
||||
@ -169,7 +160,7 @@ struct wasm_table_t {
|
||||
struct wasm_extern_t {
|
||||
wasm_name_t *module_name;
|
||||
wasm_name_t *name;
|
||||
uint16 kind;
|
||||
wasm_externkind_t kind;
|
||||
uint8 data[1];
|
||||
};
|
||||
|
||||
|
||||
@ -454,14 +454,16 @@ wasm_runtime_destroy_registered_module_list()
|
||||
bh_list_remove(registered_module_list, reg_module);
|
||||
|
||||
/* now, it is time to release every module in the runtime */
|
||||
if (reg_module->module->module_type == Wasm_Module_Bytecode) {
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (reg_module->module->module_type == Wasm_Module_Bytecode)
|
||||
wasm_unload((WASMModule *)reg_module->module);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (reg_module->module->module_type == Wasm_Module_AoT)
|
||||
aot_unload((AOTModule *)reg_module->module);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* destroy the file buffer */
|
||||
if (destroyer && reg_module->orig_file_buf) {
|
||||
@ -4318,3 +4320,244 @@ wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
|
||||
|
||||
bool
|
||||
wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
|
||||
const WASMExport *export,
|
||||
WASMType **out)
|
||||
{
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_comm->module_type == Wasm_Module_Bytecode) {
|
||||
WASMModule *module = (WASMModule *)module_comm;
|
||||
|
||||
if (export->index < module->import_function_count) {
|
||||
*out =
|
||||
module->import_functions[export->index].u.function.func_type;
|
||||
}
|
||||
else {
|
||||
*out =
|
||||
module->functions[export->index - module->import_function_count]
|
||||
->func_type;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_comm->module_type == Wasm_Module_AoT) {
|
||||
AOTModule *module = (AOTModule *)module_comm;
|
||||
|
||||
if (export->index < module->import_func_count) {
|
||||
*out = module->func_types[module->import_funcs[export->index]
|
||||
.func_type_index];
|
||||
}
|
||||
else {
|
||||
*out =
|
||||
module->func_types[module->func_type_indexes
|
||||
[export->index - module->import_func_count]];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
|
||||
const WASMExport *export,
|
||||
uint8 *out_val_type,
|
||||
bool *out_mutability)
|
||||
{
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_comm->module_type == Wasm_Module_Bytecode) {
|
||||
WASMModule *module = (WASMModule *)module_comm;
|
||||
|
||||
if (export->index < module->import_global_count) {
|
||||
WASMGlobalImport *import_global =
|
||||
&((module->import_globals + export->index)->u.global);
|
||||
*out_val_type = import_global->type;
|
||||
*out_mutability = import_global->is_mutable;
|
||||
}
|
||||
else {
|
||||
WASMGlobal *global =
|
||||
module->globals + (export->index - module->import_global_count);
|
||||
*out_val_type = global->type;
|
||||
*out_mutability = global->is_mutable;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_comm->module_type == Wasm_Module_AoT) {
|
||||
AOTModule *module = (AOTModule *)module_comm;
|
||||
|
||||
if (export->index < module->import_global_count) {
|
||||
AOTImportGlobal *import_global =
|
||||
module->import_globals + export->index;
|
||||
*out_val_type = import_global->type;
|
||||
*out_mutability = import_global->is_mutable;
|
||||
}
|
||||
else {
|
||||
AOTGlobal *global =
|
||||
module->globals + (export->index - module->import_global_count);
|
||||
*out_val_type = global->type;
|
||||
*out_mutability = global->is_mutable;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
|
||||
const WASMExport *export,
|
||||
uint32 *out_min_page,
|
||||
uint32 *out_max_page)
|
||||
{
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_comm->module_type == Wasm_Module_Bytecode) {
|
||||
WASMModule *module = (WASMModule *)module_comm;
|
||||
|
||||
if (export->index < module->import_memory_count) {
|
||||
WASMMemoryImport *import_memory =
|
||||
&((module->import_memories + export->index)->u.memory);
|
||||
*out_min_page = import_memory->init_page_count;
|
||||
*out_max_page = import_memory->max_page_count;
|
||||
}
|
||||
else {
|
||||
WASMMemory *memory =
|
||||
module->memories + (export->index - module->import_memory_count);
|
||||
*out_min_page = memory->init_page_count;
|
||||
*out_max_page = memory->max_page_count;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_comm->module_type == Wasm_Module_AoT) {
|
||||
AOTModule *module = (AOTModule *)module_comm;
|
||||
|
||||
if (export->index < module->import_memory_count) {
|
||||
AOTImportMemory *import_memory =
|
||||
module->import_memories + export->index;
|
||||
*out_min_page = import_memory->mem_init_page_count;
|
||||
*out_max_page = import_memory->mem_max_page_count;
|
||||
}
|
||||
else {
|
||||
AOTMemory *memory =
|
||||
module->memories + (export->index - module->import_memory_count);
|
||||
*out_min_page = memory->mem_init_page_count;
|
||||
*out_max_page = memory->mem_max_page_count;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
|
||||
const WASMExport *export,
|
||||
uint8 *out_elem_type,
|
||||
uint32 *out_min_size,
|
||||
uint32 *out_max_size)
|
||||
{
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_comm->module_type == Wasm_Module_Bytecode) {
|
||||
WASMModule *module = (WASMModule *)module_comm;
|
||||
|
||||
if (export->index < module->import_table_count) {
|
||||
WASMTableImport *import_table =
|
||||
&((module->import_tables + export->index)->u.table);
|
||||
*out_elem_type = import_table->elem_type;
|
||||
*out_min_size = import_table->init_size;
|
||||
*out_max_size = import_table->max_size;
|
||||
}
|
||||
else {
|
||||
WASMTable *table =
|
||||
module->tables + (export->index - module->import_table_count);
|
||||
*out_elem_type = table->elem_type;
|
||||
*out_min_size = table->init_size;
|
||||
*out_max_size = table->max_size;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_comm->module_type == Wasm_Module_AoT) {
|
||||
AOTModule *module = (AOTModule *)module_comm;
|
||||
|
||||
if (export->index < module->import_table_count) {
|
||||
AOTImportTable *import_table =
|
||||
module->import_tables + export->index;
|
||||
*out_elem_type = VALUE_TYPE_FUNCREF;
|
||||
*out_min_size = import_table->table_init_size;
|
||||
*out_max_size = import_table->table_max_size;
|
||||
}
|
||||
else {
|
||||
AOTTable *table =
|
||||
module->tables + (export->index - module->import_table_count);
|
||||
*out_elem_type = table->elem_type;
|
||||
*out_min_size = table->table_init_size;
|
||||
*out_max_size = table->table_max_size;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8 *
|
||||
wasm_runtime_get_memory_data(const WASMModuleInstanceCommon *module_inst_comm,
|
||||
uint32 memory_inst_idx)
|
||||
{
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
|
||||
WASMModuleInstance *module_inst =
|
||||
(WASMModuleInstance *)module_inst_comm;
|
||||
WASMMemoryInstance *memory_inst =
|
||||
module_inst->memories[memory_inst_idx];
|
||||
return memory_inst->memory_data;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_inst_comm->module_type == Wasm_Module_AoT) {
|
||||
AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
|
||||
AOTMemoryInstance *memory_inst =
|
||||
(AOTMemoryInstance*)module_inst->memories.ptr + memory_inst_idx;
|
||||
return memory_inst->memory_data.ptr;
|
||||
}
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32
|
||||
wasm_runtime_get_memory_data_size(
|
||||
const WASMModuleInstanceCommon *module_inst_comm,
|
||||
uint32 memory_inst_idx)
|
||||
{
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
|
||||
WASMModuleInstance *module_inst =
|
||||
(WASMModuleInstance *)module_inst_comm;
|
||||
WASMMemoryInstance *memory_inst =
|
||||
module_inst->memories[memory_inst_idx];
|
||||
return memory_inst->cur_page_count * memory_inst->num_bytes_per_page;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_inst_comm->module_type == Wasm_Module_AoT) {
|
||||
AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
|
||||
AOTMemoryInstance *memory_inst =
|
||||
(AOTMemoryInstance*)module_inst->memories.ptr + memory_inst_idx;
|
||||
return memory_inst->cur_page_count * memory_inst->num_bytes_per_page;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -744,6 +744,38 @@ wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
|
||||
bool ret, uint32 *argv);
|
||||
#endif
|
||||
|
||||
bool
|
||||
wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
|
||||
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);
|
||||
|
||||
bool
|
||||
wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
|
||||
const WASMExport *export,
|
||||
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,
|
||||
uint32 *out_max_size);
|
||||
|
||||
uint8 *
|
||||
wasm_runtime_get_memory_data(const WASMModuleInstanceCommon *module_inst_comm,
|
||||
uint32 memory_inst_idx);
|
||||
|
||||
uint32
|
||||
wasm_runtime_get_memory_data_size(const WASMModuleInstanceCommon *module_inst_comm,
|
||||
uint32 memory_inst_idx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user