| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209 |
- /*
- * 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_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(cc, 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 void
- handle_func_return(JitCompContext *cc, JitBlock *block)
- {
- JitReg prev_frame, prev_frame_sp;
- JitReg ret_reg = 0;
- 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.s.top = cur_frame */
- GEN_INSN(STPTR, cc->fp_reg, cc->exec_env_reg,
- NEW_CONST(I32, offsetof(WASMExecEnv, wasm_stack.s.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);
- }
- /**
- * 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) {
- handle_func_return(cc, block);
- 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) {
- handle_func_return(cc, block);
- 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_val(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:
- 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;
- }
- }
- 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)
- {
- 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 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) {
- 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);
- }
- 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;
- 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);
- handle_func_return(cc, block_func);
- 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, JIT_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);
- }
|