Add wasm module global type information APIs (#3406)
Support getting global type from `wasm_runtime_get_import_type` and `wasm_runtime_get_export_type`, and add two APIs: ```C wasm_valkind_t wasm_global_type_get_valkind(const wasm_global_type_t global_type); bool wasm_global_type_get_mutable(const wasm_global_type_t global_type); ```
This commit is contained in:
@ -478,14 +478,14 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
!module->import_globals[global_idx].u.global.is_mutable);
|
||||
|
||||
if (global_idx < module->import_global_count) {
|
||||
global_type =
|
||||
module->import_globals[global_idx].u.global.type;
|
||||
global_type = module->import_globals[global_idx]
|
||||
.u.global.type.val_type;
|
||||
}
|
||||
else {
|
||||
global_type =
|
||||
module
|
||||
->globals[global_idx - module->import_global_count]
|
||||
.type;
|
||||
.type.val_type;
|
||||
}
|
||||
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
|
||||
@ -834,8 +834,8 @@ load_global_import(const uint8 **p_buf, const uint8 *buf_end,
|
||||
global->is_linked = ret;
|
||||
global->module_name = sub_module_name;
|
||||
global->field_name = global_name;
|
||||
global->type = declare_type;
|
||||
global->is_mutable = is_mutable;
|
||||
global->type.val_type = declare_type;
|
||||
global->type.is_mutable = is_mutable;
|
||||
(void)p_end;
|
||||
return true;
|
||||
}
|
||||
@ -1373,14 +1373,15 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
||||
|
||||
for (i = 0; i < global_count; i++, global++) {
|
||||
CHECK_BUF(p, p_end, 2);
|
||||
global->type = read_uint8(p);
|
||||
global->type.val_type = read_uint8(p);
|
||||
mutable = read_uint8(p);
|
||||
bh_assert(mutable < 2);
|
||||
global->is_mutable = mutable ? true : false;
|
||||
global->type.is_mutable = mutable ? true : false;
|
||||
|
||||
/* initialize expression */
|
||||
if (!load_init_expr(module, &p, p_end, &(global->init_expr),
|
||||
global->type, error_buf, error_buf_size))
|
||||
global->type.val_type, error_buf,
|
||||
error_buf_size))
|
||||
return false;
|
||||
|
||||
if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) {
|
||||
@ -2065,7 +2066,7 @@ calculate_global_data_offset(WASMModule *module)
|
||||
#if WASM_ENABLE_FAST_JIT != 0
|
||||
import_global->data_offset = data_offset;
|
||||
#endif
|
||||
data_offset += wasm_value_type_size(import_global->type);
|
||||
data_offset += wasm_value_type_size(import_global->type.val_type);
|
||||
}
|
||||
|
||||
for (i = 0; i < module->global_count; i++) {
|
||||
@ -2073,7 +2074,7 @@ calculate_global_data_offset(WASMModule *module)
|
||||
#if WASM_ENABLE_FAST_JIT != 0
|
||||
global->data_offset = data_offset;
|
||||
#endif
|
||||
data_offset += wasm_value_type_size(global->type);
|
||||
data_offset += wasm_value_type_size(global->type.val_type);
|
||||
}
|
||||
|
||||
module->global_data_size = data_offset;
|
||||
@ -2702,7 +2703,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
||||
if (!strcmp(export->name, "__heap_base")) {
|
||||
global_index = export->index - module->import_global_count;
|
||||
global = module->globals + global_index;
|
||||
if (global->type == VALUE_TYPE_I32 && !global->is_mutable
|
||||
if (global->type.val_type == VALUE_TYPE_I32
|
||||
&& !global->type.is_mutable
|
||||
&& global->init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_I32_CONST) {
|
||||
aux_heap_base_global = global;
|
||||
@ -2715,7 +2717,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
||||
else if (!strcmp(export->name, "__data_end")) {
|
||||
global_index = export->index - module->import_global_count;
|
||||
global = module->globals + global_index;
|
||||
if (global->type == VALUE_TYPE_I32 && !global->is_mutable
|
||||
if (global->type.val_type == VALUE_TYPE_I32
|
||||
&& !global->type.is_mutable
|
||||
&& global->init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_I32_CONST) {
|
||||
aux_data_end_global = global;
|
||||
@ -2754,9 +2757,9 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
||||
for (global_index = 0; global_index < module->global_count;
|
||||
global_index++) {
|
||||
global = module->globals + global_index;
|
||||
if (global->is_mutable /* heap_base and data_end is
|
||||
not mutable */
|
||||
&& global->type == VALUE_TYPE_I32
|
||||
if (global->type.is_mutable /* heap_base and data_end is
|
||||
not mutable */
|
||||
&& global->type.val_type == VALUE_TYPE_I32
|
||||
&& global->init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_I32_CONST
|
||||
&& (uint64)(uint32)global->init_expr.u.i32
|
||||
@ -7149,13 +7152,13 @@ re_scan:
|
||||
read_leb_uint32(p, p_end, global_idx);
|
||||
bh_assert(global_idx < global_count);
|
||||
|
||||
global_type =
|
||||
global_idx < module->import_global_count
|
||||
? module->import_globals[global_idx].u.global.type
|
||||
: module
|
||||
->globals[global_idx
|
||||
- module->import_global_count]
|
||||
.type;
|
||||
global_type = global_idx < module->import_global_count
|
||||
? module->import_globals[global_idx]
|
||||
.u.global.type.val_type
|
||||
: module
|
||||
->globals[global_idx
|
||||
- module->import_global_count]
|
||||
.type.val_type;
|
||||
|
||||
PUSH_TYPE(global_type);
|
||||
|
||||
@ -7183,22 +7186,22 @@ re_scan:
|
||||
read_leb_uint32(p, p_end, global_idx);
|
||||
bh_assert(global_idx < global_count);
|
||||
|
||||
is_mutable =
|
||||
global_idx < module->import_global_count
|
||||
? module->import_globals[global_idx].u.global.is_mutable
|
||||
: module
|
||||
->globals[global_idx
|
||||
- module->import_global_count]
|
||||
.is_mutable;
|
||||
is_mutable = global_idx < module->import_global_count
|
||||
? module->import_globals[global_idx]
|
||||
.u.global.type.is_mutable
|
||||
: module
|
||||
->globals[global_idx
|
||||
- module->import_global_count]
|
||||
.type.is_mutable;
|
||||
bh_assert(is_mutable);
|
||||
|
||||
global_type =
|
||||
global_idx < module->import_global_count
|
||||
? module->import_globals[global_idx].u.global.type
|
||||
: module
|
||||
->globals[global_idx
|
||||
- module->import_global_count]
|
||||
.type;
|
||||
global_type = global_idx < module->import_global_count
|
||||
? module->import_globals[global_idx]
|
||||
.u.global.type.val_type
|
||||
: module
|
||||
->globals[global_idx
|
||||
- module->import_global_count]
|
||||
.type.val_type;
|
||||
|
||||
#if WASM_ENABLE_FAST_INTERP == 0
|
||||
if (is_64bit_type(global_type)) {
|
||||
|
||||
Reference in New Issue
Block a user