| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318 |
- /*
- * Copyright (C) 2019 Intel Corporation. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- */
- #include "jit_emit_control.h"
- #include "jit_emit_exception.h"
- #include "jit_emit_function.h"
- #include "../jit_frontend.h"
- #include "../interpreter/wasm_loader.h"
- #define CREATE_BASIC_BLOCK(new_basic_block) \
- do { \
- bh_assert(!new_basic_block); \
- if (!(new_basic_block = jit_cc_new_basic_block(cc, 0))) { \
- jit_set_last_error(cc, "create basic block failed"); \
- goto fail; \
- } \
- } while (0)
- #define CURR_BASIC_BLOCK() cc->cur_basic_block
- #define BUILD_BR(target_block) \
- do { \
- if (!GEN_INSN(JMP, jit_basic_block_label(target_block))) { \
- jit_set_last_error(cc, "generate jmp insn failed"); \
- goto fail; \
- } \
- } while (0)
- #define BUILD_COND_BR(value_if, block_then, block_else) \
- do { \
- if (!GEN_INSN(CMP, cc->cmp_reg, value_if, NEW_CONST(I32, 0)) \
- || !GEN_INSN(BNE, cc->cmp_reg, jit_basic_block_label(block_then), \
- jit_basic_block_label(block_else))) { \
- jit_set_last_error(cc, "generate bne insn failed"); \
- goto fail; \
- } \
- } while (0)
- #define SET_BUILDER_POS(basic_block) \
- do { \
- cc->cur_basic_block = basic_block; \
- } while (0)
- #define SET_BB_BEGIN_BCIP(basic_block, bcip) \
- do { \
- *(jit_annl_begin_bcip(cc, jit_basic_block_label(basic_block))) = bcip; \
- } while (0)
- #define SET_BB_END_BCIP(basic_block, bcip) \
- do { \
- *(jit_annl_end_bcip(cc, jit_basic_block_label(basic_block))) = bcip; \
- } while (0)
- static JitBlock *
- get_target_block(JitCompContext *cc, uint32 br_depth)
- {
- uint32 i = br_depth;
- JitBlock *block = jit_block_stack_top(&cc->block_stack);
- while (i-- > 0 && block) {
- block = block->prev;
- }
- if (!block) {
- jit_set_last_error(cc, "WASM block stack underflow");
- return NULL;
- }
- return block;
- }
- static bool
- load_block_params(JitCompContext *cc, JitBlock *block)
- {
- JitFrame *jit_frame = cc->jit_frame;
- uint32 offset, i;
- JitReg value = 0;
- /* Clear jit frame's locals and stacks */
- clear_values(jit_frame);
- /* Restore jit frame's sp to block's sp begin */
- jit_frame->sp = block->frame_sp_begin;
- /* Load params to new block */
- offset = (uint32)(jit_frame->sp - jit_frame->lp);
- for (i = 0; i < block->param_count; i++) {
- switch (block->param_types[i]) {
- case VALUE_TYPE_I32:
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_EXTERNREF:
- case VALUE_TYPE_FUNCREF:
- #endif
- value = gen_load_i32(jit_frame, offset);
- offset++;
- break;
- case VALUE_TYPE_I64:
- value = gen_load_i64(jit_frame, offset);
- offset += 2;
- break;
- case VALUE_TYPE_F32:
- value = gen_load_f32(jit_frame, offset);
- offset++;
- break;
- case VALUE_TYPE_F64:
- value = gen_load_f64(jit_frame, offset);
- offset += 2;
- break;
- default:
- bh_assert(0);
- break;
- }
- PUSH(value, block->param_types[i]);
- }
- return true;
- fail:
- return false;
- }
- static bool
- load_block_results(JitCompContext *cc, JitBlock *block)
- {
- JitFrame *jit_frame = cc->jit_frame;
- uint32 offset, i;
- JitReg value = 0;
- /* Restore jit frame's sp to block's sp begin */
- jit_frame->sp = block->frame_sp_begin;
- /* Load results to new block */
- offset = (uint32)(jit_frame->sp - jit_frame->lp);
- for (i = 0; i < block->result_count; i++) {
- switch (block->result_types[i]) {
- case VALUE_TYPE_I32:
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_EXTERNREF:
- case VALUE_TYPE_FUNCREF:
- #endif
- value = gen_load_i32(jit_frame, offset);
- offset++;
- break;
- case VALUE_TYPE_I64:
- value = gen_load_i64(jit_frame, offset);
- offset += 2;
- break;
- case VALUE_TYPE_F32:
- value = gen_load_f32(jit_frame, offset);
- offset++;
- break;
- case VALUE_TYPE_F64:
- value = gen_load_f64(jit_frame, offset);
- offset += 2;
- break;
- default:
- bh_assert(0);
- break;
- }
- PUSH(value, block->result_types[i]);
- }
- return true;
- fail:
- return false;
- }
- static bool
- jit_reg_is_i32_const(JitCompContext *cc, JitReg reg, int32 val)
- {
- return (jit_reg_kind(reg) == JIT_REG_KIND_I32 && jit_reg_is_const(reg)
- && jit_cc_get_const_I32(cc, reg) == val)
- ? true
- : false;
- }
- /**
- * get the last two insns:
- * CMP cmp_reg, r0, r1
- * SELECTcc r2, cmp_reg, 1, 0
- */
- static void
- get_last_cmp_and_selectcc(JitCompContext *cc, JitReg cond, JitInsn **p_insn_cmp,
- JitInsn **p_insn_select)
- {
- JitInsn *insn = jit_basic_block_last_insn(cc->cur_basic_block);
- if (insn && insn->prev && insn->prev->opcode == JIT_OP_CMP
- && insn->opcode >= JIT_OP_SELECTEQ && insn->opcode <= JIT_OP_SELECTLEU
- && *jit_insn_opnd(insn, 0) == cond
- && jit_reg_is_i32_const(cc, *jit_insn_opnd(insn, 2), 1)
- && jit_reg_is_i32_const(cc, *jit_insn_opnd(insn, 3), 0)) {
- *p_insn_cmp = insn->prev;
- *p_insn_select = insn;
- }
- }
- static bool
- push_jit_block_to_stack_and_pass_params(JitCompContext *cc, JitBlock *block,
- JitBasicBlock *basic_block, JitReg cond,
- bool merge_cmp_and_if)
- {
- JitFrame *jit_frame = cc->jit_frame;
- JitValue *value_list_head = NULL, *value_list_end = NULL, *jit_value;
- JitInsn *insn;
- JitReg value;
- uint32 i, param_index, cell_num;
- if (cc->cur_basic_block == basic_block) {
- /* Reuse the current basic block and no need to commit values,
- we just move param values from current block's value stack to
- the new block's value stack */
- for (i = 0; i < block->param_count; i++) {
- jit_value = jit_value_stack_pop(
- &jit_block_stack_top(&cc->block_stack)->value_stack);
- if (!value_list_head) {
- value_list_head = value_list_end = jit_value;
- jit_value->prev = jit_value->next = NULL;
- }
- else {
- jit_value->prev = NULL;
- jit_value->next = value_list_head;
- value_list_head->prev = jit_value;
- value_list_head = jit_value;
- }
- }
- block->value_stack.value_list_head = value_list_head;
- block->value_stack.value_list_end = value_list_end;
- /* Save block's begin frame sp */
- cell_num = wasm_get_cell_num(block->param_types, block->param_count);
- block->frame_sp_begin = jit_frame->sp - cell_num;
- /* Push the new block to block stack */
- jit_block_stack_push(&cc->block_stack, block);
- /* Continue to translate current block */
- }
- else {
- JitInsn *insn_select = NULL, *insn_cmp = NULL;
- if (merge_cmp_and_if) {
- get_last_cmp_and_selectcc(cc, cond, &insn_cmp, &insn_select);
- }
- /* Commit register values to locals and stacks */
- gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
- /* Pop param values from current block's value stack */
- for (i = 0; i < block->param_count; i++) {
- param_index = block->param_count - 1 - i;
- POP(value, block->param_types[param_index]);
- }
- /* Clear frame values */
- clear_values(jit_frame);
- /* Save block's begin frame sp */
- block->frame_sp_begin = jit_frame->sp;
- /* Push the new block to block stack */
- jit_block_stack_push(&cc->block_stack, block);
- if (block->label_type == LABEL_TYPE_LOOP) {
- BUILD_BR(basic_block);
- }
- else {
- /* IF block with condition br insn */
- if (insn_select && insn_cmp) {
- /* Change `CMP + SELECTcc` into `CMP + Bcc` */
- if (!(insn = GEN_INSN(BEQ, cc->cmp_reg,
- jit_basic_block_label(basic_block), 0))) {
- jit_set_last_error(cc, "generate cond br failed");
- goto fail;
- }
- insn->opcode =
- JIT_OP_BEQ + (insn_select->opcode - JIT_OP_SELECTEQ);
- jit_insn_unlink(insn_select);
- jit_insn_delete(insn_select);
- }
- else {
- if (!GEN_INSN(CMP, cc->cmp_reg, cond, NEW_CONST(I32, 0))
- || !(insn =
- GEN_INSN(BNE, cc->cmp_reg,
- jit_basic_block_label(basic_block), 0))) {
- jit_set_last_error(cc, "generate cond br failed");
- goto fail;
- }
- }
- /* Don't create else basic block or end basic block now, just
- save its incoming BNE insn, and patch the insn's else label
- when the basic block is lazily created */
- if (block->wasm_code_else) {
- block->incoming_insn_for_else_bb = insn;
- }
- else {
- if (!jit_block_add_incoming_insn(block, insn, 2)) {
- jit_set_last_error(cc, "add incoming insn failed");
- goto fail;
- }
- }
- }
- /* Start to translate the block */
- SET_BUILDER_POS(basic_block);
- /* Push the block parameters */
- if (!load_block_params(cc, block)) {
- goto fail;
- }
- }
- return true;
- fail:
- return false;
- }
- static void
- copy_block_arities(JitCompContext *cc, JitReg dst_frame_sp, uint8 *dst_types,
- uint32 dst_type_count, JitReg *p_first_res_reg)
- {
- JitFrame *jit_frame;
- uint32 offset_src, offset_dst, i;
- JitReg value;
- jit_frame = cc->jit_frame;
- offset_src = (uint32)(jit_frame->sp - jit_frame->lp)
- - wasm_get_cell_num(dst_types, dst_type_count);
- offset_dst = 0;
- /* pop values from stack and store to dest frame */
- for (i = 0; i < dst_type_count; i++) {
- switch (dst_types[i]) {
- case VALUE_TYPE_I32:
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_EXTERNREF:
- case VALUE_TYPE_FUNCREF:
- #endif
- value = gen_load_i32(jit_frame, offset_src);
- if (i == 0 && p_first_res_reg)
- *p_first_res_reg = value;
- else
- GEN_INSN(STI32, value, dst_frame_sp,
- NEW_CONST(I32, offset_dst * 4));
- offset_src++;
- offset_dst++;
- break;
- case VALUE_TYPE_I64:
- value = gen_load_i64(jit_frame, offset_src);
- if (i == 0 && p_first_res_reg)
- *p_first_res_reg = value;
- else
- GEN_INSN(STI64, value, dst_frame_sp,
- NEW_CONST(I32, offset_dst * 4));
- offset_src += 2;
- offset_dst += 2;
- break;
- case VALUE_TYPE_F32:
- value = gen_load_f32(jit_frame, offset_src);
- if (i == 0 && p_first_res_reg)
- *p_first_res_reg = value;
- else
- GEN_INSN(STF32, value, dst_frame_sp,
- NEW_CONST(I32, offset_dst * 4));
- offset_src++;
- offset_dst++;
- break;
- case VALUE_TYPE_F64:
- value = gen_load_f64(jit_frame, offset_src);
- if (i == 0 && p_first_res_reg)
- *p_first_res_reg = value;
- else
- GEN_INSN(STF64, value, dst_frame_sp,
- NEW_CONST(I32, offset_dst * 4));
- offset_src += 2;
- offset_dst += 2;
- break;
- default:
- bh_assert(0);
- break;
- }
- }
- }
- static bool
- handle_func_return(JitCompContext *cc, JitBlock *block)
- {
- JitReg prev_frame, prev_frame_sp;
- JitReg ret_reg = 0;
- #if WASM_ENABLE_PERF_PROFILING != 0
- JitReg func_inst = jit_cc_new_reg_ptr(cc);
- JitReg time_start = jit_cc_new_reg_I64(cc);
- JitReg time_end = jit_cc_new_reg_I64(cc);
- JitReg cur_exec_time = jit_cc_new_reg_I64(cc);
- JitReg total_exec_time = jit_cc_new_reg_I64(cc);
- JitReg total_exec_cnt = jit_cc_new_reg_I32(cc);
- #endif
- #if WASM_ENABLE_PERF_PROFILING != 0
- /* time_end = os_time_thread_cputime_us() */
- if (!jit_emit_callnative(cc, os_time_thread_cputime_us, time_end, NULL,
- 0)) {
- return false;
- }
- /* time_start = cur_frame->time_started */
- GEN_INSN(LDI64, time_start, cc->fp_reg,
- NEW_CONST(I32, offsetof(WASMInterpFrame, time_started)));
- /* cur_exec_time = time_end - time_start */
- GEN_INSN(SUB, cur_exec_time, time_end, time_start);
- /* func_inst = cur_frame->function */
- GEN_INSN(LDPTR, func_inst, cc->fp_reg,
- NEW_CONST(I32, offsetof(WASMInterpFrame, function)));
- /* total_exec_time = func_inst->total_exec_time */
- GEN_INSN(LDI64, total_exec_time, func_inst,
- NEW_CONST(I32, offsetof(WASMFunctionInstance, total_exec_time)));
- /* total_exec_time += cur_exec_time */
- GEN_INSN(ADD, total_exec_time, total_exec_time, cur_exec_time);
- /* func_inst->total_exec_time = total_exec_time */
- GEN_INSN(STI64, total_exec_time, func_inst,
- NEW_CONST(I32, offsetof(WASMFunctionInstance, total_exec_time)));
- /* totoal_exec_cnt = func_inst->total_exec_cnt */
- GEN_INSN(LDI32, total_exec_cnt, func_inst,
- NEW_CONST(I32, offsetof(WASMFunctionInstance, total_exec_cnt)));
- /* total_exec_cnt++ */
- GEN_INSN(ADD, total_exec_cnt, total_exec_cnt, NEW_CONST(I32, 1));
- /* func_inst->total_exec_cnt = total_exec_cnt */
- GEN_INSN(STI32, total_exec_cnt, func_inst,
- NEW_CONST(I32, offsetof(WASMFunctionInstance, total_exec_cnt)));
- #endif
- prev_frame = jit_cc_new_reg_ptr(cc);
- prev_frame_sp = jit_cc_new_reg_ptr(cc);
- /* prev_frame = cur_frame->prev_frame */
- GEN_INSN(LDPTR, prev_frame, cc->fp_reg,
- NEW_CONST(I32, offsetof(WASMInterpFrame, prev_frame)));
- GEN_INSN(LDPTR, prev_frame_sp, prev_frame,
- NEW_CONST(I32, offsetof(WASMInterpFrame, sp)));
- if (block->result_count) {
- uint32 cell_num =
- wasm_get_cell_num(block->result_types, block->result_count);
- copy_block_arities(cc, prev_frame_sp, block->result_types,
- block->result_count, &ret_reg);
- /* prev_frame->sp += cell_num */
- GEN_INSN(ADD, prev_frame_sp, prev_frame_sp,
- NEW_CONST(PTR, cell_num * 4));
- GEN_INSN(STPTR, prev_frame_sp, prev_frame,
- NEW_CONST(I32, offsetof(WASMInterpFrame, sp)));
- }
- /* Free stack space of the current frame:
- exec_env->wasm_stack.top = cur_frame */
- GEN_INSN(STPTR, cc->fp_reg, cc->exec_env_reg,
- NEW_CONST(I32, offsetof(WASMExecEnv, wasm_stack.top)));
- /* Set the prev_frame as the current frame:
- exec_env->cur_frame = prev_frame */
- GEN_INSN(STPTR, prev_frame, cc->exec_env_reg,
- NEW_CONST(I32, offsetof(WASMExecEnv, cur_frame)));
- /* fp_reg = prev_frame */
- GEN_INSN(MOV, cc->fp_reg, prev_frame);
- /* return 0 */
- GEN_INSN(RETURNBC, NEW_CONST(I32, JIT_INTERP_ACTION_NORMAL), ret_reg, 0);
- return true;
- }
- /**
- * is_block_polymorphic: whether current block's stack is in polymorphic state,
- * if the opcode is one of unreachable/br/br_table/return, stack is marked
- * to polymorphic state until the block's 'end' opcode is processed
- */
- static bool
- handle_op_end(JitCompContext *cc, uint8 **p_frame_ip, bool is_block_polymorphic)
- {
- JitFrame *jit_frame = cc->jit_frame;
- JitBlock *block, *block_prev;
- JitIncomingInsn *incoming_insn;
- JitInsn *insn;
- /* Check block stack */
- if (!(block = jit_block_stack_top(&cc->block_stack))) {
- jit_set_last_error(cc, "WASM block stack underflow");
- return false;
- }
- if (!block->incoming_insns_for_end_bb) {
- /* No other basic blocks jumping to this end, no need to
- create the end basic block, just continue to translate
- the following opcodes */
- if (block->label_type == LABEL_TYPE_FUNCTION) {
- if (!handle_func_return(cc, block)) {
- return false;
- }
- SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
- clear_values(jit_frame);
- }
- else if (block->result_count > 0) {
- JitValue *value_list_head = NULL, *value_list_end = NULL;
- JitValue *jit_value;
- uint32 i;
- /* No need to change cc->jit_frame, just move result values
- from current block's value stack to previous block's
- value stack */
- block_prev = block->prev;
- for (i = 0; i < block->result_count; i++) {
- jit_value = jit_value_stack_pop(&block->value_stack);
- bh_assert(jit_value);
- if (!value_list_head) {
- value_list_head = value_list_end = jit_value;
- jit_value->prev = jit_value->next = NULL;
- }
- else {
- jit_value->prev = NULL;
- jit_value->next = value_list_head;
- value_list_head->prev = jit_value;
- value_list_head = jit_value;
- }
- }
- if (!block_prev->value_stack.value_list_head) {
- block_prev->value_stack.value_list_head = value_list_head;
- block_prev->value_stack.value_list_end = value_list_end;
- }
- else {
- /* Link to the end of previous block's value stack */
- block_prev->value_stack.value_list_end->next = value_list_head;
- value_list_head->prev = block_prev->value_stack.value_list_end;
- block_prev->value_stack.value_list_end = value_list_end;
- }
- }
- /* Pop block and destroy the block */
- block = jit_block_stack_pop(&cc->block_stack);
- jit_block_destroy(block);
- return true;
- }
- else {
- /* Commit register values to locals and stacks */
- gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
- /* Clear frame values */
- clear_values(jit_frame);
- /* Create the end basic block */
- CREATE_BASIC_BLOCK(block->basic_block_end);
- SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
- SET_BB_BEGIN_BCIP(block->basic_block_end, *p_frame_ip);
- /* No need to create 'JMP' insn if block is in stack polymorphic
- state, as previous br/br_table opcode has created 'JMP' insn
- to this end basic block */
- if (!is_block_polymorphic) {
- /* Jump to the end basic block */
- BUILD_BR(block->basic_block_end);
- }
- /* Patch the INSNs which jump to this basic block */
- incoming_insn = block->incoming_insns_for_end_bb;
- while (incoming_insn) {
- insn = incoming_insn->insn;
- bh_assert(
- insn->opcode == JIT_OP_JMP
- || (insn->opcode >= JIT_OP_BEQ && insn->opcode <= JIT_OP_BLEU)
- || insn->opcode == JIT_OP_LOOKUPSWITCH);
- if (insn->opcode == JIT_OP_JMP
- || (insn->opcode >= JIT_OP_BEQ
- && insn->opcode <= JIT_OP_BLEU)) {
- *(jit_insn_opnd(insn, incoming_insn->opnd_idx)) =
- jit_basic_block_label(block->basic_block_end);
- }
- else {
- /* Patch LOOKUPSWITCH INSN */
- JitOpndLookupSwitch *opnd = jit_insn_opndls(insn);
- if (incoming_insn->opnd_idx < opnd->match_pairs_num) {
- opnd->match_pairs[incoming_insn->opnd_idx].target =
- jit_basic_block_label(block->basic_block_end);
- }
- else {
- opnd->default_target =
- jit_basic_block_label(block->basic_block_end);
- }
- }
- incoming_insn = incoming_insn->next;
- }
- SET_BUILDER_POS(block->basic_block_end);
- /* Pop block and load block results */
- block = jit_block_stack_pop(&cc->block_stack);
- if (block->label_type == LABEL_TYPE_FUNCTION) {
- if (!handle_func_return(cc, block)) {
- jit_block_destroy(block);
- goto fail;
- }
- SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
- clear_values(jit_frame);
- }
- else {
- if (!load_block_results(cc, block)) {
- jit_block_destroy(block);
- goto fail;
- }
- }
- jit_block_destroy(block);
- return true;
- }
- return true;
- fail:
- return false;
- }
- /**
- * is_block_polymorphic: whether current block's stack is in polymorphic state,
- * if the opcode is one of unreachable/br/br_table/return, stack is marked
- * to polymorphic state until the block's 'end' opcode is processed
- */
- static bool
- handle_op_else(JitCompContext *cc, uint8 **p_frame_ip,
- bool is_block_polymorphic)
- {
- JitBlock *block = jit_block_stack_top(&cc->block_stack);
- JitFrame *jit_frame = cc->jit_frame;
- JitInsn *insn;
- /* Check block */
- if (!block) {
- jit_set_last_error(cc, "WASM block stack underflow");
- return false;
- }
- if (block->label_type != LABEL_TYPE_IF) {
- jit_set_last_error(cc, "Invalid WASM block type");
- return false;
- }
- if (!block->incoming_insn_for_else_bb) {
- /* The if branch is handled like OP_BLOCK (cond is const and != 0),
- just skip the else branch and handle OP_END */
- *p_frame_ip = block->wasm_code_end + 1;
- return handle_op_end(cc, p_frame_ip, false);
- }
- else {
- /* Has else branch and need to translate else branch */
- /* Commit register values to locals and stacks */
- gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
- /* Clear frame values */
- clear_values(jit_frame);
- /* No need to create 'JMP' insn if block is in stack polymorphic
- state, as previous br/br_table opcode has created 'JMP' insn
- to this end basic block */
- if (!is_block_polymorphic) {
- /* Jump to end basic block */
- if (!(insn = GEN_INSN(JMP, 0))) {
- jit_set_last_error(cc, "generate jmp insn failed");
- return false;
- }
- if (!jit_block_add_incoming_insn(block, insn, 0)) {
- jit_set_last_error(cc, "add incoming insn failed");
- return false;
- }
- }
- /* Clear value stack, restore param values and
- start to translate the else branch. */
- jit_value_stack_destroy(&block->value_stack);
- /* create else basic block */
- CREATE_BASIC_BLOCK(block->basic_block_else);
- SET_BB_END_BCIP(block->basic_block_entry, *p_frame_ip - 1);
- SET_BB_BEGIN_BCIP(block->basic_block_else, *p_frame_ip);
- /* Patch the insn which conditionly jumps to the else basic block */
- insn = block->incoming_insn_for_else_bb;
- *(jit_insn_opnd(insn, 2)) =
- jit_basic_block_label(block->basic_block_else);
- SET_BUILDER_POS(block->basic_block_else);
- /* Reload block parameters */
- if (!load_block_params(cc, block)) {
- return false;
- }
- return true;
- }
- return true;
- fail:
- return false;
- }
- static bool
- handle_next_reachable_block(JitCompContext *cc, uint8 **p_frame_ip)
- {
- JitBlock *block = jit_block_stack_top(&cc->block_stack);
- bh_assert(block);
- do {
- if (block->label_type == LABEL_TYPE_IF
- && block->incoming_insn_for_else_bb
- && *p_frame_ip <= block->wasm_code_else) {
- /* Else branch hasn't been translated,
- start to translate the else branch */
- *p_frame_ip = block->wasm_code_else + 1;
- /* Restore jit frame's sp to block's sp begin */
- cc->jit_frame->sp = block->frame_sp_begin;
- return handle_op_else(cc, p_frame_ip, true);
- }
- else if (block->incoming_insns_for_end_bb) {
- *p_frame_ip = block->wasm_code_end + 1;
- /* Restore jit frame's sp to block's sp end */
- cc->jit_frame->sp =
- block->frame_sp_begin
- + wasm_get_cell_num(block->result_types, block->result_count);
- return handle_op_end(cc, p_frame_ip, true);
- }
- else {
- *p_frame_ip = block->wasm_code_end + 1;
- jit_block_stack_pop(&cc->block_stack);
- jit_block_destroy(block);
- block = jit_block_stack_top(&cc->block_stack);
- }
- } while (block != NULL);
- return true;
- }
- bool
- jit_compile_op_block(JitCompContext *cc, uint8 **p_frame_ip,
- uint8 *frame_ip_end, uint32 label_type, uint32 param_count,
- uint8 *param_types, uint32 result_count,
- uint8 *result_types, bool merge_cmp_and_if)
- {
- BlockAddr block_addr_cache[BLOCK_ADDR_CACHE_SIZE][BLOCK_ADDR_CONFLICT_SIZE];
- JitBlock *block;
- JitReg value;
- uint8 *else_addr, *end_addr;
- /* Check block stack */
- if (!jit_block_stack_top(&cc->block_stack)) {
- jit_set_last_error(cc, "WASM block stack underflow");
- return false;
- }
- memset(block_addr_cache, 0, sizeof(block_addr_cache));
- /* Get block info */
- if (!(wasm_loader_find_block_addr(
- NULL, (BlockAddr *)block_addr_cache, *p_frame_ip, frame_ip_end,
- (uint8)label_type, &else_addr, &end_addr))) {
- jit_set_last_error(cc, "find block end addr failed");
- return false;
- }
- /* Allocate memory */
- if (!(block = jit_calloc(sizeof(JitBlock)))) {
- jit_set_last_error(cc, "allocate memory failed");
- return false;
- }
- if (param_count && !(block->param_types = jit_calloc(param_count))) {
- jit_set_last_error(cc, "allocate memory failed");
- goto fail;
- }
- if (result_count && !(block->result_types = jit_calloc(result_count))) {
- jit_set_last_error(cc, "allocate memory failed");
- goto fail;
- }
- /* Initialize block data */
- block->label_type = label_type;
- block->param_count = param_count;
- if (param_count) {
- bh_memcpy_s(block->param_types, param_count, param_types, param_count);
- }
- block->result_count = result_count;
- if (result_count) {
- bh_memcpy_s(block->result_types, result_count, result_types,
- result_count);
- }
- block->wasm_code_else = else_addr;
- block->wasm_code_end = end_addr;
- if (label_type == LABEL_TYPE_BLOCK) {
- /* Push the new jit block to block stack and continue to
- translate current basic block */
- if (!push_jit_block_to_stack_and_pass_params(
- cc, block, cc->cur_basic_block, 0, false))
- goto fail;
- }
- else if (label_type == LABEL_TYPE_LOOP) {
- CREATE_BASIC_BLOCK(block->basic_block_entry);
- SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
- SET_BB_BEGIN_BCIP(block->basic_block_entry, *p_frame_ip);
- /* Push the new jit block to block stack and continue to
- translate the new basic block */
- if (!push_jit_block_to_stack_and_pass_params(
- cc, block, block->basic_block_entry, 0, false))
- goto fail;
- }
- else if (label_type == LABEL_TYPE_IF) {
- POP_I32(value);
- if (!jit_reg_is_const(value)) {
- /* Compare value is not constant, create condition br IR */
- /* Create entry block */
- CREATE_BASIC_BLOCK(block->basic_block_entry);
- SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
- SET_BB_BEGIN_BCIP(block->basic_block_entry, *p_frame_ip);
- if (!push_jit_block_to_stack_and_pass_params(
- cc, block, block->basic_block_entry, value,
- merge_cmp_and_if))
- goto fail;
- }
- else {
- if (jit_cc_get_const_I32(cc, value) != 0) {
- /* Compare value is not 0, condition is true, else branch of
- BASIC_BLOCK if cannot be reached, we treat it same as
- LABEL_TYPE_BLOCK and start to translate if branch */
- if (!push_jit_block_to_stack_and_pass_params(
- cc, block, cc->cur_basic_block, 0, false))
- goto fail;
- }
- else {
- if (else_addr) {
- /* Compare value is not 0, condition is false, if branch of
- BASIC_BLOCK if cannot be reached, we treat it same as
- LABEL_TYPE_BLOCK and start to translate else branch */
- if (!push_jit_block_to_stack_and_pass_params(
- cc, block, cc->cur_basic_block, 0, false))
- goto fail;
- *p_frame_ip = else_addr + 1;
- }
- else {
- /* The whole if block cannot be reached, skip it */
- jit_block_destroy(block);
- *p_frame_ip = end_addr + 1;
- }
- }
- }
- }
- else {
- jit_set_last_error(cc, "Invalid block type");
- goto fail;
- }
- return true;
- fail:
- /* Only destroy the block if it hasn't been pushed into
- the block stack, or if will be destroyed again when
- destroying the block stack */
- if (jit_block_stack_top(&cc->block_stack) != block)
- jit_block_destroy(block);
- return false;
- }
- bool
- jit_compile_op_else(JitCompContext *cc, uint8 **p_frame_ip)
- {
- return handle_op_else(cc, p_frame_ip, false);
- }
- bool
- jit_compile_op_end(JitCompContext *cc, uint8 **p_frame_ip)
- {
- return handle_op_end(cc, p_frame_ip, false);
- }
- /* Check whether need to copy arities when jumping from current block
- to the dest block */
- static bool
- check_copy_arities(const JitBlock *block_dst, JitFrame *jit_frame)
- {
- JitValueSlot *frame_sp_src = NULL;
- if (block_dst->label_type == LABEL_TYPE_LOOP) {
- frame_sp_src =
- jit_frame->sp
- - wasm_get_cell_num(block_dst->param_types, block_dst->param_count);
- /* There are parameters to copy and the src/dst addr are different */
- return (block_dst->param_count > 0
- && block_dst->frame_sp_begin != frame_sp_src)
- ? true
- : false;
- }
- else {
- frame_sp_src = jit_frame->sp
- - wasm_get_cell_num(block_dst->result_types,
- block_dst->result_count);
- /* There are results to copy and the src/dst addr are different */
- return (block_dst->result_count > 0
- && block_dst->frame_sp_begin != frame_sp_src)
- ? true
- : false;
- }
- }
- #if WASM_ENABLE_THREAD_MGR != 0
- bool
- jit_check_suspend_flags(JitCompContext *cc)
- {
- JitReg exec_env, suspend_flags, terminate_flag, offset;
- JitBasicBlock *terminate_block, *cur_basic_block;
- JitFrame *jit_frame = cc->jit_frame;
- cur_basic_block = cc->cur_basic_block;
- terminate_block = jit_cc_new_basic_block(cc, 0);
- if (!terminate_block) {
- return false;
- }
- gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
- exec_env = cc->exec_env_reg;
- suspend_flags = jit_cc_new_reg_I32(cc);
- terminate_flag = jit_cc_new_reg_I32(cc);
- offset = jit_cc_new_const_I32(cc, offsetof(WASMExecEnv, suspend_flags));
- GEN_INSN(LDI32, suspend_flags, exec_env, offset);
- GEN_INSN(AND, terminate_flag, suspend_flags, NEW_CONST(I32, 1));
- GEN_INSN(CMP, cc->cmp_reg, terminate_flag, NEW_CONST(I32, 0));
- GEN_INSN(BNE, cc->cmp_reg, jit_basic_block_label(terminate_block), 0);
- cc->cur_basic_block = terminate_block;
- GEN_INSN(RETURN, NEW_CONST(I32, 0));
- cc->cur_basic_block = cur_basic_block;
- return true;
- }
- #endif
- static bool
- handle_op_br(JitCompContext *cc, uint32 br_depth, uint8 **p_frame_ip)
- {
- JitFrame *jit_frame;
- JitBlock *block_dst, *block;
- JitReg frame_sp_dst;
- JitInsn *insn;
- bool copy_arities;
- uint32 offset;
- /* Check block stack */
- if (!(block = jit_block_stack_top(&cc->block_stack))) {
- jit_set_last_error(cc, "WASM block stack underflow");
- return false;
- }
- if (!(block_dst = get_target_block(cc, br_depth))) {
- return false;
- }
- jit_frame = cc->jit_frame;
- /* Only opy parameters or results when their count > 0 and
- the src/dst addr are different */
- copy_arities = check_copy_arities(block_dst, jit_frame);
- if (copy_arities) {
- frame_sp_dst = jit_cc_new_reg_ptr(cc);
- offset = offsetof(WASMInterpFrame, lp)
- + (block_dst->frame_sp_begin - jit_frame->lp) * 4;
- GEN_INSN(ADD, frame_sp_dst, cc->fp_reg, NEW_CONST(PTR, offset));
- /* No need to commit results as they will be copied to dest block */
- gen_commit_values(jit_frame, jit_frame->lp, block->frame_sp_begin);
- }
- else {
- /* Commit all including results as they won't be copied */
- gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
- }
- if (block_dst->label_type == LABEL_TYPE_LOOP) {
- if (copy_arities) {
- /* Dest block is Loop block, copy loop parameters */
- copy_block_arities(cc, frame_sp_dst, block_dst->param_types,
- block_dst->param_count, NULL);
- }
- clear_values(jit_frame);
- /* Jump to the begin basic block */
- BUILD_BR(block_dst->basic_block_entry);
- SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
- }
- else {
- if (copy_arities) {
- /* Dest block is Block/If/Function block, copy block results */
- copy_block_arities(cc, frame_sp_dst, block_dst->result_types,
- block_dst->result_count, NULL);
- }
- clear_values(jit_frame);
- /* Jump to the end basic block */
- if (!(insn = GEN_INSN(JMP, 0))) {
- jit_set_last_error(cc, "generate jmp insn failed");
- goto fail;
- }
- if (!jit_block_add_incoming_insn(block_dst, insn, 0)) {
- jit_set_last_error(cc, "add incoming insn failed");
- goto fail;
- }
- SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
- }
- return true;
- fail:
- return false;
- }
- bool
- jit_compile_op_br(JitCompContext *cc, uint32 br_depth, uint8 **p_frame_ip)
- {
- #if WASM_ENABLE_THREAD_MGR != 0
- /* Insert suspend check point */
- if (!jit_check_suspend_flags(cc))
- return false;
- #endif
- return handle_op_br(cc, br_depth, p_frame_ip)
- && handle_next_reachable_block(cc, p_frame_ip);
- }
- static JitFrame *
- jit_frame_clone(const JitFrame *jit_frame)
- {
- JitFrame *jit_frame_cloned;
- uint32 max_locals = jit_frame->max_locals;
- uint32 max_stacks = jit_frame->max_stacks;
- uint32 total_size;
- total_size = (uint32)(offsetof(JitFrame, lp)
- + sizeof(*jit_frame->lp) * (max_locals + max_stacks));
- jit_frame_cloned = jit_calloc(total_size);
- if (jit_frame_cloned) {
- bh_memcpy_s(jit_frame_cloned, total_size, jit_frame, total_size);
- jit_frame_cloned->sp =
- jit_frame_cloned->lp + (jit_frame->sp - jit_frame->lp);
- }
- return jit_frame_cloned;
- }
- static void
- jit_frame_copy(JitFrame *jit_frame_dst, const JitFrame *jit_frame_src)
- {
- uint32 max_locals = jit_frame_src->max_locals;
- uint32 max_stacks = jit_frame_src->max_stacks;
- uint32 total_size;
- total_size =
- (uint32)(offsetof(JitFrame, lp)
- + sizeof(*jit_frame_src->lp) * (max_locals + max_stacks));
- bh_memcpy_s(jit_frame_dst, total_size, jit_frame_src, total_size);
- jit_frame_dst->sp =
- jit_frame_dst->lp + (jit_frame_src->sp - jit_frame_src->lp);
- }
- bool
- jit_compile_op_br_if(JitCompContext *cc, uint32 br_depth,
- bool merge_cmp_and_br_if, uint8 **p_frame_ip)
- {
- JitFrame *jit_frame, *jit_frame_cloned;
- JitBlock *block_dst;
- JitReg cond;
- JitBasicBlock *cur_basic_block, *if_basic_block = NULL;
- JitInsn *insn, *insn_select = NULL, *insn_cmp = NULL;
- bool copy_arities;
- if (!(block_dst = get_target_block(cc, br_depth))) {
- return false;
- }
- /* append IF to current basic block */
- POP_I32(cond);
- if (merge_cmp_and_br_if) {
- get_last_cmp_and_selectcc(cc, cond, &insn_cmp, &insn_select);
- }
- jit_frame = cc->jit_frame;
- cur_basic_block = cc->cur_basic_block;
- gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
- if (!(insn_select && insn_cmp)) {
- if (!GEN_INSN(CMP, cc->cmp_reg, cond, NEW_CONST(I32, 0))) {
- jit_set_last_error(cc, "generate cmp insn failed");
- goto fail;
- }
- }
- /* Only copy parameters or results when their count > 0 and
- the src/dst addr are different */
- copy_arities = check_copy_arities(block_dst, jit_frame);
- if (!copy_arities) {
- if (block_dst->label_type == LABEL_TYPE_LOOP) {
- if (!(insn = GEN_INSN(
- BNE, cc->cmp_reg,
- jit_basic_block_label(block_dst->basic_block_entry),
- 0))) {
- jit_set_last_error(cc, "generate bne insn failed");
- goto fail;
- }
- }
- else {
- if (!(insn = GEN_INSN(BNE, cc->cmp_reg, 0, 0))) {
- jit_set_last_error(cc, "generate bne insn failed");
- goto fail;
- }
- if (!jit_block_add_incoming_insn(block_dst, insn, 1)) {
- jit_set_last_error(cc, "add incoming insn failed");
- goto fail;
- }
- }
- if (insn_select && insn_cmp) {
- /* Change `CMP + SELECTcc` into `CMP + Bcc` */
- insn->opcode = JIT_OP_BEQ + (insn_select->opcode - JIT_OP_SELECTEQ);
- jit_insn_unlink(insn_select);
- jit_insn_delete(insn_select);
- }
- return true;
- }
- CREATE_BASIC_BLOCK(if_basic_block);
- if (!(insn = GEN_INSN(BNE, cc->cmp_reg,
- jit_basic_block_label(if_basic_block), 0))) {
- jit_set_last_error(cc, "generate bne insn failed");
- goto fail;
- }
- if (insn_select && insn_cmp) {
- /* Change `CMP + SELECTcc` into `CMP + Bcc` */
- insn->opcode = JIT_OP_BEQ + (insn_select->opcode - JIT_OP_SELECTEQ);
- jit_insn_unlink(insn_select);
- jit_insn_delete(insn_select);
- }
- #if WASM_ENABLE_THREAD_MGR != 0
- /* Insert suspend check point */
- if (!jit_check_suspend_flags(cc))
- return false;
- #endif
- SET_BUILDER_POS(if_basic_block);
- SET_BB_BEGIN_BCIP(if_basic_block, *p_frame_ip - 1);
- /* Clone current jit frame to a new jit fame */
- if (!(jit_frame_cloned = jit_frame_clone(jit_frame))) {
- jit_set_last_error(cc, "allocate memory failed");
- goto fail;
- }
- /* Clear current jit frame so that the registers
- in the new basic block will be loaded again */
- clear_values(jit_frame);
- if (!handle_op_br(cc, br_depth, p_frame_ip)) {
- jit_free(jit_frame_cloned);
- goto fail;
- }
- /* Restore the jit frame so that the registers can
- be used again in current basic block */
- jit_frame_copy(jit_frame, jit_frame_cloned);
- jit_free(jit_frame_cloned);
- /* Continue processing opcodes after BR_IF */
- SET_BUILDER_POS(cur_basic_block);
- return true;
- fail:
- return false;
- }
- bool
- jit_compile_op_br_table(JitCompContext *cc, uint32 *br_depths, uint32 br_count,
- uint8 **p_frame_ip)
- {
- JitBasicBlock *cur_basic_block;
- JitReg value;
- JitInsn *insn;
- uint32 i = 0;
- JitOpndLookupSwitch *opnd = NULL;
- #if WASM_ENABLE_THREAD_MGR != 0
- /* Insert suspend check point */
- if (!jit_check_suspend_flags(cc))
- return false;
- #endif
- cur_basic_block = cc->cur_basic_block;
- POP_I32(value);
- /* append LOOKUPSWITCH to current basic block */
- gen_commit_values(cc->jit_frame, cc->jit_frame->lp, cc->jit_frame->sp);
- /* Clear frame values */
- clear_values(cc->jit_frame);
- SET_BB_END_BCIP(cur_basic_block, *p_frame_ip - 1);
- /* prepare basic blocks for br */
- insn = GEN_INSN(LOOKUPSWITCH, value, br_count);
- if (NULL == insn) {
- jit_set_last_error(cc, "generate insn LOOKUPSWITCH failed");
- goto fail;
- }
- for (i = 0, opnd = jit_insn_opndls(insn); i < br_count + 1; i++) {
- JitBasicBlock *basic_block = NULL;
- JitBlock *block_dst;
- bool copy_arities;
- if (!(block_dst = get_target_block(cc, br_depths[i]))) {
- goto fail;
- }
- /* Only opy parameters or results when their count > 0 and
- the src/dst addr are different */
- copy_arities = check_copy_arities(block_dst, cc->jit_frame);
- if (!copy_arities) {
- /* No need to create new basic block, direclty jump to
- the existing basic block when no need to copy arities */
- if (i == br_count) {
- if (block_dst->label_type == LABEL_TYPE_LOOP) {
- opnd->default_target =
- jit_basic_block_label(block_dst->basic_block_entry);
- }
- else {
- bh_assert(!block_dst->basic_block_end);
- if (!jit_block_add_incoming_insn(block_dst, insn, i)) {
- jit_set_last_error(cc, "add incoming insn failed");
- goto fail;
- }
- }
- }
- else {
- opnd->match_pairs[i].value = i;
- if (block_dst->label_type == LABEL_TYPE_LOOP) {
- opnd->match_pairs[i].target =
- jit_basic_block_label(block_dst->basic_block_entry);
- }
- else {
- bh_assert(!block_dst->basic_block_end);
- if (!jit_block_add_incoming_insn(block_dst, insn, i)) {
- jit_set_last_error(cc, "add incoming insn failed");
- goto fail;
- }
- }
- }
- continue;
- }
- /* Create new basic block when need to copy arities */
- CREATE_BASIC_BLOCK(basic_block);
- SET_BB_BEGIN_BCIP(basic_block, *p_frame_ip - 1);
- if (i == br_count) {
- opnd->default_target = jit_basic_block_label(basic_block);
- }
- else {
- opnd->match_pairs[i].value = i;
- opnd->match_pairs[i].target = jit_basic_block_label(basic_block);
- }
- SET_BUILDER_POS(basic_block);
- if (!handle_op_br(cc, br_depths[i], p_frame_ip))
- goto fail;
- }
- /* Search next available block to handle */
- return handle_next_reachable_block(cc, p_frame_ip);
- fail:
- return false;
- }
- bool
- jit_compile_op_return(JitCompContext *cc, uint8 **p_frame_ip)
- {
- JitBlock *block_func = cc->block_stack.block_list_head;
- bh_assert(block_func);
- if (!handle_func_return(cc, block_func)) {
- return false;
- }
- SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
- clear_values(cc->jit_frame);
- return handle_next_reachable_block(cc, p_frame_ip);
- }
- bool
- jit_compile_op_unreachable(JitCompContext *cc, uint8 **p_frame_ip)
- {
- if (!jit_emit_exception(cc, EXCE_UNREACHABLE, JIT_OP_JMP, 0, NULL))
- return false;
- return handle_next_reachable_block(cc, p_frame_ip);
- }
- bool
- jit_handle_next_reachable_block(JitCompContext *cc, uint8 **p_frame_ip)
- {
- return handle_next_reachable_block(cc, p_frame_ip);
- }
|