Implement opcode memory.grow and fix zydis compile error (#1123)

This commit is contained in:
liang.he 2022-04-27 20:02:38 +08:00 committed by GitHub
parent 26c4a7ca33
commit 87b259a40a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 7 deletions

View File

@ -4435,11 +4435,11 @@ lower_callnative(JitCompContext *cc, x86::Assembler &a, bh_list *jmp_info_list,
case JIT_REG_KIND_I32:
if (jit_reg_is_const(arg_reg)) {
i32 = jit_cc_get_const_I32(cc, arg_reg);
imm.setValue(i32);
imm.setValue((int64)i32);
a.mov(regs_arg[i], imm);
}
else {
a.mov(regs_arg[i], regs_i32[jit_reg_no(arg_reg)]);
a.movsxd(regs_arg[i], regs_i32[jit_reg_no(arg_reg)]);
}
break;
case JIT_REG_KIND_I64:

View File

@ -4,8 +4,10 @@
*/
#include "jit_emit_memory.h"
#include "jit_emit_exception.h"
#include "../jit_frontend.h"
#include "fe/jit_emit_exception.h"
#include "../jit_codegen.h"
#include "../../interpreter/wasm_runtime.h"
static JitReg
get_memory_boundary(JitCompContext *cc, uint32 mem_idx, uint32 bytes)
@ -409,8 +411,45 @@ jit_compile_op_memory_size(JitCompContext *cc)
}
bool
jit_compile_op_memory_grow(JitCompContext *cc)
jit_compile_op_memory_grow(JitCompContext *cc, uint32 mem_idx)
{
JitReg delta, module_inst, grow_result, res, memory_inst, prev_page_count;
JitInsn *insn;
/* WASMMemoryInstance->cur_page_count before enlarging */
memory_inst = get_memory_inst_reg(cc->jit_frame, mem_idx);
prev_page_count = jit_cc_new_reg_I32(cc);
GEN_INSN(LDI32, prev_page_count, memory_inst,
NEW_CONST(I32, offsetof(WASMMemoryInstance, cur_page_count)));
/* call wasm_enlarge_memory */
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
/* Set native_ret to x86::eax */
grow_result = jit_codegen_get_hreg_by_name("eax");
#else
grow_result = jit_cc_new_reg_I32(cc);
#endif
POP_I32(delta);
module_inst = get_module_inst_reg(cc->jit_frame);
insn = GEN_INSN(CALLNATIVE, grow_result,
NEW_CONST(PTR, (uintptr_t)wasm_enlarge_memory), 2);
if (insn) {
*(jit_insn_opndv(insn, 2)) = module_inst;
*(jit_insn_opndv(insn, 3)) = delta;
}
/* check if enlarge memory success */
res = jit_cc_new_reg_I32(cc);
GEN_INSN(CMP, cc->cmp_reg, grow_result, NEW_CONST(I32, 0));
GEN_INSN(SELECTNE, res, cc->cmp_reg, prev_page_count,
NEW_CONST(I32, (int32)-1));
PUSH_I32(res);
/* ensure a refresh in next get_memory_XXX_reg */
clear_memory_regs(cc->jit_frame);
return true;
fail:
return false;
}

View File

@ -47,7 +47,7 @@ bool
jit_compile_op_memory_size(JitCompContext *cc);
bool
jit_compile_op_memory_grow(JitCompContext *cc);
jit_compile_op_memory_grow(JitCompContext *cc, uint32 mem_idx);
#if WASM_ENABLE_BULK_MEMORY != 0
bool

View File

@ -153,7 +153,7 @@ jit_compile_op_tee_local(JitCompContext *cc, uint32 local_idx)
break;
default:
bh_assert(0);
break;
goto fail;
}
return true;

View File

@ -58,6 +58,7 @@ if (WAMR_BUILD_TARGET STREQUAL "X86_64" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
FetchContent_Declare(
zydis
GIT_REPOSITORY https://github.com/zyantific/zydis.git
GIT_TAG e14a07895136182a5b53e181eec3b1c6e0b434de
)
FetchContent_GetProperties(zydis)
if (NOT zydis_POPULATED)

View File

@ -1398,7 +1398,7 @@ jit_compile_func(JitCompContext *cc)
case WASM_OP_MEMORY_GROW:
read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
if (!jit_compile_op_memory_grow(cc))
if (!jit_compile_op_memory_grow(cc, mem_idx))
return false;
break;