jit_emit_control.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "jit_emit_control.h"
  6. #include "jit_emit_exception.h"
  7. #include "jit_emit_function.h"
  8. #include "../jit_frontend.h"
  9. #include "../interpreter/wasm_loader.h"
  10. #define CREATE_BASIC_BLOCK(new_basic_block) \
  11. do { \
  12. bh_assert(!new_basic_block); \
  13. if (!(new_basic_block = jit_cc_new_basic_block(cc, 0))) { \
  14. jit_set_last_error(cc, "create basic block failed"); \
  15. goto fail; \
  16. } \
  17. } while (0)
  18. #define CURR_BASIC_BLOCK() cc->cur_basic_block
  19. #define BUILD_BR(target_block) \
  20. do { \
  21. if (!GEN_INSN(JMP, jit_basic_block_label(target_block))) { \
  22. jit_set_last_error(cc, "generate jmp insn failed"); \
  23. goto fail; \
  24. } \
  25. } while (0)
  26. #define BUILD_COND_BR(value_if, block_then, block_else) \
  27. do { \
  28. if (!GEN_INSN(CMP, cc->cmp_reg, value_if, NEW_CONST(I32, 0)) \
  29. || !GEN_INSN(BNE, cc->cmp_reg, jit_basic_block_label(block_then), \
  30. jit_basic_block_label(block_else))) { \
  31. jit_set_last_error(cc, "generate bne insn failed"); \
  32. goto fail; \
  33. } \
  34. } while (0)
  35. #define SET_BUILDER_POS(basic_block) \
  36. do { \
  37. cc->cur_basic_block = basic_block; \
  38. } while (0)
  39. #define SET_BB_BEGIN_BCIP(basic_block, bcip) \
  40. do { \
  41. *(jit_annl_begin_bcip(cc, jit_basic_block_label(basic_block))) = bcip; \
  42. } while (0)
  43. #define SET_BB_END_BCIP(basic_block, bcip) \
  44. do { \
  45. *(jit_annl_end_bcip(cc, jit_basic_block_label(basic_block))) = bcip; \
  46. } while (0)
  47. static JitBlock *
  48. get_target_block(JitCompContext *cc, uint32 br_depth)
  49. {
  50. uint32 i = br_depth;
  51. JitBlock *block = jit_block_stack_top(&cc->block_stack);
  52. while (i-- > 0 && block) {
  53. block = block->prev;
  54. }
  55. if (!block) {
  56. jit_set_last_error(cc, "WASM block stack underflow");
  57. return NULL;
  58. }
  59. return block;
  60. }
  61. static bool
  62. load_block_params(JitCompContext *cc, JitBlock *block)
  63. {
  64. JitFrame *jit_frame = cc->jit_frame;
  65. uint32 offset, i;
  66. JitReg value = 0;
  67. /* Clear jit frame's locals and stacks */
  68. clear_values(jit_frame);
  69. /* Restore jit frame's sp to block's sp begin */
  70. jit_frame->sp = block->frame_sp_begin;
  71. /* Load params to new block */
  72. offset = (uint32)(jit_frame->sp - jit_frame->lp);
  73. for (i = 0; i < block->param_count; i++) {
  74. switch (block->param_types[i]) {
  75. case VALUE_TYPE_I32:
  76. #if WASM_ENABLE_REF_TYPES != 0
  77. case VALUE_TYPE_EXTERNREF:
  78. case VALUE_TYPE_FUNCREF:
  79. #endif
  80. value = gen_load_i32(jit_frame, offset);
  81. offset++;
  82. break;
  83. case VALUE_TYPE_I64:
  84. value = gen_load_i64(jit_frame, offset);
  85. offset += 2;
  86. break;
  87. case VALUE_TYPE_F32:
  88. value = gen_load_f32(jit_frame, offset);
  89. offset++;
  90. break;
  91. case VALUE_TYPE_F64:
  92. value = gen_load_f64(jit_frame, offset);
  93. offset += 2;
  94. break;
  95. default:
  96. bh_assert(0);
  97. break;
  98. }
  99. PUSH(value, block->param_types[i]);
  100. }
  101. return true;
  102. fail:
  103. return false;
  104. }
  105. static bool
  106. load_block_results(JitCompContext *cc, JitBlock *block)
  107. {
  108. JitFrame *jit_frame = cc->jit_frame;
  109. uint32 offset, i;
  110. JitReg value = 0;
  111. /* Restore jit frame's sp to block's sp begin */
  112. jit_frame->sp = block->frame_sp_begin;
  113. /* Load results to new block */
  114. offset = (uint32)(jit_frame->sp - jit_frame->lp);
  115. for (i = 0; i < block->result_count; i++) {
  116. switch (block->result_types[i]) {
  117. case VALUE_TYPE_I32:
  118. #if WASM_ENABLE_REF_TYPES != 0
  119. case VALUE_TYPE_EXTERNREF:
  120. case VALUE_TYPE_FUNCREF:
  121. #endif
  122. value = gen_load_i32(jit_frame, offset);
  123. offset++;
  124. break;
  125. case VALUE_TYPE_I64:
  126. value = gen_load_i64(jit_frame, offset);
  127. offset += 2;
  128. break;
  129. case VALUE_TYPE_F32:
  130. value = gen_load_f32(jit_frame, offset);
  131. offset++;
  132. break;
  133. case VALUE_TYPE_F64:
  134. value = gen_load_f64(jit_frame, offset);
  135. offset += 2;
  136. break;
  137. default:
  138. bh_assert(0);
  139. break;
  140. }
  141. PUSH(value, block->result_types[i]);
  142. }
  143. return true;
  144. fail:
  145. return false;
  146. }
  147. static bool
  148. jit_reg_is_i32_const(JitCompContext *cc, JitReg reg, int32 val)
  149. {
  150. return (jit_reg_kind(reg) == JIT_REG_KIND_I32 && jit_reg_is_const(reg)
  151. && jit_cc_get_const_I32(cc, reg) == val)
  152. ? true
  153. : false;
  154. }
  155. /**
  156. * get the last two insns:
  157. * CMP cmp_reg, r0, r1
  158. * SELECTcc r2, cmp_reg, 1, 0
  159. */
  160. static void
  161. get_last_cmp_and_selectcc(JitCompContext *cc, JitReg cond, JitInsn **p_insn_cmp,
  162. JitInsn **p_insn_select)
  163. {
  164. JitInsn *insn = jit_basic_block_last_insn(cc->cur_basic_block);
  165. if (insn && insn->prev && insn->prev->opcode == JIT_OP_CMP
  166. && insn->opcode >= JIT_OP_SELECTEQ && insn->opcode <= JIT_OP_SELECTLEU
  167. && *jit_insn_opnd(insn, 0) == cond
  168. && jit_reg_is_i32_const(cc, *jit_insn_opnd(insn, 2), 1)
  169. && jit_reg_is_i32_const(cc, *jit_insn_opnd(insn, 3), 0)) {
  170. *p_insn_cmp = insn->prev;
  171. *p_insn_select = insn;
  172. }
  173. }
  174. static bool
  175. push_jit_block_to_stack_and_pass_params(JitCompContext *cc, JitBlock *block,
  176. JitBasicBlock *basic_block, JitReg cond,
  177. bool merge_cmp_and_if)
  178. {
  179. JitFrame *jit_frame = cc->jit_frame;
  180. JitValue *value_list_head = NULL, *value_list_end = NULL, *jit_value;
  181. JitInsn *insn;
  182. JitReg value;
  183. uint32 i, param_index, cell_num;
  184. if (cc->cur_basic_block == basic_block) {
  185. /* Reuse the current basic block and no need to commit values,
  186. we just move param values from current block's value stack to
  187. the new block's value stack */
  188. for (i = 0; i < block->param_count; i++) {
  189. jit_value = jit_value_stack_pop(
  190. &jit_block_stack_top(&cc->block_stack)->value_stack);
  191. if (!value_list_head) {
  192. value_list_head = value_list_end = jit_value;
  193. jit_value->prev = jit_value->next = NULL;
  194. }
  195. else {
  196. jit_value->prev = NULL;
  197. jit_value->next = value_list_head;
  198. value_list_head->prev = jit_value;
  199. value_list_head = jit_value;
  200. }
  201. }
  202. block->value_stack.value_list_head = value_list_head;
  203. block->value_stack.value_list_end = value_list_end;
  204. /* Save block's begin frame sp */
  205. cell_num = wasm_get_cell_num(block->param_types, block->param_count);
  206. block->frame_sp_begin = jit_frame->sp - cell_num;
  207. /* Push the new block to block stack */
  208. jit_block_stack_push(&cc->block_stack, block);
  209. /* Continue to translate current block */
  210. }
  211. else {
  212. JitInsn *insn_select = NULL, *insn_cmp = NULL;
  213. if (merge_cmp_and_if) {
  214. get_last_cmp_and_selectcc(cc, cond, &insn_cmp, &insn_select);
  215. }
  216. /* Commit register values to locals and stacks */
  217. gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
  218. /* Pop param values from current block's value stack */
  219. for (i = 0; i < block->param_count; i++) {
  220. param_index = block->param_count - 1 - i;
  221. POP(value, block->param_types[param_index]);
  222. }
  223. /* Clear frame values */
  224. clear_values(jit_frame);
  225. /* Save block's begin frame sp */
  226. block->frame_sp_begin = jit_frame->sp;
  227. /* Push the new block to block stack */
  228. jit_block_stack_push(&cc->block_stack, block);
  229. if (block->label_type == LABEL_TYPE_LOOP) {
  230. BUILD_BR(basic_block);
  231. }
  232. else {
  233. /* IF block with condition br insn */
  234. if (insn_select && insn_cmp) {
  235. /* Change `CMP + SELECTcc` into `CMP + Bcc` */
  236. if (!(insn = GEN_INSN(BEQ, cc->cmp_reg,
  237. jit_basic_block_label(basic_block), 0))) {
  238. jit_set_last_error(cc, "generate cond br failed");
  239. goto fail;
  240. }
  241. insn->opcode =
  242. JIT_OP_BEQ + (insn_select->opcode - JIT_OP_SELECTEQ);
  243. jit_insn_unlink(insn_select);
  244. jit_insn_delete(insn_select);
  245. }
  246. else {
  247. if (!GEN_INSN(CMP, cc->cmp_reg, cond, NEW_CONST(I32, 0))
  248. || !(insn =
  249. GEN_INSN(BNE, cc->cmp_reg,
  250. jit_basic_block_label(basic_block), 0))) {
  251. jit_set_last_error(cc, "generate cond br failed");
  252. goto fail;
  253. }
  254. }
  255. /* Don't create else basic block or end basic block now, just
  256. save its incoming BNE insn, and patch the insn's else label
  257. when the basic block is lazily created */
  258. if (block->wasm_code_else) {
  259. block->incoming_insn_for_else_bb = insn;
  260. }
  261. else {
  262. if (!jit_block_add_incoming_insn(block, insn, 2)) {
  263. jit_set_last_error(cc, "add incoming insn failed");
  264. goto fail;
  265. }
  266. }
  267. }
  268. /* Start to translate the block */
  269. SET_BUILDER_POS(basic_block);
  270. /* Push the block parameters */
  271. if (!load_block_params(cc, block)) {
  272. goto fail;
  273. }
  274. }
  275. return true;
  276. fail:
  277. return false;
  278. }
  279. static void
  280. copy_block_arities(JitCompContext *cc, JitReg dst_frame_sp, uint8 *dst_types,
  281. uint32 dst_type_count, JitReg *p_first_res_reg)
  282. {
  283. JitFrame *jit_frame;
  284. uint32 offset_src, offset_dst, i;
  285. JitReg value;
  286. jit_frame = cc->jit_frame;
  287. offset_src = (uint32)(jit_frame->sp - jit_frame->lp)
  288. - wasm_get_cell_num(dst_types, dst_type_count);
  289. offset_dst = 0;
  290. /* pop values from stack and store to dest frame */
  291. for (i = 0; i < dst_type_count; i++) {
  292. switch (dst_types[i]) {
  293. case VALUE_TYPE_I32:
  294. #if WASM_ENABLE_REF_TYPES != 0
  295. case VALUE_TYPE_EXTERNREF:
  296. case VALUE_TYPE_FUNCREF:
  297. #endif
  298. value = gen_load_i32(jit_frame, offset_src);
  299. if (i == 0 && p_first_res_reg)
  300. *p_first_res_reg = value;
  301. else
  302. GEN_INSN(STI32, value, dst_frame_sp,
  303. NEW_CONST(I32, offset_dst * 4));
  304. offset_src++;
  305. offset_dst++;
  306. break;
  307. case VALUE_TYPE_I64:
  308. value = gen_load_i64(jit_frame, offset_src);
  309. if (i == 0 && p_first_res_reg)
  310. *p_first_res_reg = value;
  311. else
  312. GEN_INSN(STI64, value, dst_frame_sp,
  313. NEW_CONST(I32, offset_dst * 4));
  314. offset_src += 2;
  315. offset_dst += 2;
  316. break;
  317. case VALUE_TYPE_F32:
  318. value = gen_load_f32(jit_frame, offset_src);
  319. if (i == 0 && p_first_res_reg)
  320. *p_first_res_reg = value;
  321. else
  322. GEN_INSN(STF32, value, dst_frame_sp,
  323. NEW_CONST(I32, offset_dst * 4));
  324. offset_src++;
  325. offset_dst++;
  326. break;
  327. case VALUE_TYPE_F64:
  328. value = gen_load_f64(jit_frame, offset_src);
  329. if (i == 0 && p_first_res_reg)
  330. *p_first_res_reg = value;
  331. else
  332. GEN_INSN(STF64, value, dst_frame_sp,
  333. NEW_CONST(I32, offset_dst * 4));
  334. offset_src += 2;
  335. offset_dst += 2;
  336. break;
  337. default:
  338. bh_assert(0);
  339. break;
  340. }
  341. }
  342. }
  343. static bool
  344. handle_func_return(JitCompContext *cc, JitBlock *block)
  345. {
  346. JitReg prev_frame, prev_frame_sp;
  347. JitReg ret_reg = 0;
  348. #if WASM_ENABLE_PERF_PROFILING != 0
  349. JitReg func_inst = jit_cc_new_reg_ptr(cc);
  350. JitReg time_start = jit_cc_new_reg_I64(cc);
  351. JitReg time_end = jit_cc_new_reg_I64(cc);
  352. JitReg cur_exec_time = jit_cc_new_reg_I64(cc);
  353. JitReg total_exec_time = jit_cc_new_reg_I64(cc);
  354. JitReg total_exec_cnt = jit_cc_new_reg_I32(cc);
  355. #endif
  356. #if WASM_ENABLE_PERF_PROFILING != 0
  357. /* time_end = os_time_get_boot_microsecond() */
  358. if (!jit_emit_callnative(cc, os_time_get_boot_microsecond, time_end, NULL,
  359. 0)) {
  360. return false;
  361. }
  362. /* time_start = cur_frame->time_started */
  363. GEN_INSN(LDI64, time_start, cc->fp_reg,
  364. NEW_CONST(I32, offsetof(WASMInterpFrame, time_started)));
  365. /* cur_exec_time = time_end - time_start */
  366. GEN_INSN(SUB, cur_exec_time, time_end, time_start);
  367. /* func_inst = cur_frame->function */
  368. GEN_INSN(LDPTR, func_inst, cc->fp_reg,
  369. NEW_CONST(I32, offsetof(WASMInterpFrame, function)));
  370. /* total_exec_time = func_inst->total_exec_time */
  371. GEN_INSN(LDI64, total_exec_time, func_inst,
  372. NEW_CONST(I32, offsetof(WASMFunctionInstance, total_exec_time)));
  373. /* total_exec_time += cur_exec_time */
  374. GEN_INSN(ADD, total_exec_time, total_exec_time, cur_exec_time);
  375. /* func_inst->total_exec_time = total_exec_time */
  376. GEN_INSN(STI64, total_exec_time, func_inst,
  377. NEW_CONST(I32, offsetof(WASMFunctionInstance, total_exec_time)));
  378. /* totoal_exec_cnt = func_inst->total_exec_cnt */
  379. GEN_INSN(LDI32, total_exec_cnt, func_inst,
  380. NEW_CONST(I32, offsetof(WASMFunctionInstance, total_exec_cnt)));
  381. /* total_exec_cnt++ */
  382. GEN_INSN(ADD, total_exec_cnt, total_exec_cnt, NEW_CONST(I32, 1));
  383. /* func_inst->total_exec_cnt = total_exec_cnt */
  384. GEN_INSN(STI32, total_exec_cnt, func_inst,
  385. NEW_CONST(I32, offsetof(WASMFunctionInstance, total_exec_cnt)));
  386. #endif
  387. prev_frame = jit_cc_new_reg_ptr(cc);
  388. prev_frame_sp = jit_cc_new_reg_ptr(cc);
  389. /* prev_frame = cur_frame->prev_frame */
  390. GEN_INSN(LDPTR, prev_frame, cc->fp_reg,
  391. NEW_CONST(I32, offsetof(WASMInterpFrame, prev_frame)));
  392. GEN_INSN(LDPTR, prev_frame_sp, prev_frame,
  393. NEW_CONST(I32, offsetof(WASMInterpFrame, sp)));
  394. if (block->result_count) {
  395. uint32 cell_num =
  396. wasm_get_cell_num(block->result_types, block->result_count);
  397. copy_block_arities(cc, prev_frame_sp, block->result_types,
  398. block->result_count, &ret_reg);
  399. /* prev_frame->sp += cell_num */
  400. GEN_INSN(ADD, prev_frame_sp, prev_frame_sp,
  401. NEW_CONST(PTR, cell_num * 4));
  402. GEN_INSN(STPTR, prev_frame_sp, prev_frame,
  403. NEW_CONST(I32, offsetof(WASMInterpFrame, sp)));
  404. }
  405. /* Free stack space of the current frame:
  406. exec_env->wasm_stack.s.top = cur_frame */
  407. GEN_INSN(STPTR, cc->fp_reg, cc->exec_env_reg,
  408. NEW_CONST(I32, offsetof(WASMExecEnv, wasm_stack.s.top)));
  409. /* Set the prev_frame as the current frame:
  410. exec_env->cur_frame = prev_frame */
  411. GEN_INSN(STPTR, prev_frame, cc->exec_env_reg,
  412. NEW_CONST(I32, offsetof(WASMExecEnv, cur_frame)));
  413. /* fp_reg = prev_frame */
  414. GEN_INSN(MOV, cc->fp_reg, prev_frame);
  415. /* return 0 */
  416. GEN_INSN(RETURNBC, NEW_CONST(I32, JIT_INTERP_ACTION_NORMAL), ret_reg, 0);
  417. return true;
  418. }
  419. /**
  420. * is_block_polymorphic: whether current block's stack is in polymorphic state,
  421. * if the opcode is one of unreachable/br/br_table/return, stack is marked
  422. * to polymorphic state until the block's 'end' opcode is processed
  423. */
  424. static bool
  425. handle_op_end(JitCompContext *cc, uint8 **p_frame_ip, bool is_block_polymorphic)
  426. {
  427. JitFrame *jit_frame = cc->jit_frame;
  428. JitBlock *block, *block_prev;
  429. JitIncomingInsn *incoming_insn;
  430. JitInsn *insn;
  431. /* Check block stack */
  432. if (!(block = jit_block_stack_top(&cc->block_stack))) {
  433. jit_set_last_error(cc, "WASM block stack underflow");
  434. return false;
  435. }
  436. if (!block->incoming_insns_for_end_bb) {
  437. /* No other basic blocks jumping to this end, no need to
  438. create the end basic block, just continue to translate
  439. the following opcodes */
  440. if (block->label_type == LABEL_TYPE_FUNCTION) {
  441. if (!handle_func_return(cc, block)) {
  442. return false;
  443. }
  444. SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
  445. clear_values(jit_frame);
  446. }
  447. else if (block->result_count > 0) {
  448. JitValue *value_list_head = NULL, *value_list_end = NULL;
  449. JitValue *jit_value;
  450. uint32 i;
  451. /* No need to change cc->jit_frame, just move result values
  452. from current block's value stack to previous block's
  453. value stack */
  454. block_prev = block->prev;
  455. for (i = 0; i < block->result_count; i++) {
  456. jit_value = jit_value_stack_pop(&block->value_stack);
  457. bh_assert(jit_value);
  458. if (!value_list_head) {
  459. value_list_head = value_list_end = jit_value;
  460. jit_value->prev = jit_value->next = NULL;
  461. }
  462. else {
  463. jit_value->prev = NULL;
  464. jit_value->next = value_list_head;
  465. value_list_head->prev = jit_value;
  466. value_list_head = jit_value;
  467. }
  468. }
  469. if (!block_prev->value_stack.value_list_head) {
  470. block_prev->value_stack.value_list_head = value_list_head;
  471. block_prev->value_stack.value_list_end = value_list_end;
  472. }
  473. else {
  474. /* Link to the end of previous block's value stack */
  475. block_prev->value_stack.value_list_end->next = value_list_head;
  476. value_list_head->prev = block_prev->value_stack.value_list_end;
  477. block_prev->value_stack.value_list_end = value_list_end;
  478. }
  479. }
  480. /* Pop block and destroy the block */
  481. block = jit_block_stack_pop(&cc->block_stack);
  482. jit_block_destroy(block);
  483. return true;
  484. }
  485. else {
  486. /* Commit register values to locals and stacks */
  487. gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
  488. /* Clear frame values */
  489. clear_values(jit_frame);
  490. /* Create the end basic block */
  491. CREATE_BASIC_BLOCK(block->basic_block_end);
  492. SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
  493. SET_BB_BEGIN_BCIP(block->basic_block_end, *p_frame_ip);
  494. /* No need to create 'JMP' insn if block is in stack polymorphic
  495. state, as previous br/br_table opcode has created 'JMP' insn
  496. to this end basic block */
  497. if (!is_block_polymorphic) {
  498. /* Jump to the end basic block */
  499. BUILD_BR(block->basic_block_end);
  500. }
  501. /* Patch the INSNs which jump to this basic block */
  502. incoming_insn = block->incoming_insns_for_end_bb;
  503. while (incoming_insn) {
  504. insn = incoming_insn->insn;
  505. bh_assert(
  506. insn->opcode == JIT_OP_JMP
  507. || (insn->opcode >= JIT_OP_BEQ && insn->opcode <= JIT_OP_BLEU)
  508. || insn->opcode == JIT_OP_LOOKUPSWITCH);
  509. if (insn->opcode == JIT_OP_JMP
  510. || (insn->opcode >= JIT_OP_BEQ
  511. && insn->opcode <= JIT_OP_BLEU)) {
  512. *(jit_insn_opnd(insn, incoming_insn->opnd_idx)) =
  513. jit_basic_block_label(block->basic_block_end);
  514. }
  515. else {
  516. /* Patch LOOKUPSWITCH INSN */
  517. JitOpndLookupSwitch *opnd = jit_insn_opndls(insn);
  518. if (incoming_insn->opnd_idx < opnd->match_pairs_num) {
  519. opnd->match_pairs[incoming_insn->opnd_idx].target =
  520. jit_basic_block_label(block->basic_block_end);
  521. }
  522. else {
  523. opnd->default_target =
  524. jit_basic_block_label(block->basic_block_end);
  525. }
  526. }
  527. incoming_insn = incoming_insn->next;
  528. }
  529. SET_BUILDER_POS(block->basic_block_end);
  530. /* Pop block and load block results */
  531. block = jit_block_stack_pop(&cc->block_stack);
  532. if (block->label_type == LABEL_TYPE_FUNCTION) {
  533. if (!handle_func_return(cc, block)) {
  534. jit_block_destroy(block);
  535. goto fail;
  536. }
  537. SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
  538. clear_values(jit_frame);
  539. }
  540. else {
  541. if (!load_block_results(cc, block)) {
  542. jit_block_destroy(block);
  543. goto fail;
  544. }
  545. }
  546. jit_block_destroy(block);
  547. return true;
  548. }
  549. return true;
  550. fail:
  551. return false;
  552. }
  553. /**
  554. * is_block_polymorphic: whether current block's stack is in polymorphic state,
  555. * if the opcode is one of unreachable/br/br_table/return, stack is marked
  556. * to polymorphic state until the block's 'end' opcode is processed
  557. */
  558. static bool
  559. handle_op_else(JitCompContext *cc, uint8 **p_frame_ip,
  560. bool is_block_polymorphic)
  561. {
  562. JitBlock *block = jit_block_stack_top(&cc->block_stack);
  563. JitFrame *jit_frame = cc->jit_frame;
  564. JitInsn *insn;
  565. /* Check block */
  566. if (!block) {
  567. jit_set_last_error(cc, "WASM block stack underflow");
  568. return false;
  569. }
  570. if (block->label_type != LABEL_TYPE_IF) {
  571. jit_set_last_error(cc, "Invalid WASM block type");
  572. return false;
  573. }
  574. if (!block->incoming_insn_for_else_bb) {
  575. /* The if branch is handled like OP_BLOCK (cond is const and != 0),
  576. just skip the else branch and handle OP_END */
  577. *p_frame_ip = block->wasm_code_end + 1;
  578. return handle_op_end(cc, p_frame_ip, false);
  579. }
  580. else {
  581. /* Has else branch and need to translate else branch */
  582. /* Commit register values to locals and stacks */
  583. gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
  584. /* Clear frame values */
  585. clear_values(jit_frame);
  586. /* No need to create 'JMP' insn if block is in stack polymorphic
  587. state, as previous br/br_table opcode has created 'JMP' insn
  588. to this end basic block */
  589. if (!is_block_polymorphic) {
  590. /* Jump to end basic block */
  591. if (!(insn = GEN_INSN(JMP, 0))) {
  592. jit_set_last_error(cc, "generate jmp insn failed");
  593. return false;
  594. }
  595. if (!jit_block_add_incoming_insn(block, insn, 0)) {
  596. jit_set_last_error(cc, "add incoming insn failed");
  597. return false;
  598. }
  599. }
  600. /* Clear value stack, restore param values and
  601. start to translate the else branch. */
  602. jit_value_stack_destroy(&block->value_stack);
  603. /* create else basic block */
  604. CREATE_BASIC_BLOCK(block->basic_block_else);
  605. SET_BB_END_BCIP(block->basic_block_entry, *p_frame_ip - 1);
  606. SET_BB_BEGIN_BCIP(block->basic_block_else, *p_frame_ip);
  607. /* Patch the insn which conditionly jumps to the else basic block */
  608. insn = block->incoming_insn_for_else_bb;
  609. *(jit_insn_opnd(insn, 2)) =
  610. jit_basic_block_label(block->basic_block_else);
  611. SET_BUILDER_POS(block->basic_block_else);
  612. /* Reload block parameters */
  613. if (!load_block_params(cc, block)) {
  614. return false;
  615. }
  616. return true;
  617. }
  618. return true;
  619. fail:
  620. return false;
  621. }
  622. static bool
  623. handle_next_reachable_block(JitCompContext *cc, uint8 **p_frame_ip)
  624. {
  625. JitBlock *block = jit_block_stack_top(&cc->block_stack);
  626. bh_assert(block);
  627. do {
  628. if (block->label_type == LABEL_TYPE_IF
  629. && block->incoming_insn_for_else_bb
  630. && *p_frame_ip <= block->wasm_code_else) {
  631. /* Else branch hasn't been translated,
  632. start to translate the else branch */
  633. *p_frame_ip = block->wasm_code_else + 1;
  634. /* Restore jit frame's sp to block's sp begin */
  635. cc->jit_frame->sp = block->frame_sp_begin;
  636. return handle_op_else(cc, p_frame_ip, true);
  637. }
  638. else if (block->incoming_insns_for_end_bb) {
  639. *p_frame_ip = block->wasm_code_end + 1;
  640. /* Restore jit frame's sp to block's sp end */
  641. cc->jit_frame->sp =
  642. block->frame_sp_begin
  643. + wasm_get_cell_num(block->result_types, block->result_count);
  644. return handle_op_end(cc, p_frame_ip, true);
  645. }
  646. else {
  647. *p_frame_ip = block->wasm_code_end + 1;
  648. jit_block_stack_pop(&cc->block_stack);
  649. jit_block_destroy(block);
  650. block = jit_block_stack_top(&cc->block_stack);
  651. }
  652. } while (block != NULL);
  653. return true;
  654. }
  655. bool
  656. jit_compile_op_block(JitCompContext *cc, uint8 **p_frame_ip,
  657. uint8 *frame_ip_end, uint32 label_type, uint32 param_count,
  658. uint8 *param_types, uint32 result_count,
  659. uint8 *result_types, bool merge_cmp_and_if)
  660. {
  661. BlockAddr block_addr_cache[BLOCK_ADDR_CACHE_SIZE][BLOCK_ADDR_CONFLICT_SIZE];
  662. JitBlock *block;
  663. JitReg value;
  664. uint8 *else_addr, *end_addr;
  665. /* Check block stack */
  666. if (!jit_block_stack_top(&cc->block_stack)) {
  667. jit_set_last_error(cc, "WASM block stack underflow");
  668. return false;
  669. }
  670. memset(block_addr_cache, 0, sizeof(block_addr_cache));
  671. /* Get block info */
  672. if (!(wasm_loader_find_block_addr(
  673. NULL, (BlockAddr *)block_addr_cache, *p_frame_ip, frame_ip_end,
  674. (uint8)label_type, &else_addr, &end_addr))) {
  675. jit_set_last_error(cc, "find block end addr failed");
  676. return false;
  677. }
  678. /* Allocate memory */
  679. if (!(block = jit_calloc(sizeof(JitBlock)))) {
  680. jit_set_last_error(cc, "allocate memory failed");
  681. return false;
  682. }
  683. if (param_count && !(block->param_types = jit_calloc(param_count))) {
  684. jit_set_last_error(cc, "allocate memory failed");
  685. goto fail;
  686. }
  687. if (result_count && !(block->result_types = jit_calloc(result_count))) {
  688. jit_set_last_error(cc, "allocate memory failed");
  689. goto fail;
  690. }
  691. /* Initialize block data */
  692. block->label_type = label_type;
  693. block->param_count = param_count;
  694. if (param_count) {
  695. bh_memcpy_s(block->param_types, param_count, param_types, param_count);
  696. }
  697. block->result_count = result_count;
  698. if (result_count) {
  699. bh_memcpy_s(block->result_types, result_count, result_types,
  700. result_count);
  701. }
  702. block->wasm_code_else = else_addr;
  703. block->wasm_code_end = end_addr;
  704. if (label_type == LABEL_TYPE_BLOCK) {
  705. /* Push the new jit block to block stack and continue to
  706. translate current basic block */
  707. if (!push_jit_block_to_stack_and_pass_params(
  708. cc, block, cc->cur_basic_block, 0, false))
  709. goto fail;
  710. }
  711. else if (label_type == LABEL_TYPE_LOOP) {
  712. CREATE_BASIC_BLOCK(block->basic_block_entry);
  713. SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
  714. SET_BB_BEGIN_BCIP(block->basic_block_entry, *p_frame_ip);
  715. /* Push the new jit block to block stack and continue to
  716. translate the new basic block */
  717. if (!push_jit_block_to_stack_and_pass_params(
  718. cc, block, block->basic_block_entry, 0, false))
  719. goto fail;
  720. }
  721. else if (label_type == LABEL_TYPE_IF) {
  722. POP_I32(value);
  723. if (!jit_reg_is_const_val(value)) {
  724. /* Compare value is not constant, create condition br IR */
  725. /* Create entry block */
  726. CREATE_BASIC_BLOCK(block->basic_block_entry);
  727. SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
  728. SET_BB_BEGIN_BCIP(block->basic_block_entry, *p_frame_ip);
  729. if (!push_jit_block_to_stack_and_pass_params(
  730. cc, block, block->basic_block_entry, value,
  731. merge_cmp_and_if))
  732. goto fail;
  733. }
  734. else {
  735. if (jit_cc_get_const_I32(cc, value) != 0) {
  736. /* Compare value is not 0, condition is true, else branch of
  737. BASIC_BLOCK if cannot be reached, we treat it same as
  738. LABEL_TYPE_BLOCK and start to translate if branch */
  739. if (!push_jit_block_to_stack_and_pass_params(
  740. cc, block, cc->cur_basic_block, 0, false))
  741. goto fail;
  742. }
  743. else {
  744. if (else_addr) {
  745. /* Compare value is not 0, condition is false, if branch of
  746. BASIC_BLOCK if cannot be reached, we treat it same as
  747. LABEL_TYPE_BLOCK and start to translate else branch */
  748. if (!push_jit_block_to_stack_and_pass_params(
  749. cc, block, cc->cur_basic_block, 0, false))
  750. goto fail;
  751. *p_frame_ip = else_addr + 1;
  752. }
  753. else {
  754. /* The whole if block cannot be reached, skip it */
  755. jit_block_destroy(block);
  756. *p_frame_ip = end_addr + 1;
  757. }
  758. }
  759. }
  760. }
  761. else {
  762. jit_set_last_error(cc, "Invalid block type");
  763. goto fail;
  764. }
  765. return true;
  766. fail:
  767. /* Only destroy the block if it hasn't been pushed into
  768. the block stack, or if will be destroyed again when
  769. destroying the block stack */
  770. if (jit_block_stack_top(&cc->block_stack) != block)
  771. jit_block_destroy(block);
  772. return false;
  773. }
  774. bool
  775. jit_compile_op_else(JitCompContext *cc, uint8 **p_frame_ip)
  776. {
  777. return handle_op_else(cc, p_frame_ip, false);
  778. }
  779. bool
  780. jit_compile_op_end(JitCompContext *cc, uint8 **p_frame_ip)
  781. {
  782. return handle_op_end(cc, p_frame_ip, false);
  783. }
  784. /* Check whether need to copy arities when jumping from current block
  785. to the dest block */
  786. static bool
  787. check_copy_arities(const JitBlock *block_dst, JitFrame *jit_frame)
  788. {
  789. JitValueSlot *frame_sp_src = NULL;
  790. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  791. frame_sp_src =
  792. jit_frame->sp
  793. - wasm_get_cell_num(block_dst->param_types, block_dst->param_count);
  794. /* There are parameters to copy and the src/dst addr are different */
  795. return (block_dst->param_count > 0
  796. && block_dst->frame_sp_begin != frame_sp_src)
  797. ? true
  798. : false;
  799. }
  800. else {
  801. frame_sp_src = jit_frame->sp
  802. - wasm_get_cell_num(block_dst->result_types,
  803. block_dst->result_count);
  804. /* There are results to copy and the src/dst addr are different */
  805. return (block_dst->result_count > 0
  806. && block_dst->frame_sp_begin != frame_sp_src)
  807. ? true
  808. : false;
  809. }
  810. }
  811. static bool
  812. handle_op_br(JitCompContext *cc, uint32 br_depth, uint8 **p_frame_ip)
  813. {
  814. JitFrame *jit_frame;
  815. JitBlock *block_dst, *block;
  816. JitReg frame_sp_dst;
  817. JitInsn *insn;
  818. bool copy_arities;
  819. uint32 offset;
  820. /* Check block stack */
  821. if (!(block = jit_block_stack_top(&cc->block_stack))) {
  822. jit_set_last_error(cc, "WASM block stack underflow");
  823. return false;
  824. }
  825. if (!(block_dst = get_target_block(cc, br_depth))) {
  826. return false;
  827. }
  828. jit_frame = cc->jit_frame;
  829. /* Only opy parameters or results when their count > 0 and
  830. the src/dst addr are different */
  831. copy_arities = check_copy_arities(block_dst, jit_frame);
  832. if (copy_arities) {
  833. frame_sp_dst = jit_cc_new_reg_ptr(cc);
  834. offset = offsetof(WASMInterpFrame, lp)
  835. + (block_dst->frame_sp_begin - jit_frame->lp) * 4;
  836. GEN_INSN(ADD, frame_sp_dst, cc->fp_reg, NEW_CONST(PTR, offset));
  837. /* No need to commit results as they will be copied to dest block */
  838. gen_commit_values(jit_frame, jit_frame->lp, block->frame_sp_begin);
  839. }
  840. else {
  841. /* Commit all including results as they won't be copied */
  842. gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
  843. }
  844. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  845. if (copy_arities) {
  846. /* Dest block is Loop block, copy loop parameters */
  847. copy_block_arities(cc, frame_sp_dst, block_dst->param_types,
  848. block_dst->param_count, NULL);
  849. }
  850. clear_values(jit_frame);
  851. /* Jump to the begin basic block */
  852. BUILD_BR(block_dst->basic_block_entry);
  853. SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
  854. }
  855. else {
  856. if (copy_arities) {
  857. /* Dest block is Block/If/Function block, copy block results */
  858. copy_block_arities(cc, frame_sp_dst, block_dst->result_types,
  859. block_dst->result_count, NULL);
  860. }
  861. clear_values(jit_frame);
  862. /* Jump to the end basic block */
  863. if (!(insn = GEN_INSN(JMP, 0))) {
  864. jit_set_last_error(cc, "generate jmp insn failed");
  865. goto fail;
  866. }
  867. if (!jit_block_add_incoming_insn(block_dst, insn, 0)) {
  868. jit_set_last_error(cc, "add incoming insn failed");
  869. goto fail;
  870. }
  871. SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
  872. }
  873. return true;
  874. fail:
  875. return false;
  876. }
  877. bool
  878. jit_compile_op_br(JitCompContext *cc, uint32 br_depth, uint8 **p_frame_ip)
  879. {
  880. return handle_op_br(cc, br_depth, p_frame_ip)
  881. && handle_next_reachable_block(cc, p_frame_ip);
  882. }
  883. static JitFrame *
  884. jit_frame_clone(const JitFrame *jit_frame)
  885. {
  886. JitFrame *jit_frame_cloned;
  887. uint32 max_locals = jit_frame->max_locals;
  888. uint32 max_stacks = jit_frame->max_stacks;
  889. uint32 total_size;
  890. total_size = (uint32)(offsetof(JitFrame, lp)
  891. + sizeof(*jit_frame->lp) * (max_locals + max_stacks));
  892. jit_frame_cloned = jit_calloc(total_size);
  893. if (jit_frame_cloned) {
  894. bh_memcpy_s(jit_frame_cloned, total_size, jit_frame, total_size);
  895. jit_frame_cloned->sp =
  896. jit_frame_cloned->lp + (jit_frame->sp - jit_frame->lp);
  897. }
  898. return jit_frame_cloned;
  899. }
  900. static void
  901. jit_frame_copy(JitFrame *jit_frame_dst, const JitFrame *jit_frame_src)
  902. {
  903. uint32 max_locals = jit_frame_src->max_locals;
  904. uint32 max_stacks = jit_frame_src->max_stacks;
  905. uint32 total_size;
  906. total_size =
  907. (uint32)(offsetof(JitFrame, lp)
  908. + sizeof(*jit_frame_src->lp) * (max_locals + max_stacks));
  909. bh_memcpy_s(jit_frame_dst, total_size, jit_frame_src, total_size);
  910. jit_frame_dst->sp =
  911. jit_frame_dst->lp + (jit_frame_src->sp - jit_frame_src->lp);
  912. }
  913. bool
  914. jit_compile_op_br_if(JitCompContext *cc, uint32 br_depth,
  915. bool merge_cmp_and_br_if, uint8 **p_frame_ip)
  916. {
  917. JitFrame *jit_frame, *jit_frame_cloned;
  918. JitBlock *block_dst;
  919. JitReg cond;
  920. JitBasicBlock *cur_basic_block, *if_basic_block = NULL;
  921. JitInsn *insn, *insn_select = NULL, *insn_cmp = NULL;
  922. bool copy_arities;
  923. if (!(block_dst = get_target_block(cc, br_depth))) {
  924. return false;
  925. }
  926. /* append IF to current basic block */
  927. POP_I32(cond);
  928. if (merge_cmp_and_br_if) {
  929. get_last_cmp_and_selectcc(cc, cond, &insn_cmp, &insn_select);
  930. }
  931. jit_frame = cc->jit_frame;
  932. cur_basic_block = cc->cur_basic_block;
  933. gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
  934. if (!(insn_select && insn_cmp)) {
  935. if (!GEN_INSN(CMP, cc->cmp_reg, cond, NEW_CONST(I32, 0))) {
  936. jit_set_last_error(cc, "generate cmp insn failed");
  937. goto fail;
  938. }
  939. }
  940. /* Only opy parameters or results when their count > 0 and
  941. the src/dst addr are different */
  942. copy_arities = check_copy_arities(block_dst, jit_frame);
  943. if (!copy_arities) {
  944. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  945. if (!(insn = GEN_INSN(
  946. BNE, cc->cmp_reg,
  947. jit_basic_block_label(block_dst->basic_block_entry),
  948. 0))) {
  949. jit_set_last_error(cc, "generate bne insn failed");
  950. goto fail;
  951. }
  952. }
  953. else {
  954. if (!(insn = GEN_INSN(BNE, cc->cmp_reg, 0, 0))) {
  955. jit_set_last_error(cc, "generate bne insn failed");
  956. goto fail;
  957. }
  958. if (!jit_block_add_incoming_insn(block_dst, insn, 1)) {
  959. jit_set_last_error(cc, "add incoming insn failed");
  960. goto fail;
  961. }
  962. }
  963. if (insn_select && insn_cmp) {
  964. /* Change `CMP + SELECTcc` into `CMP + Bcc` */
  965. insn->opcode = JIT_OP_BEQ + (insn_select->opcode - JIT_OP_SELECTEQ);
  966. jit_insn_unlink(insn_select);
  967. jit_insn_delete(insn_select);
  968. }
  969. return true;
  970. }
  971. CREATE_BASIC_BLOCK(if_basic_block);
  972. if (!(insn = GEN_INSN(BNE, cc->cmp_reg,
  973. jit_basic_block_label(if_basic_block), 0))) {
  974. jit_set_last_error(cc, "generate bne insn failed");
  975. goto fail;
  976. }
  977. if (insn_select && insn_cmp) {
  978. /* Change `CMP + SELECTcc` into `CMP + Bcc` */
  979. insn->opcode = JIT_OP_BEQ + (insn_select->opcode - JIT_OP_SELECTEQ);
  980. jit_insn_unlink(insn_select);
  981. jit_insn_delete(insn_select);
  982. }
  983. SET_BUILDER_POS(if_basic_block);
  984. SET_BB_BEGIN_BCIP(if_basic_block, *p_frame_ip - 1);
  985. /* Clone current jit frame to a new jit fame */
  986. if (!(jit_frame_cloned = jit_frame_clone(jit_frame))) {
  987. jit_set_last_error(cc, "allocate memory failed");
  988. goto fail;
  989. }
  990. /* Clear current jit frame so that the registers
  991. in the new basic block will be loaded again */
  992. clear_values(jit_frame);
  993. if (!handle_op_br(cc, br_depth, p_frame_ip)) {
  994. jit_free(jit_frame_cloned);
  995. goto fail;
  996. }
  997. /* Restore the jit frame so that the registers can
  998. be used again in current basic block */
  999. jit_frame_copy(jit_frame, jit_frame_cloned);
  1000. jit_free(jit_frame_cloned);
  1001. /* Continue processing opcodes after BR_IF */
  1002. SET_BUILDER_POS(cur_basic_block);
  1003. return true;
  1004. fail:
  1005. return false;
  1006. }
  1007. bool
  1008. jit_compile_op_br_table(JitCompContext *cc, uint32 *br_depths, uint32 br_count,
  1009. uint8 **p_frame_ip)
  1010. {
  1011. JitBasicBlock *cur_basic_block;
  1012. JitReg value;
  1013. JitInsn *insn;
  1014. uint32 i = 0;
  1015. JitOpndLookupSwitch *opnd = NULL;
  1016. cur_basic_block = cc->cur_basic_block;
  1017. POP_I32(value);
  1018. /* append LOOKUPSWITCH to current basic block */
  1019. gen_commit_values(cc->jit_frame, cc->jit_frame->lp, cc->jit_frame->sp);
  1020. /* Clear frame values */
  1021. clear_values(cc->jit_frame);
  1022. SET_BB_END_BCIP(cur_basic_block, *p_frame_ip - 1);
  1023. /* prepare basic blocks for br */
  1024. insn = GEN_INSN(LOOKUPSWITCH, value, br_count);
  1025. if (NULL == insn) {
  1026. jit_set_last_error(cc, "generate insn LOOKUPSWITCH failed");
  1027. goto fail;
  1028. }
  1029. for (i = 0, opnd = jit_insn_opndls(insn); i < br_count + 1; i++) {
  1030. JitBasicBlock *basic_block = NULL;
  1031. JitBlock *block_dst;
  1032. bool copy_arities;
  1033. if (!(block_dst = get_target_block(cc, br_depths[i]))) {
  1034. goto fail;
  1035. }
  1036. /* Only opy parameters or results when their count > 0 and
  1037. the src/dst addr are different */
  1038. copy_arities = check_copy_arities(block_dst, cc->jit_frame);
  1039. if (!copy_arities) {
  1040. /* No need to create new basic block, direclty jump to
  1041. the existing basic block when no need to copy arities */
  1042. if (i == br_count) {
  1043. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  1044. opnd->default_target =
  1045. jit_basic_block_label(block_dst->basic_block_entry);
  1046. }
  1047. else {
  1048. bh_assert(!block_dst->basic_block_end);
  1049. if (!jit_block_add_incoming_insn(block_dst, insn, i)) {
  1050. jit_set_last_error(cc, "add incoming insn failed");
  1051. goto fail;
  1052. }
  1053. }
  1054. }
  1055. else {
  1056. opnd->match_pairs[i].value = i;
  1057. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  1058. opnd->match_pairs[i].target =
  1059. jit_basic_block_label(block_dst->basic_block_entry);
  1060. }
  1061. else {
  1062. bh_assert(!block_dst->basic_block_end);
  1063. if (!jit_block_add_incoming_insn(block_dst, insn, i)) {
  1064. jit_set_last_error(cc, "add incoming insn failed");
  1065. goto fail;
  1066. }
  1067. }
  1068. }
  1069. continue;
  1070. }
  1071. /* Create new basic block when need to copy arities */
  1072. CREATE_BASIC_BLOCK(basic_block);
  1073. SET_BB_BEGIN_BCIP(basic_block, *p_frame_ip - 1);
  1074. if (i == br_count) {
  1075. opnd->default_target = jit_basic_block_label(basic_block);
  1076. }
  1077. else {
  1078. opnd->match_pairs[i].value = i;
  1079. opnd->match_pairs[i].target = jit_basic_block_label(basic_block);
  1080. }
  1081. SET_BUILDER_POS(basic_block);
  1082. if (!handle_op_br(cc, br_depths[i], p_frame_ip))
  1083. goto fail;
  1084. }
  1085. /* Search next available block to handle */
  1086. return handle_next_reachable_block(cc, p_frame_ip);
  1087. fail:
  1088. return false;
  1089. }
  1090. bool
  1091. jit_compile_op_return(JitCompContext *cc, uint8 **p_frame_ip)
  1092. {
  1093. JitBlock *block_func = cc->block_stack.block_list_head;
  1094. bh_assert(block_func);
  1095. if (!handle_func_return(cc, block_func)) {
  1096. return false;
  1097. }
  1098. SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
  1099. clear_values(cc->jit_frame);
  1100. return handle_next_reachable_block(cc, p_frame_ip);
  1101. }
  1102. bool
  1103. jit_compile_op_unreachable(JitCompContext *cc, uint8 **p_frame_ip)
  1104. {
  1105. if (!jit_emit_exception(cc, EXCE_UNREACHABLE, JIT_OP_JMP, 0, NULL))
  1106. return false;
  1107. return handle_next_reachable_block(cc, p_frame_ip);
  1108. }
  1109. bool
  1110. jit_handle_next_reachable_block(JitCompContext *cc, uint8 **p_frame_ip)
  1111. {
  1112. return handle_next_reachable_block(cc, p_frame_ip);
  1113. }