re-org bh_definition.c && introduce wamr fast interpreter (#189)

Co-authored-by: Xu Jun
This commit is contained in:
Xu Jun
2020-03-07 22:20:38 +08:00
committed by GitHub
parent cfcaca3d35
commit 057c849fc0
44 changed files with 4118 additions and 1066 deletions

View File

@ -7,7 +7,17 @@ add_definitions (-DWASM_ENABLE_INTERP=1)
include_directories(${IWASM_INTERP_DIR})
file (GLOB_RECURSE source_all ${IWASM_INTERP_DIR}/*.c)
if (WAMR_BUILD_FAST_INTERP EQUAL 1)
set (INTERPRETER "wasm_interp_fast.c")
else ()
set (INTERPRETER "wasm_interp.c")
endif ()
file (GLOB_RECURSE source_all
${IWASM_INTERP_DIR}/wasm_loader.c
${IWASM_INTERP_DIR}/wasm_runtime.c
${IWASM_INTERP_DIR}/${INTERPRETER}
)
set (IWASM_INTERP_SOURCE ${source_all})

View File

@ -189,6 +189,12 @@ typedef struct WASMFunction {
bool has_op_func_call;
uint32 code_size;
uint8 *code;
#if WASM_ENABLE_FAST_INTERP != 0
uint32 code_compiled_size;
uint8 *code_compiled;
uint8 *consts;
uint32 const_cell_num;
#endif
} WASMFunction;
typedef struct WASMGlobal {
@ -231,7 +237,7 @@ typedef struct WASIArguments {
uint32 map_dir_count;
const char **env;
uint32 env_count;
const char **argv;
char **argv;
uint32 argc;
} WASIArguments;
#endif

View File

@ -225,27 +225,24 @@ LOAD_I16(void *addr)
#endif /* WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0 */
#define CHECK_MEMORY_OVERFLOW() do { \
uint32 offset1 = offset + addr; \
uint64 offset1 = offset + addr; \
/* if (flags != 2) \
LOG_VERBOSE("unaligned load/store in wasm interp, flag: %d.\n", flags); */\
/* The WASM spec doesn't require that the dynamic address operand must be \
unsigned, so we don't check whether integer overflow or not here. */ \
/* if (offset1 < offset) \
goto out_of_bounds; */ \
if (offset1 < memory_data_size) { \
if (offset1 + LOAD_SIZE[opcode - WASM_OP_I32_LOAD] <= memory_data_size) { \
/* If offset1 is in valid range, maddr must also be in valid range, \
no need to check it again. */ \
maddr = memory->memory_data + offset1; \
if (maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD] > memory->end_addr) \
goto out_of_bounds; \
} \
else if (offset1 > heap_base_offset \
&& offset1 < heap_base_offset + heap_data_size) { \
else if (offset1 > DEFAULT_APP_HEAP_BASE_OFFSET \
&& (offset1 + LOAD_SIZE[opcode - WASM_OP_I32_LOAD] <= \
DEFAULT_APP_HEAP_BASE_OFFSET + heap_data_size)) { \
/* If offset1 is in valid range, maddr must also be in valid range, \
no need to check it again. */ \
maddr = memory->heap_data + offset1 - memory->heap_base_offset; \
if (maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD] > memory->heap_data_end) \
goto out_of_bounds; \
} \
else \
goto out_of_bounds; \
@ -795,7 +792,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
WASMMemoryInstance *memory = module->default_memory;
uint32 num_bytes_per_page = memory ? memory->num_bytes_per_page : 0;
uint32 memory_data_size = memory ? num_bytes_per_page * memory->cur_page_count : 0;
uint32 heap_base_offset = memory ? (uint32)memory->heap_base_offset : 0;
uint32 heap_data_size = memory ? (uint32)(memory->heap_data_end - memory->heap_data) : 0;
uint8 *global_data = memory ? memory->global_data : NULL;
WASMTableInstance *table = module->default_table;
@ -1080,7 +1076,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_GET_LOCAL_FAST):
HANDLE_OP (EXT_OP_GET_LOCAL_FAST):
{
local_offset = *frame_ip++;
if (local_offset & 0x80)
@ -1111,7 +1107,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_SET_LOCAL_FAST):
HANDLE_OP (EXT_OP_SET_LOCAL_FAST):
{
local_offset = *frame_ip++;
if (local_offset & 0x80)
@ -1143,7 +1139,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_TEE_LOCAL_FAST):
HANDLE_OP (EXT_OP_TEE_LOCAL_FAST):
{
local_offset = *frame_ip++;
if (local_offset & 0x80)
@ -2225,6 +2221,11 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
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):
HANDLE_OP (EXT_OP_COPY_STACK_TOP):
HANDLE_OP (EXT_OP_COPY_STACK_TOP_I64):
{
wasm_set_exception(module, "WASM interp failed: unsupported opcode.");
goto got_exception;

View File

@ -26,6 +26,13 @@ typedef struct WASMInterpFrame {
/* Instruction pointer of the bytecode array. */
uint8 *ip;
#if WASM_ENABLE_FAST_INTERP != 0
/* return offset of current frame.
the callee will put return value here */
uint32 ret_offset;
uint32 *lp;
uint32 operand[1];
#else
/* Operand stack top pointer of the current frame. The bottom of
the stack is the next cell after the last local variable. */
uint32 *sp_bottom;
@ -43,6 +50,7 @@ typedef struct WASMInterpFrame {
ref to frame end: data types of local vairables and stack data
*/
uint32 lp[1];
#endif
} WASMInterpFrame;
/**

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -238,13 +238,19 @@ typedef enum WASMOpcode {
WASM_OP_F64_REINTERPRET_I64 = 0xbf, /* f64.reinterpret/i64 */
/* drop/select specified types*/
WASM_OP_DROP_64 = 0xc0,
WASM_OP_SELECT_64 = 0xc1,
WASM_OP_GET_LOCAL_FAST = 0xc2,
WASM_OP_SET_LOCAL_FAST = 0xc3,
WASM_OP_TEE_LOCAL_FAST = 0xc4,
WASM_OP_DROP_64 = 0xc0,
WASM_OP_SELECT_64 = 0xc1,
WASM_OP_IMPDEP = 0xc5
/* extend op code */
EXT_OP_GET_LOCAL_FAST = 0xc2,
EXT_OP_SET_LOCAL_FAST_I64 = 0xc3,
EXT_OP_SET_LOCAL_FAST = 0xc4,
EXT_OP_TEE_LOCAL_FAST = 0xc5,
EXT_OP_TEE_LOCAL_FAST_I64 = 0xc6,
EXT_OP_COPY_STACK_TOP = 0xc7,
EXT_OP_COPY_STACK_TOP_I64 = 0xc8,
WASM_OP_IMPDEP = 0xc9
} WASMOpcode;
#ifdef __cplusplus
@ -450,12 +456,16 @@ static const void *_name[WASM_INSTRUCTION_NUM] = { \
HANDLE_OPCODE (WASM_OP_I64_REINTERPRET_F64), /* 0xbd */ \
HANDLE_OPCODE (WASM_OP_F32_REINTERPRET_I32), /* 0xbe */ \
HANDLE_OPCODE (WASM_OP_F64_REINTERPRET_I64), /* 0xbf */ \
HANDLE_OPCODE (WASM_OP_DROP_64), /* 0xc0 */ \
HANDLE_OPCODE (WASM_OP_SELECT_64), /* 0xc1 */ \
HANDLE_OPCODE (WASM_OP_GET_LOCAL_FAST),/* 0xc2 */ \
HANDLE_OPCODE (WASM_OP_SET_LOCAL_FAST),/* 0xc3 */ \
HANDLE_OPCODE (WASM_OP_TEE_LOCAL_FAST),/* 0xc4 */ \
HANDLE_OPCODE (WASM_OP_IMPDEP), /* 0xc5 */ \
HANDLE_OPCODE (WASM_OP_DROP_64), /* 0xc0 */ \
HANDLE_OPCODE (WASM_OP_SELECT_64), /* 0xc1 */ \
HANDLE_OPCODE (EXT_OP_GET_LOCAL_FAST), /* 0xc2 */ \
HANDLE_OPCODE (EXT_OP_SET_LOCAL_FAST_I64), /* 0xc3 */ \
HANDLE_OPCODE (EXT_OP_SET_LOCAL_FAST), /* 0xc4 */ \
HANDLE_OPCODE (EXT_OP_TEE_LOCAL_FAST), /* 0xc5 */ \
HANDLE_OPCODE (EXT_OP_TEE_LOCAL_FAST_I64), /* 0xc6 */ \
HANDLE_OPCODE (EXT_OP_COPY_STACK_TOP), /* 0xc7 */ \
HANDLE_OPCODE (EXT_OP_COPY_STACK_TOP_I64), /* 0xc8 */ \
HANDLE_OPCODE (WASM_OP_IMPDEP), /* 0xc9 */ \
}
#endif /* end of _WASM_OPCODE_H */

View File

@ -355,6 +355,10 @@ functions_instantiate(const WASMModule *module,
function->local_offsets = function->u.func->local_offsets;
#if WASM_ENABLE_FAST_INTERP != 0
function->const_cell_num = function->u.func->const_cell_num;
#endif
function++;
}

View File

@ -87,6 +87,10 @@ typedef struct WASMFunctionInstance {
uint16 ret_cell_num;
/* cell num of local variables, 0 for import function */
uint16 local_cell_num;
#if WASM_ENABLE_FAST_INTERP != 0
/* cell num of consts */
uint16 const_cell_num;
#endif
uint16 *local_offsets;
/* parameter types */
uint8 *param_types;
@ -165,7 +169,11 @@ typedef struct WASMInterpFrame WASMRuntimeFrame;
static inline uint8*
wasm_get_func_code(WASMFunctionInstance *func)
{
#if WASM_ENABLE_FAST_INTERP == 0
return func->is_import_func ? NULL : func->u.func->code;
#else
return func->is_import_func ? NULL : func->u.func->code_compiled;
#endif
}
/**
@ -178,8 +186,13 @@ wasm_get_func_code(WASMFunctionInstance *func)
static inline uint8*
wasm_get_func_code_end(WASMFunctionInstance *func)
{
#if WASM_ENABLE_FAST_INTERP == 0
return func->is_import_func
? NULL : func->u.func->code + func->u.func->code_size;
#else
return func->is_import_func
? NULL : func->u.func->code_compiled + func->u.func->code_compiled_size;
#endif
}
WASMModule *