Refine get/set global opcodes for interpreter (#294)

This commit is contained in:
wenyongh 2020-06-29 14:17:27 +08:00 committed by GitHub
parent ee315e4049
commit 847dccaa34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 252 additions and 152 deletions

View File

@ -974,7 +974,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
uint8 *global_data = module->global_data;
uint32 linear_mem_size = memory ? num_bytes_per_page * memory->cur_page_count : 0;
WASMTableInstance *table = module->default_table;
WASMGlobalInstance *globals = module->globals;
WASMGlobalInstance *globals = module->globals, *global;
uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
WASMInterpFrame *frame = NULL;
/* Points to this special opcode so as to jump to the call_method_from_entry. */
@ -982,7 +982,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
register uint32 *frame_lp = NULL; /* cache of frame->lp */
register uint32 *frame_sp = NULL; /* cache of frame->sp */
WASMBranchBlock *frame_csp = NULL;
WASMGlobalInstance *global;
BlockAddr *cache_items;
uint8 *frame_ip_end = frame_ip + 1;
uint8 opcode, block_ret_type;
@ -995,13 +994,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
uint32 local_idx, local_offset, global_idx;
uint8 local_type, *global_addr;
uint32 cache_index;
int32 aux_stack_top_global_idx = -1;
/* If the aux stack information is resolved,
we will check the aux stack boundary */
if (module->module->llvm_aux_stack_size) {
aux_stack_top_global_idx = module->module->llvm_aux_stack_global_index;
}
#if WASM_ENABLE_LABELS_AS_VALUES != 0
#define HANDLE_OPCODE(op) &&HANDLE_##op
@ -1383,70 +1375,87 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
HANDLE_OP (WASM_OP_GET_GLOBAL):
{
read_leb_uint32(frame_ip, frame_ip_end, global_idx);
bh_assert(global_idx < module->global_count);
global = globals + global_idx;
global_addr =
#if WASM_ENABLE_MULTI_MODULE != 0
global->import_global_inst
#if WASM_ENABLE_MULTI_MODULE == 0
global_addr = global_data + global->data_offset;
#else
global_addr = global->import_global_inst
? global->import_module_inst->global_data
+ global->import_global_inst->data_offset
:
: global_data + global->data_offset;
#endif
global_data + global->data_offset;
switch (global->type) {
case VALUE_TYPE_I32:
case VALUE_TYPE_F32:
PUSH_I32(*(uint32*)global_addr);
break;
case VALUE_TYPE_I64:
case VALUE_TYPE_F64:
PUSH_I64(GET_I64_FROM_ADDR((uint32*)global_addr));
break;
default:
wasm_set_exception(module, "invalid global type");
goto got_exception;
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_GET_GLOBAL_64):
{
read_leb_uint32(frame_ip, frame_ip_end, global_idx);
bh_assert(global_idx < module->global_count);
global = globals + global_idx;
#if WASM_ENABLE_MULTI_MODULE == 0
global_addr = global_data + global->data_offset;
#else
global_addr = global->import_global_inst
? global->import_module_inst->global_data
+ global->import_global_inst->data_offset
: global_data + global->data_offset;
#endif
PUSH_I64(GET_I64_FROM_ADDR((uint32*)global_addr));
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_SET_GLOBAL):
{
read_leb_uint32(frame_ip, frame_ip_end, global_idx);
bh_assert(global_idx < module->global_count);
global = globals + global_idx;
global_addr =
#if WASM_ENABLE_MULTI_MODULE != 0
global->import_global_inst
#if WASM_ENABLE_MULTI_MODULE == 0
global_addr = global_data + global->data_offset;
#else
global_addr = global->import_global_inst
? global->import_module_inst->global_data
+ global->import_global_inst->data_offset
:
: global_data + global->data_offset;
#endif
global_data + global->data_offset;
switch (global->type) {
case VALUE_TYPE_I32:
/* Check aux stack boundary */
if ((global_idx == (uint32)aux_stack_top_global_idx)
&& (*(uint32*)(frame_sp - 1) < exec_env->aux_stack_boundary))
goto out_of_bounds;
*(int32*)global_addr = POP_I32();
break;
case VALUE_TYPE_F32:
*(int32*)global_addr = POP_I32();
break;
case VALUE_TYPE_I64:
case VALUE_TYPE_F64:
PUT_I64_TO_ADDR((uint32*)global_addr, POP_I64());
break;
default:
wasm_set_exception(module, "invalid global type");
goto got_exception;
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_SET_GLOBAL_AUX_STACK):
{
read_leb_uint32(frame_ip, frame_ip_end, global_idx);
bh_assert(global_idx < module->global_count);
global = globals + global_idx;
#if WASM_ENABLE_MULTI_MODULE == 0
global_addr = global_data + global->data_offset;
#else
global_addr = global->import_global_inst
? global->import_module_inst->global_data
+ global->import_global_inst->data_offset
: global_data + global->data_offset;
#endif
if (*(uint32*)(frame_sp - 1) < exec_env->aux_stack_boundary)
goto out_of_bounds;
*(int32*)global_addr = POP_I32();
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_SET_GLOBAL_64):
{
read_leb_uint32(frame_ip, frame_ip_end, global_idx);
bh_assert(global_idx < module->global_count);
global = globals + global_idx;
#if WASM_ENABLE_MULTI_MODULE == 0
global_addr = global_data + global->data_offset;
#else
global_addr = global->import_global_inst
? global->import_module_inst->global_data
+ global->import_global_inst->data_offset
: global_data + global->data_offset;
#endif
PUT_I64_TO_ADDR((uint32*)global_addr, POP_I64());
HANDLE_OP_END ();
}
@ -2691,9 +2700,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
HANDLE_OP (WASM_OP_UNUSED_0x1d):
HANDLE_OP (WASM_OP_UNUSED_0x1e):
HANDLE_OP (WASM_OP_UNUSED_0x1f):
HANDLE_OP (WASM_OP_UNUSED_0x25):
HANDLE_OP (WASM_OP_UNUSED_0x26):
HANDLE_OP (WASM_OP_UNUSED_0x27):
/* Used by fast interpreter */
HANDLE_OP (EXT_OP_SET_LOCAL_FAST_I64):
HANDLE_OP (EXT_OP_TEE_LOCAL_FAST_I64):

View File

@ -968,7 +968,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
uint8 *global_data = module->global_data;
uint32 linear_mem_size = memory ? num_bytes_per_page * memory->cur_page_count : 0;
WASMTableInstance *table = module->default_table;
WASMGlobalInstance *globals = module->globals;
WASMGlobalInstance *globals = module->globals, *global;
uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
WASMInterpFrame *frame = NULL;
/* Points to this special opcode so as to jump to the call_method_from_entry. */
@ -977,7 +977,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
#if WASM_ENABLE_ABS_LABEL_ADDR == 0
register uint8 *label_base = &&HANDLE_WASM_OP_UNREACHABLE; /* cache of label base addr */
#endif
WASMGlobalInstance *global;
uint8 *frame_ip_end;
uint32 cond, count, fidx, tidx, frame_size = 0;
uint64 all_cell_num = 0;
@ -986,7 +985,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
uint8 *maddr = NULL;
uint32 local_idx, local_offset, global_idx;
uint8 opcode, local_type, *global_addr;
int32 aux_stack_top_global_idx = -1;
#if WASM_ENABLE_LABELS_AS_VALUES != 0
#define HANDLE_OPCODE(op) &&HANDLE_##op
@ -1000,12 +998,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
#endif
#endif
/* If the aux stack information is resolved,
we will check the aux stack boundary */
if (module->module->llvm_aux_stack_size) {
aux_stack_top_global_idx = module->module->llvm_aux_stack_global_index;
}
#if WASM_ENABLE_LABELS_AS_VALUES == 0
while (frame_ip < frame_ip_end) {
opcode = *frame_ip++;
@ -1225,72 +1217,92 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
HANDLE_OP (WASM_OP_GET_GLOBAL):
{
global_idx = read_uint32(frame_ip);
addr_ret = GET_OFFSET();
bh_assert(global_idx < module->global_count);
global = globals + global_idx;
global_addr =
#if WASM_ENABLE_MULTI_MODULE != 0
global->import_global_inst
#if WASM_ENABLE_MULTI_MODULE == 0
global_addr = global_data + global->data_offset;
#else
global_addr = global->import_global_inst
? global->import_module_inst->global_data
+ global->import_global_inst->data_offset
:
: global_data + global->data_offset;
#endif
global_data + global->data_offset;
switch (global->type) {
case VALUE_TYPE_I32:
case VALUE_TYPE_F32:
addr_ret = GET_OFFSET();
frame_lp[addr_ret] = *(uint32*)global_addr;
break;
case VALUE_TYPE_I64:
case VALUE_TYPE_F64:
*(uint64 *)(frame_lp + addr_ret) = GET_I64_FROM_ADDR((uint32*)global_addr);
break;
default:
wasm_set_exception(module, "invalid global type");
goto got_exception;
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_GET_GLOBAL_64):
{
global_idx = read_uint32(frame_ip);
bh_assert(global_idx < module->global_count);
global = globals + global_idx;
#if WASM_ENABLE_MULTI_MODULE == 0
global_addr = global_data + global->data_offset;
#else
global_addr = global->import_global_inst
? global->import_module_inst->global_data
+ global->import_global_inst->data_offset
: global_data + global->data_offset;
#endif
addr_ret = GET_OFFSET();
*(uint64 *)(frame_lp + addr_ret) = GET_I64_FROM_ADDR((uint32*)global_addr);
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_SET_GLOBAL):
{
global_idx = read_uint32(frame_ip);
addr1 = GET_OFFSET();
bh_assert(global_idx < module->global_count);
global = globals + global_idx;
global_addr =
#if WASM_ENABLE_MULTI_MODULE != 0
global->import_global_inst
#if WASM_ENABLE_MULTI_MODULE == 0
global_addr = global_data + global->data_offset;
#else
global_addr = global->import_global_inst
? global->import_module_inst->global_data
+ global->import_global_inst->data_offset
:
: global_data + global->data_offset;
#endif
global_data + global->data_offset;
switch (global->type) {
case VALUE_TYPE_I32:
/* Check aux stack boundary */
if ((global_idx == (uint32)aux_stack_top_global_idx)
&& (frame_lp[addr1] < exec_env->aux_stack_boundary))
goto out_of_bounds;
addr1 = GET_OFFSET();
*(int32*)global_addr = frame_lp[addr1];
break;
case VALUE_TYPE_F32:
*(int32*)global_addr = frame_lp[addr1];
break;
case VALUE_TYPE_I64:
case VALUE_TYPE_F64:
PUT_I64_TO_ADDR((uint32*)global_addr, *(int64 *)(frame_lp + addr1));
break;
default:
wasm_set_exception(module, "invalid global type");
goto got_exception;
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_SET_GLOBAL_AUX_STACK):
{
global_idx = read_uint32(frame_ip);
bh_assert(global_idx < module->global_count);
global = globals + global_idx;
#if WASM_ENABLE_MULTI_MODULE == 0
global_addr = global_data + global->data_offset;
#else
global_addr = global->import_global_inst
? global->import_module_inst->global_data
+ global->import_global_inst->data_offset
: global_data + global->data_offset;
#endif
addr1 = GET_OFFSET();
if (frame_lp[addr1] < exec_env->aux_stack_boundary)
goto out_of_bounds;
*(int32*)global_addr = frame_lp[addr1];
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_SET_GLOBAL_64):
{
global_idx = read_uint32(frame_ip);
bh_assert(global_idx < module->global_count);
global = globals + global_idx;
#if WASM_ENABLE_MULTI_MODULE == 0
global_addr = global_data + global->data_offset;
#else
global_addr = global->import_global_inst
? global->import_module_inst->global_data
+ global->import_global_inst->data_offset
: global_data + global->data_offset;
#endif
addr1 = GET_OFFSET();
PUT_I64_TO_ADDR((uint32*)global_addr, *(int64 *)(frame_lp + addr1));
HANDLE_OP_END ();
}
@ -2538,9 +2550,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
HANDLE_OP (WASM_OP_UNUSED_0x1d):
HANDLE_OP (WASM_OP_UNUSED_0x1e):
HANDLE_OP (WASM_OP_UNUSED_0x1f):
HANDLE_OP (WASM_OP_UNUSED_0x25):
HANDLE_OP (WASM_OP_UNUSED_0x26):
HANDLE_OP (WASM_OP_UNUSED_0x27):
/* optimized op code */
HANDLE_OP (WASM_OP_F32_STORE):
HANDLE_OP (WASM_OP_F64_STORE):

View File

@ -1538,10 +1538,9 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
case IMPORT_KIND_GLOBAL: /* import global */
bh_assert(import_globals);
import = import_globals++;
if (!load_global_import(module,
sub_module,
sub_module_name, field_name, &p,
p_end, &import->u.global,
if (!load_global_import(module, sub_module,
sub_module_name, field_name,
&p, p_end, &import->u.global,
error_buf, error_buf_size)) {
return false;
}
@ -3058,6 +3057,9 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
case WASM_OP_TEE_LOCAL:
case WASM_OP_GET_GLOBAL:
case WASM_OP_SET_GLOBAL:
case WASM_OP_GET_GLOBAL_64:
case WASM_OP_SET_GLOBAL_64:
case WASM_OP_SET_GLOBAL_AUX_STACK:
skip_leb_uint32(p, p_end); /* localidx */
break;
@ -5473,6 +5475,7 @@ re_scan:
case WASM_OP_GET_GLOBAL:
{
p_org = p - 1;
read_leb_uint32(p, p_end, global_idx);
if (global_idx >= global_count) {
set_error_buf(error_buf, error_buf_size,
@ -5481,21 +5484,38 @@ re_scan:
goto fail;
}
global_type = global_idx < module->import_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;
: module->globals[global_idx - module->import_global_count]
.type;
PUSH_TYPE(global_type);
#if WASM_ENABLE_FAST_INTERP != 0
#if WASM_ENABLE_FAST_INTERP == 0
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
if (global_type == VALUE_TYPE_I64
|| global_type == VALUE_TYPE_F64) {
*p_org = WASM_OP_GET_GLOBAL_64;
}
#endif
#else /* else of WASM_ENABLE_FAST_INTERP */
if (global_type == VALUE_TYPE_I64
|| global_type == VALUE_TYPE_F64) {
skip_label();
emit_label(WASM_OP_GET_GLOBAL_64);
}
emit_uint32(loader_ctx, global_idx);
PUSH_OFFSET_TYPE(global_type);
#endif
#endif /* end of WASM_ENABLE_FAST_INTERP */
break;
}
case WASM_OP_SET_GLOBAL:
{
bool is_mutable = false;
p_org = p - 1;
read_leb_uint32(p, p_end, global_idx);
if (global_idx >= global_count) {
set_error_buf(error_buf, error_buf_size,
@ -5523,10 +5543,32 @@ re_scan:
.type;
POP_TYPE(global_type);
#if WASM_ENABLE_FAST_INTERP != 0
#if WASM_ENABLE_FAST_INTERP == 0
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
if (global_type == VALUE_TYPE_I64
|| global_type == VALUE_TYPE_F64) {
*p_org = WASM_OP_SET_GLOBAL_64;
}
else if (module->llvm_aux_stack_size > 0
&& global_idx == module->llvm_aux_stack_global_index) {
*p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
}
#endif
#else /* else of WASM_ENABLE_FAST_INTERP */
if (global_type == VALUE_TYPE_I64
|| global_type == VALUE_TYPE_F64) {
skip_label();
emit_label(WASM_OP_SET_GLOBAL_64);
}
else if (module->llvm_aux_stack_size > 0
&& global_idx == module->llvm_aux_stack_global_index) {
skip_label();
emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
}
emit_uint32(loader_ctx, global_idx);
POP_OFFSET_TYPE(global_type);
#endif
#endif /* end of WASM_ENABLE_FAST_INTERP */
break;
}

View File

@ -782,10 +782,9 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
case IMPORT_KIND_GLOBAL: /* import global */
bh_assert(import_globals);
import = import_globals++;
if (!load_global_import(module,
sub_module,
sub_module_name, field_name, &p,
p_end, &import->u.global,
if (!load_global_import(module, sub_module,
sub_module_name, field_name,
&p, p_end, &import->u.global,
error_buf, error_buf_size)) {
return false;
}
@ -2087,6 +2086,9 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
case WASM_OP_TEE_LOCAL:
case WASM_OP_GET_GLOBAL:
case WASM_OP_SET_GLOBAL:
case WASM_OP_GET_GLOBAL_64:
case WASM_OP_SET_GLOBAL_64:
case WASM_OP_SET_GLOBAL_AUX_STACK:
skip_leb_uint32(p, p_end); /* localidx */
break;
@ -4371,24 +4373,42 @@ re_scan:
case WASM_OP_GET_GLOBAL:
{
p_org = p - 1;
read_leb_uint32(p, p_end, global_idx);
bh_assert(global_idx < global_count);
global_type = global_idx < module->import_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;
: module->globals[global_idx - module->import_global_count]
.type;
PUSH_TYPE(global_type);
#if WASM_ENABLE_FAST_INTERP != 0
#if WASM_ENABLE_FAST_INTERP == 0
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
if (global_type == VALUE_TYPE_I64
|| global_type == VALUE_TYPE_F64) {
*p_org = WASM_OP_GET_GLOBAL_64;
}
#endif
#else /* else of WASM_ENABLE_FAST_INTERP */
if (global_type == VALUE_TYPE_I64
|| global_type == VALUE_TYPE_F64) {
skip_label();
emit_label(WASM_OP_GET_GLOBAL_64);
}
emit_uint32(loader_ctx, global_idx);
PUSH_OFFSET_TYPE(global_type);
#endif
#endif /* end of WASM_ENABLE_FAST_INTERP */
break;
}
case WASM_OP_SET_GLOBAL:
{
bool is_mutable = false;
p_org = p - 1;
read_leb_uint32(p, p_end, global_idx);
bh_assert(global_idx < global_count);
@ -4406,10 +4426,33 @@ re_scan:
.type;
POP_TYPE(global_type);
#if WASM_ENABLE_FAST_INTERP != 0
#if WASM_ENABLE_FAST_INTERP == 0
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
if (global_type == VALUE_TYPE_I64
|| global_type == VALUE_TYPE_F64) {
*p_org = WASM_OP_SET_GLOBAL_64;
}
else if (module->llvm_aux_stack_size > 0
&& global_idx == module->llvm_aux_stack_global_index) {
*p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
}
#endif
#else /* else of WASM_ENABLE_FAST_INTERP */
if (global_type == VALUE_TYPE_I64
|| global_type == VALUE_TYPE_F64) {
skip_label();
emit_label(WASM_OP_SET_GLOBAL_64);
}
else if (module->llvm_aux_stack_size > 0
&& global_idx == module->llvm_aux_stack_global_index) {
skip_label();
emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
}
emit_uint32(loader_ctx, global_idx);
POP_OFFSET_TYPE(global_type);
#endif
#endif /* end of WASM_ENABLE_FAST_INTERP */
(void)is_mutable;
break;
}

View File

@ -60,9 +60,9 @@ typedef enum WASMOpcode {
WASM_OP_GET_GLOBAL = 0x23, /* get_global */
WASM_OP_SET_GLOBAL = 0x24, /* set_global */
WASM_OP_UNUSED_0x25 = 0x25,
WASM_OP_UNUSED_0x26 = 0x26,
WASM_OP_UNUSED_0x27 = 0x27,
WASM_OP_GET_GLOBAL_64 = 0x25,
WASM_OP_SET_GLOBAL_64 = 0x26,
WASM_OP_SET_GLOBAL_AUX_STACK = 0x27,
/* memory instructions */
WASM_OP_I32_LOAD = 0x28, /* i32.load */
@ -330,9 +330,9 @@ static type _name[WASM_INSTRUCTION_NUM] = { \
HANDLE_OPCODE (WASM_OP_TEE_LOCAL), /* 0x22 */ \
HANDLE_OPCODE (WASM_OP_GET_GLOBAL), /* 0x23 */ \
HANDLE_OPCODE (WASM_OP_SET_GLOBAL), /* 0x24 */ \
HANDLE_OPCODE (WASM_OP_UNUSED_0x25), /* 0x25 */ \
HANDLE_OPCODE (WASM_OP_UNUSED_0x26), /* 0x26 */ \
HANDLE_OPCODE (WASM_OP_UNUSED_0x27), /* 0x27 */ \
HANDLE_OPCODE (WASM_OP_GET_GLOBAL_64), /* 0x25 */ \
HANDLE_OPCODE (WASM_OP_SET_GLOBAL_64), /* 0x26 */ \
HANDLE_OPCODE (WASM_OP_SET_GLOBAL_AUX_STACK), /* 0x27 */ \
HANDLE_OPCODE (WASM_OP_I32_LOAD), /* 0x28 */ \
HANDLE_OPCODE (WASM_OP_I64_LOAD), /* 0x29 */ \
HANDLE_OPCODE (WASM_OP_F32_LOAD), /* 0x2a */ \