jit_emit_control.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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_thread_cputime_us() */
  358. if (!jit_emit_callnative(cc, os_time_thread_cputime_us, 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.top = cur_frame */
  407. GEN_INSN(STPTR, cc->fp_reg, cc->exec_env_reg,
  408. NEW_CONST(I32, offsetof(WASMExecEnv, wasm_stack.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(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. #if WASM_ENABLE_THREAD_MGR != 0
  812. bool
  813. jit_check_suspend_flags(JitCompContext *cc)
  814. {
  815. JitReg exec_env, suspend_flags, terminate_flag, offset;
  816. JitBasicBlock *terminate_block, *cur_basic_block;
  817. JitFrame *jit_frame = cc->jit_frame;
  818. cur_basic_block = cc->cur_basic_block;
  819. terminate_block = jit_cc_new_basic_block(cc, 0);
  820. if (!terminate_block) {
  821. return false;
  822. }
  823. gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
  824. exec_env = cc->exec_env_reg;
  825. suspend_flags = jit_cc_new_reg_I32(cc);
  826. terminate_flag = jit_cc_new_reg_I32(cc);
  827. offset = jit_cc_new_const_I32(cc, offsetof(WASMExecEnv, suspend_flags));
  828. GEN_INSN(LDI32, suspend_flags, exec_env, offset);
  829. GEN_INSN(AND, terminate_flag, suspend_flags, NEW_CONST(I32, 1));
  830. GEN_INSN(CMP, cc->cmp_reg, terminate_flag, NEW_CONST(I32, 0));
  831. GEN_INSN(BNE, cc->cmp_reg, jit_basic_block_label(terminate_block), 0);
  832. cc->cur_basic_block = terminate_block;
  833. GEN_INSN(RETURN, NEW_CONST(I32, 0));
  834. cc->cur_basic_block = cur_basic_block;
  835. return true;
  836. }
  837. #endif
  838. static bool
  839. handle_op_br(JitCompContext *cc, uint32 br_depth, uint8 **p_frame_ip)
  840. {
  841. JitFrame *jit_frame;
  842. JitBlock *block_dst, *block;
  843. JitReg frame_sp_dst;
  844. JitInsn *insn;
  845. bool copy_arities;
  846. uint32 offset;
  847. /* Check block stack */
  848. if (!(block = jit_block_stack_top(&cc->block_stack))) {
  849. jit_set_last_error(cc, "WASM block stack underflow");
  850. return false;
  851. }
  852. if (!(block_dst = get_target_block(cc, br_depth))) {
  853. return false;
  854. }
  855. jit_frame = cc->jit_frame;
  856. /* Only opy parameters or results when their count > 0 and
  857. the src/dst addr are different */
  858. copy_arities = check_copy_arities(block_dst, jit_frame);
  859. if (copy_arities) {
  860. frame_sp_dst = jit_cc_new_reg_ptr(cc);
  861. offset = offsetof(WASMInterpFrame, lp)
  862. + (block_dst->frame_sp_begin - jit_frame->lp) * 4;
  863. GEN_INSN(ADD, frame_sp_dst, cc->fp_reg, NEW_CONST(PTR, offset));
  864. /* No need to commit results as they will be copied to dest block */
  865. gen_commit_values(jit_frame, jit_frame->lp, block->frame_sp_begin);
  866. }
  867. else {
  868. /* Commit all including results as they won't be copied */
  869. gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
  870. }
  871. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  872. if (copy_arities) {
  873. /* Dest block is Loop block, copy loop parameters */
  874. copy_block_arities(cc, frame_sp_dst, block_dst->param_types,
  875. block_dst->param_count, NULL);
  876. }
  877. clear_values(jit_frame);
  878. /* Jump to the begin basic block */
  879. BUILD_BR(block_dst->basic_block_entry);
  880. SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
  881. }
  882. else {
  883. if (copy_arities) {
  884. /* Dest block is Block/If/Function block, copy block results */
  885. copy_block_arities(cc, frame_sp_dst, block_dst->result_types,
  886. block_dst->result_count, NULL);
  887. }
  888. clear_values(jit_frame);
  889. /* Jump to the end basic block */
  890. if (!(insn = GEN_INSN(JMP, 0))) {
  891. jit_set_last_error(cc, "generate jmp insn failed");
  892. goto fail;
  893. }
  894. if (!jit_block_add_incoming_insn(block_dst, insn, 0)) {
  895. jit_set_last_error(cc, "add incoming insn failed");
  896. goto fail;
  897. }
  898. SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
  899. }
  900. return true;
  901. fail:
  902. return false;
  903. }
  904. bool
  905. jit_compile_op_br(JitCompContext *cc, uint32 br_depth, uint8 **p_frame_ip)
  906. {
  907. #if WASM_ENABLE_THREAD_MGR != 0
  908. /* Insert suspend check point */
  909. if (!jit_check_suspend_flags(cc))
  910. return false;
  911. #endif
  912. return handle_op_br(cc, br_depth, p_frame_ip)
  913. && handle_next_reachable_block(cc, p_frame_ip);
  914. }
  915. static JitFrame *
  916. jit_frame_clone(const JitFrame *jit_frame)
  917. {
  918. JitFrame *jit_frame_cloned;
  919. uint32 max_locals = jit_frame->max_locals;
  920. uint32 max_stacks = jit_frame->max_stacks;
  921. uint32 total_size;
  922. total_size = (uint32)(offsetof(JitFrame, lp)
  923. + sizeof(*jit_frame->lp) * (max_locals + max_stacks));
  924. jit_frame_cloned = jit_calloc(total_size);
  925. if (jit_frame_cloned) {
  926. bh_memcpy_s(jit_frame_cloned, total_size, jit_frame, total_size);
  927. jit_frame_cloned->sp =
  928. jit_frame_cloned->lp + (jit_frame->sp - jit_frame->lp);
  929. }
  930. return jit_frame_cloned;
  931. }
  932. static void
  933. jit_frame_copy(JitFrame *jit_frame_dst, const JitFrame *jit_frame_src)
  934. {
  935. uint32 max_locals = jit_frame_src->max_locals;
  936. uint32 max_stacks = jit_frame_src->max_stacks;
  937. uint32 total_size;
  938. total_size =
  939. (uint32)(offsetof(JitFrame, lp)
  940. + sizeof(*jit_frame_src->lp) * (max_locals + max_stacks));
  941. bh_memcpy_s(jit_frame_dst, total_size, jit_frame_src, total_size);
  942. jit_frame_dst->sp =
  943. jit_frame_dst->lp + (jit_frame_src->sp - jit_frame_src->lp);
  944. }
  945. bool
  946. jit_compile_op_br_if(JitCompContext *cc, uint32 br_depth,
  947. bool merge_cmp_and_br_if, uint8 **p_frame_ip)
  948. {
  949. JitFrame *jit_frame, *jit_frame_cloned;
  950. JitBlock *block_dst;
  951. JitReg cond;
  952. JitBasicBlock *cur_basic_block, *if_basic_block = NULL;
  953. JitInsn *insn, *insn_select = NULL, *insn_cmp = NULL;
  954. bool copy_arities;
  955. if (!(block_dst = get_target_block(cc, br_depth))) {
  956. return false;
  957. }
  958. /* append IF to current basic block */
  959. POP_I32(cond);
  960. if (merge_cmp_and_br_if) {
  961. get_last_cmp_and_selectcc(cc, cond, &insn_cmp, &insn_select);
  962. }
  963. jit_frame = cc->jit_frame;
  964. cur_basic_block = cc->cur_basic_block;
  965. gen_commit_values(jit_frame, jit_frame->lp, jit_frame->sp);
  966. if (!(insn_select && insn_cmp)) {
  967. if (!GEN_INSN(CMP, cc->cmp_reg, cond, NEW_CONST(I32, 0))) {
  968. jit_set_last_error(cc, "generate cmp insn failed");
  969. goto fail;
  970. }
  971. }
  972. /* Only copy parameters or results when their count > 0 and
  973. the src/dst addr are different */
  974. copy_arities = check_copy_arities(block_dst, jit_frame);
  975. if (!copy_arities) {
  976. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  977. if (!(insn = GEN_INSN(
  978. BNE, cc->cmp_reg,
  979. jit_basic_block_label(block_dst->basic_block_entry),
  980. 0))) {
  981. jit_set_last_error(cc, "generate bne insn failed");
  982. goto fail;
  983. }
  984. }
  985. else {
  986. if (!(insn = GEN_INSN(BNE, cc->cmp_reg, 0, 0))) {
  987. jit_set_last_error(cc, "generate bne insn failed");
  988. goto fail;
  989. }
  990. if (!jit_block_add_incoming_insn(block_dst, insn, 1)) {
  991. jit_set_last_error(cc, "add incoming insn failed");
  992. goto fail;
  993. }
  994. }
  995. if (insn_select && insn_cmp) {
  996. /* Change `CMP + SELECTcc` into `CMP + Bcc` */
  997. insn->opcode = JIT_OP_BEQ + (insn_select->opcode - JIT_OP_SELECTEQ);
  998. jit_insn_unlink(insn_select);
  999. jit_insn_delete(insn_select);
  1000. }
  1001. return true;
  1002. }
  1003. CREATE_BASIC_BLOCK(if_basic_block);
  1004. if (!(insn = GEN_INSN(BNE, cc->cmp_reg,
  1005. jit_basic_block_label(if_basic_block), 0))) {
  1006. jit_set_last_error(cc, "generate bne insn failed");
  1007. goto fail;
  1008. }
  1009. if (insn_select && insn_cmp) {
  1010. /* Change `CMP + SELECTcc` into `CMP + Bcc` */
  1011. insn->opcode = JIT_OP_BEQ + (insn_select->opcode - JIT_OP_SELECTEQ);
  1012. jit_insn_unlink(insn_select);
  1013. jit_insn_delete(insn_select);
  1014. }
  1015. #if WASM_ENABLE_THREAD_MGR != 0
  1016. /* Insert suspend check point */
  1017. if (!jit_check_suspend_flags(cc))
  1018. return false;
  1019. #endif
  1020. SET_BUILDER_POS(if_basic_block);
  1021. SET_BB_BEGIN_BCIP(if_basic_block, *p_frame_ip - 1);
  1022. /* Clone current jit frame to a new jit fame */
  1023. if (!(jit_frame_cloned = jit_frame_clone(jit_frame))) {
  1024. jit_set_last_error(cc, "allocate memory failed");
  1025. goto fail;
  1026. }
  1027. /* Clear current jit frame so that the registers
  1028. in the new basic block will be loaded again */
  1029. clear_values(jit_frame);
  1030. if (!handle_op_br(cc, br_depth, p_frame_ip)) {
  1031. jit_free(jit_frame_cloned);
  1032. goto fail;
  1033. }
  1034. /* Restore the jit frame so that the registers can
  1035. be used again in current basic block */
  1036. jit_frame_copy(jit_frame, jit_frame_cloned);
  1037. jit_free(jit_frame_cloned);
  1038. /* Continue processing opcodes after BR_IF */
  1039. SET_BUILDER_POS(cur_basic_block);
  1040. return true;
  1041. fail:
  1042. return false;
  1043. }
  1044. bool
  1045. jit_compile_op_br_table(JitCompContext *cc, uint32 *br_depths, uint32 br_count,
  1046. uint8 **p_frame_ip)
  1047. {
  1048. JitBasicBlock *cur_basic_block;
  1049. JitReg value;
  1050. JitInsn *insn;
  1051. uint32 i = 0;
  1052. JitOpndLookupSwitch *opnd = NULL;
  1053. #if WASM_ENABLE_THREAD_MGR != 0
  1054. /* Insert suspend check point */
  1055. if (!jit_check_suspend_flags(cc))
  1056. return false;
  1057. #endif
  1058. cur_basic_block = cc->cur_basic_block;
  1059. POP_I32(value);
  1060. /* append LOOKUPSWITCH to current basic block */
  1061. gen_commit_values(cc->jit_frame, cc->jit_frame->lp, cc->jit_frame->sp);
  1062. /* Clear frame values */
  1063. clear_values(cc->jit_frame);
  1064. SET_BB_END_BCIP(cur_basic_block, *p_frame_ip - 1);
  1065. /* prepare basic blocks for br */
  1066. insn = GEN_INSN(LOOKUPSWITCH, value, br_count);
  1067. if (NULL == insn) {
  1068. jit_set_last_error(cc, "generate insn LOOKUPSWITCH failed");
  1069. goto fail;
  1070. }
  1071. for (i = 0, opnd = jit_insn_opndls(insn); i < br_count + 1; i++) {
  1072. JitBasicBlock *basic_block = NULL;
  1073. JitBlock *block_dst;
  1074. bool copy_arities;
  1075. if (!(block_dst = get_target_block(cc, br_depths[i]))) {
  1076. goto fail;
  1077. }
  1078. /* Only opy parameters or results when their count > 0 and
  1079. the src/dst addr are different */
  1080. copy_arities = check_copy_arities(block_dst, cc->jit_frame);
  1081. if (!copy_arities) {
  1082. /* No need to create new basic block, direclty jump to
  1083. the existing basic block when no need to copy arities */
  1084. if (i == br_count) {
  1085. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  1086. opnd->default_target =
  1087. jit_basic_block_label(block_dst->basic_block_entry);
  1088. }
  1089. else {
  1090. bh_assert(!block_dst->basic_block_end);
  1091. if (!jit_block_add_incoming_insn(block_dst, insn, i)) {
  1092. jit_set_last_error(cc, "add incoming insn failed");
  1093. goto fail;
  1094. }
  1095. }
  1096. }
  1097. else {
  1098. opnd->match_pairs[i].value = i;
  1099. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  1100. opnd->match_pairs[i].target =
  1101. jit_basic_block_label(block_dst->basic_block_entry);
  1102. }
  1103. else {
  1104. bh_assert(!block_dst->basic_block_end);
  1105. if (!jit_block_add_incoming_insn(block_dst, insn, i)) {
  1106. jit_set_last_error(cc, "add incoming insn failed");
  1107. goto fail;
  1108. }
  1109. }
  1110. }
  1111. continue;
  1112. }
  1113. /* Create new basic block when need to copy arities */
  1114. CREATE_BASIC_BLOCK(basic_block);
  1115. SET_BB_BEGIN_BCIP(basic_block, *p_frame_ip - 1);
  1116. if (i == br_count) {
  1117. opnd->default_target = jit_basic_block_label(basic_block);
  1118. }
  1119. else {
  1120. opnd->match_pairs[i].value = i;
  1121. opnd->match_pairs[i].target = jit_basic_block_label(basic_block);
  1122. }
  1123. SET_BUILDER_POS(basic_block);
  1124. if (!handle_op_br(cc, br_depths[i], p_frame_ip))
  1125. goto fail;
  1126. }
  1127. /* Search next available block to handle */
  1128. return handle_next_reachable_block(cc, p_frame_ip);
  1129. fail:
  1130. return false;
  1131. }
  1132. bool
  1133. jit_compile_op_return(JitCompContext *cc, uint8 **p_frame_ip)
  1134. {
  1135. JitBlock *block_func = cc->block_stack.block_list_head;
  1136. bh_assert(block_func);
  1137. if (!handle_func_return(cc, block_func)) {
  1138. return false;
  1139. }
  1140. SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
  1141. clear_values(cc->jit_frame);
  1142. return handle_next_reachable_block(cc, p_frame_ip);
  1143. }
  1144. bool
  1145. jit_compile_op_unreachable(JitCompContext *cc, uint8 **p_frame_ip)
  1146. {
  1147. if (!jit_emit_exception(cc, EXCE_UNREACHABLE, JIT_OP_JMP, 0, NULL))
  1148. return false;
  1149. return handle_next_reachable_block(cc, p_frame_ip);
  1150. }
  1151. bool
  1152. jit_handle_next_reachable_block(JitCompContext *cc, uint8 **p_frame_ip)
  1153. {
  1154. return handle_next_reachable_block(cc, p_frame_ip);
  1155. }