aot_emit_control.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_emit_control.h"
  6. #include "aot_compiler.h"
  7. #include "aot_emit_exception.h"
  8. #include "../aot/aot_runtime.h"
  9. #include "../interpreter/wasm_loader.h"
  10. #if WASM_ENABLE_DEBUG_AOT != 0
  11. #include "debug/dwarf_extractor.h"
  12. #endif
  13. static char *block_name_prefix[] = { "block", "loop", "if" };
  14. static char *block_name_suffix[] = { "begin", "else", "end" };
  15. /* clang-format off */
  16. enum {
  17. LABEL_BEGIN = 0,
  18. LABEL_ELSE,
  19. LABEL_END
  20. };
  21. /* clang-format on */
  22. static void
  23. format_block_name(char *name, uint32 name_size, uint32 block_index,
  24. uint32 label_type, uint32 label_id)
  25. {
  26. if (label_type != LABEL_TYPE_FUNCTION)
  27. snprintf(name, name_size, "%s%d%s%s", block_name_prefix[label_type],
  28. block_index, "_", block_name_suffix[label_id]);
  29. else
  30. snprintf(name, name_size, "%s", "func_end");
  31. }
  32. #define CREATE_BLOCK(new_llvm_block, name) \
  33. do { \
  34. if (!(new_llvm_block = LLVMAppendBasicBlockInContext( \
  35. comp_ctx->context, func_ctx->func, name))) { \
  36. aot_set_last_error("add LLVM basic block failed."); \
  37. goto fail; \
  38. } \
  39. } while (0)
  40. #define CURR_BLOCK() LLVMGetInsertBlock(comp_ctx->builder)
  41. #define MOVE_BLOCK_AFTER(llvm_block, llvm_block_after) \
  42. LLVMMoveBasicBlockAfter(llvm_block, llvm_block_after)
  43. #define MOVE_BLOCK_AFTER_CURR(llvm_block) \
  44. LLVMMoveBasicBlockAfter(llvm_block, CURR_BLOCK())
  45. #define MOVE_BLOCK_BEFORE(llvm_block, llvm_block_before) \
  46. LLVMMoveBasicBlockBefore(llvm_block, llvm_block_before)
  47. #define BUILD_BR(llvm_block) \
  48. do { \
  49. if (!LLVMBuildBr(comp_ctx->builder, llvm_block)) { \
  50. aot_set_last_error("llvm build br failed."); \
  51. goto fail; \
  52. } \
  53. } while (0)
  54. #define BUILD_COND_BR(value_if, block_then, block_else) \
  55. do { \
  56. if (!LLVMBuildCondBr(comp_ctx->builder, value_if, block_then, \
  57. block_else)) { \
  58. aot_set_last_error("llvm build cond br failed."); \
  59. goto fail; \
  60. } \
  61. } while (0)
  62. #define SET_BUILDER_POS(llvm_block) \
  63. LLVMPositionBuilderAtEnd(comp_ctx->builder, llvm_block)
  64. #define CREATE_RESULT_VALUE_PHIS(block) \
  65. do { \
  66. if (block->result_count && !block->result_phis) { \
  67. uint32 _i; \
  68. uint64 _size; \
  69. LLVMBasicBlockRef _block_curr = CURR_BLOCK(); \
  70. /* Allocate memory */ \
  71. _size = sizeof(LLVMValueRef) * (uint64)block->result_count; \
  72. if (_size >= UINT32_MAX \
  73. || !(block->result_phis = \
  74. wasm_runtime_malloc((uint32)_size))) { \
  75. aot_set_last_error("allocate memory failed."); \
  76. goto fail; \
  77. } \
  78. SET_BUILDER_POS(block->llvm_end_block); \
  79. for (_i = 0; _i < block->result_count; _i++) { \
  80. if (!(block->result_phis[_i] = LLVMBuildPhi( \
  81. comp_ctx->builder, \
  82. TO_LLVM_TYPE(block->result_types[_i]), "phi"))) { \
  83. aot_set_last_error("llvm build phi failed."); \
  84. goto fail; \
  85. } \
  86. } \
  87. SET_BUILDER_POS(_block_curr); \
  88. } \
  89. } while (0)
  90. #define ADD_TO_RESULT_PHIS(block, value, idx) \
  91. do { \
  92. LLVMBasicBlockRef _block_curr = CURR_BLOCK(); \
  93. LLVMTypeRef phi_ty = LLVMTypeOf(block->result_phis[idx]); \
  94. LLVMTypeRef value_ty = LLVMTypeOf(value); \
  95. bh_assert(LLVMGetTypeKind(phi_ty) == LLVMGetTypeKind(value_ty)); \
  96. bh_assert(LLVMGetTypeContext(phi_ty) == LLVMGetTypeContext(value_ty)); \
  97. LLVMAddIncoming(block->result_phis[idx], &value, &_block_curr, 1); \
  98. (void)phi_ty; \
  99. (void)value_ty; \
  100. } while (0)
  101. #define BUILD_ICMP(op, left, right, res, name) \
  102. do { \
  103. if (!(res = \
  104. LLVMBuildICmp(comp_ctx->builder, op, left, right, name))) { \
  105. aot_set_last_error("llvm build icmp failed."); \
  106. goto fail; \
  107. } \
  108. } while (0)
  109. #define ADD_TO_PARAM_PHIS(block, value, idx) \
  110. do { \
  111. LLVMBasicBlockRef _block_curr = CURR_BLOCK(); \
  112. LLVMAddIncoming(block->param_phis[idx], &value, &_block_curr, 1); \
  113. } while (0)
  114. static LLVMBasicBlockRef
  115. find_next_llvm_end_block(AOTBlock *block)
  116. {
  117. block = block->prev;
  118. while (block && !block->llvm_end_block)
  119. block = block->prev;
  120. return block ? block->llvm_end_block : NULL;
  121. }
  122. static AOTBlock *
  123. get_target_block(AOTFuncContext *func_ctx, uint32 br_depth)
  124. {
  125. uint32 i = br_depth;
  126. AOTBlock *block = func_ctx->block_stack.block_list_end;
  127. while (i-- > 0 && block) {
  128. block = block->prev;
  129. }
  130. if (!block) {
  131. aot_set_last_error("WASM block stack underflow.");
  132. return NULL;
  133. }
  134. return block;
  135. }
  136. static bool
  137. handle_next_reachable_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  138. uint8 **p_frame_ip)
  139. {
  140. AOTBlock *block = func_ctx->block_stack.block_list_end;
  141. AOTBlock *block_prev;
  142. uint8 *frame_ip = NULL;
  143. uint32 i;
  144. AOTFuncType *func_type;
  145. LLVMValueRef ret;
  146. #if WASM_ENABLE_DEBUG_AOT != 0
  147. LLVMMetadataRef return_location;
  148. #endif
  149. aot_checked_addr_list_destroy(func_ctx);
  150. bh_assert(block);
  151. #if WASM_ENABLE_DEBUG_AOT != 0
  152. return_location = dwarf_gen_location(
  153. comp_ctx, func_ctx,
  154. (*p_frame_ip - 1) - comp_ctx->comp_data->wasm_module->buf_code);
  155. #endif
  156. if (block->label_type == LABEL_TYPE_IF && block->llvm_else_block
  157. && *p_frame_ip <= block->wasm_code_else) {
  158. /* Clear value stack and start to translate else branch */
  159. aot_value_stack_destroy(&block->value_stack);
  160. /* Recover parameters of else branch */
  161. for (i = 0; i < block->param_count; i++)
  162. PUSH(block->else_param_phis[i], block->param_types[i]);
  163. SET_BUILDER_POS(block->llvm_else_block);
  164. *p_frame_ip = block->wasm_code_else + 1;
  165. return true;
  166. }
  167. while (block && !block->is_reachable) {
  168. block_prev = block->prev;
  169. block = aot_block_stack_pop(&func_ctx->block_stack);
  170. if (block->label_type == LABEL_TYPE_IF) {
  171. if (block->llvm_else_block && !block->skip_wasm_code_else
  172. && *p_frame_ip <= block->wasm_code_else) {
  173. /* Clear value stack and start to translate else branch */
  174. aot_value_stack_destroy(&block->value_stack);
  175. SET_BUILDER_POS(block->llvm_else_block);
  176. *p_frame_ip = block->wasm_code_else + 1;
  177. /* Push back the block */
  178. aot_block_stack_push(&func_ctx->block_stack, block);
  179. /* Recover parameters of else branch */
  180. for (i = 0; i < block->param_count; i++)
  181. PUSH(block->else_param_phis[i], block->param_types[i]);
  182. return true;
  183. }
  184. else if (block->llvm_end_block) {
  185. /* Remove unreachable basic block */
  186. LLVMDeleteBasicBlock(block->llvm_end_block);
  187. block->llvm_end_block = NULL;
  188. }
  189. }
  190. frame_ip = block->wasm_code_end;
  191. aot_block_destroy(block);
  192. block = block_prev;
  193. }
  194. if (!block) {
  195. *p_frame_ip = frame_ip + 1;
  196. return true;
  197. }
  198. if (block->label_type == LABEL_TYPE_IF && block->llvm_else_block
  199. && !block->skip_wasm_code_else
  200. && *p_frame_ip <= block->wasm_code_else) {
  201. /* Clear value stack and start to translate else branch */
  202. aot_value_stack_destroy(&block->value_stack);
  203. /* Recover parameters of else branch */
  204. for (i = 0; i < block->param_count; i++)
  205. PUSH(block->else_param_phis[i], block->param_types[i]);
  206. SET_BUILDER_POS(block->llvm_else_block);
  207. *p_frame_ip = block->wasm_code_else + 1;
  208. return true;
  209. }
  210. *p_frame_ip = block->wasm_code_end + 1;
  211. SET_BUILDER_POS(block->llvm_end_block);
  212. /* Pop block, push its return value, and destroy the block */
  213. block = aot_block_stack_pop(&func_ctx->block_stack);
  214. func_type = func_ctx->aot_func->func_type;
  215. for (i = 0; i < block->result_count; i++) {
  216. bh_assert(block->result_phis[i]);
  217. if (block->label_type != LABEL_TYPE_FUNCTION) {
  218. PUSH(block->result_phis[i], block->result_types[i]);
  219. }
  220. else {
  221. /* Store extra return values to function parameters */
  222. if (i != 0) {
  223. LLVMValueRef res;
  224. uint32 param_index = func_type->param_count + i;
  225. if (!(res = LLVMBuildStore(
  226. comp_ctx->builder, block->result_phis[i],
  227. LLVMGetParam(func_ctx->func, param_index)))) {
  228. aot_set_last_error("llvm build store failed.");
  229. goto fail;
  230. }
  231. LLVMSetAlignment(res, 1);
  232. }
  233. }
  234. }
  235. if (block->label_type == LABEL_TYPE_FUNCTION) {
  236. if (block->result_count) {
  237. /* Return the first return value */
  238. if (!(ret =
  239. LLVMBuildRet(comp_ctx->builder, block->result_phis[0]))) {
  240. aot_set_last_error("llvm build return failed.");
  241. goto fail;
  242. }
  243. #if WASM_ENABLE_DEBUG_AOT != 0
  244. LLVMInstructionSetDebugLoc(ret, return_location);
  245. #endif
  246. }
  247. else {
  248. if (!(ret = LLVMBuildRetVoid(comp_ctx->builder))) {
  249. aot_set_last_error("llvm build return void failed.");
  250. goto fail;
  251. }
  252. #if WASM_ENABLE_DEBUG_AOT != 0
  253. LLVMInstructionSetDebugLoc(ret, return_location);
  254. #endif
  255. }
  256. }
  257. aot_block_destroy(block);
  258. return true;
  259. fail:
  260. return false;
  261. }
  262. static bool
  263. push_aot_block_to_stack_and_pass_params(AOTCompContext *comp_ctx,
  264. AOTFuncContext *func_ctx,
  265. AOTBlock *block)
  266. {
  267. uint32 i, param_index;
  268. LLVMValueRef value, br_inst;
  269. uint64 size;
  270. char name[32];
  271. LLVMBasicBlockRef block_curr = CURR_BLOCK();
  272. if (block->param_count) {
  273. size = sizeof(LLVMValueRef) * (uint64)block->param_count;
  274. if (size >= UINT32_MAX
  275. || !(block->param_phis = wasm_runtime_malloc((uint32)size))) {
  276. aot_set_last_error("allocate memory failed.");
  277. return false;
  278. }
  279. if (block->label_type == LABEL_TYPE_IF && !block->skip_wasm_code_else
  280. && !(block->else_param_phis = wasm_runtime_malloc((uint32)size))) {
  281. wasm_runtime_free(block->param_phis);
  282. block->param_phis = NULL;
  283. aot_set_last_error("allocate memory failed.");
  284. return false;
  285. }
  286. /* Create param phis */
  287. for (i = 0; i < block->param_count; i++) {
  288. SET_BUILDER_POS(block->llvm_entry_block);
  289. snprintf(name, sizeof(name), "%s%d_phi%d",
  290. block_name_prefix[block->label_type], block->block_index,
  291. i);
  292. if (!(block->param_phis[i] = LLVMBuildPhi(
  293. comp_ctx->builder, TO_LLVM_TYPE(block->param_types[i]),
  294. name))) {
  295. aot_set_last_error("llvm build phi failed.");
  296. goto fail;
  297. }
  298. if (block->label_type == LABEL_TYPE_IF
  299. && !block->skip_wasm_code_else && block->llvm_else_block) {
  300. /* Build else param phis */
  301. SET_BUILDER_POS(block->llvm_else_block);
  302. snprintf(name, sizeof(name), "else%d_phi%d", block->block_index,
  303. i);
  304. if (!(block->else_param_phis[i] = LLVMBuildPhi(
  305. comp_ctx->builder,
  306. TO_LLVM_TYPE(block->param_types[i]), name))) {
  307. aot_set_last_error("llvm build phi failed.");
  308. goto fail;
  309. }
  310. }
  311. }
  312. /* At this point, the branch instruction was already built to jump to
  313. * the new BB, to avoid generating zext instruction from the popped
  314. * operand that would come after branch instruction, we should position
  315. * the builder before the last branch instruction */
  316. br_inst = LLVMGetLastInstruction(block_curr);
  317. bh_assert(LLVMGetInstructionOpcode(br_inst) == LLVMBr);
  318. LLVMPositionBuilderBefore(comp_ctx->builder, br_inst);
  319. /* Pop param values from current block's
  320. * value stack and add to param phis.
  321. */
  322. for (i = 0; i < block->param_count; i++) {
  323. param_index = block->param_count - 1 - i;
  324. POP(value, block->param_types[param_index]);
  325. if (block->llvm_entry_block)
  326. /* Only add incoming phis if the entry block was created */
  327. ADD_TO_PARAM_PHIS(block, value, param_index);
  328. if (block->label_type == LABEL_TYPE_IF
  329. && !block->skip_wasm_code_else) {
  330. if (block->llvm_else_block) {
  331. /* has else branch, add to else param phis */
  332. LLVMAddIncoming(block->else_param_phis[param_index], &value,
  333. &block_curr, 1);
  334. }
  335. else {
  336. /* no else branch, add to result phis */
  337. CREATE_RESULT_VALUE_PHIS(block);
  338. ADD_TO_RESULT_PHIS(block, value, param_index);
  339. }
  340. }
  341. }
  342. }
  343. /* Push the new block to block stack */
  344. aot_block_stack_push(&func_ctx->block_stack, block);
  345. /* Push param phis to the new block */
  346. for (i = 0; i < block->param_count; i++) {
  347. if (block->llvm_entry_block)
  348. /* Push param phis if the entry basic block was created */
  349. PUSH(block->param_phis[i], block->param_types[i]);
  350. else {
  351. bh_assert(block->label_type == LABEL_TYPE_IF
  352. && block->llvm_else_block && block->else_param_phis
  353. && !block->skip_wasm_code_else);
  354. /* Push else param phis if we start to translate the
  355. else branch */
  356. PUSH(block->else_param_phis[i], block->param_types[i]);
  357. }
  358. }
  359. return true;
  360. fail:
  361. if (block->param_phis) {
  362. wasm_runtime_free(block->param_phis);
  363. block->param_phis = NULL;
  364. }
  365. if (block->else_param_phis) {
  366. wasm_runtime_free(block->else_param_phis);
  367. block->else_param_phis = NULL;
  368. }
  369. return false;
  370. }
  371. bool
  372. aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  373. uint8 **p_frame_ip, uint8 *frame_ip_end, uint32 label_type,
  374. uint32 param_count, uint8 *param_types,
  375. uint32 result_count, uint8 *result_types)
  376. {
  377. BlockAddr block_addr_cache[BLOCK_ADDR_CACHE_SIZE][BLOCK_ADDR_CONFLICT_SIZE];
  378. AOTBlock *block;
  379. uint8 *else_addr, *end_addr;
  380. LLVMValueRef value;
  381. char name[32];
  382. /* Check block stack */
  383. if (!func_ctx->block_stack.block_list_end) {
  384. aot_set_last_error("WASM block stack underflow.");
  385. return false;
  386. }
  387. memset(block_addr_cache, 0, sizeof(block_addr_cache));
  388. /* Get block info */
  389. if (!(wasm_loader_find_block_addr(
  390. NULL, (BlockAddr *)block_addr_cache, *p_frame_ip, frame_ip_end,
  391. (uint8)label_type, &else_addr, &end_addr))) {
  392. aot_set_last_error("find block end addr failed.");
  393. return false;
  394. }
  395. /* Allocate memory */
  396. if (!(block = wasm_runtime_malloc(sizeof(AOTBlock)))) {
  397. aot_set_last_error("allocate memory failed.");
  398. return false;
  399. }
  400. memset(block, 0, sizeof(AOTBlock));
  401. if (param_count
  402. && !(block->param_types = wasm_runtime_malloc(param_count))) {
  403. aot_set_last_error("allocate memory failed.");
  404. goto fail;
  405. }
  406. if (result_count) {
  407. if (!(block->result_types = wasm_runtime_malloc(result_count))) {
  408. aot_set_last_error("allocate memory failed.");
  409. goto fail;
  410. }
  411. }
  412. /* Init aot block data */
  413. block->label_type = label_type;
  414. block->param_count = param_count;
  415. if (param_count) {
  416. bh_memcpy_s(block->param_types, param_count, param_types, param_count);
  417. }
  418. block->result_count = result_count;
  419. if (result_count) {
  420. bh_memcpy_s(block->result_types, result_count, result_types,
  421. result_count);
  422. }
  423. block->wasm_code_else = else_addr;
  424. block->wasm_code_end = end_addr;
  425. block->block_index = func_ctx->block_stack.block_index[label_type];
  426. func_ctx->block_stack.block_index[label_type]++;
  427. if (label_type == LABEL_TYPE_BLOCK || label_type == LABEL_TYPE_LOOP) {
  428. /* Create block */
  429. format_block_name(name, sizeof(name), block->block_index, label_type,
  430. LABEL_BEGIN);
  431. CREATE_BLOCK(block->llvm_entry_block, name);
  432. MOVE_BLOCK_AFTER_CURR(block->llvm_entry_block);
  433. /* Jump to the entry block */
  434. BUILD_BR(block->llvm_entry_block);
  435. if (!push_aot_block_to_stack_and_pass_params(comp_ctx, func_ctx, block))
  436. goto fail;
  437. /* Start to translate the block */
  438. SET_BUILDER_POS(block->llvm_entry_block);
  439. if (label_type == LABEL_TYPE_LOOP)
  440. aot_checked_addr_list_destroy(func_ctx);
  441. }
  442. else if (label_type == LABEL_TYPE_IF) {
  443. POP_COND(value);
  444. if (LLVMIsUndef(value)
  445. #if LLVM_VERSION_NUMBER >= 12
  446. || LLVMIsPoison(value)
  447. #endif
  448. ) {
  449. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INTEGER_OVERFLOW,
  450. false, NULL, NULL))) {
  451. goto fail;
  452. }
  453. aot_block_destroy(block);
  454. return aot_handle_next_reachable_block(comp_ctx, func_ctx,
  455. p_frame_ip);
  456. }
  457. if (!LLVMIsEfficientConstInt(value)) {
  458. /* Compare value is not constant, create condition br IR */
  459. /* Create entry block */
  460. format_block_name(name, sizeof(name), block->block_index,
  461. label_type, LABEL_BEGIN);
  462. CREATE_BLOCK(block->llvm_entry_block, name);
  463. MOVE_BLOCK_AFTER_CURR(block->llvm_entry_block);
  464. /* Create end block */
  465. format_block_name(name, sizeof(name), block->block_index,
  466. label_type, LABEL_END);
  467. CREATE_BLOCK(block->llvm_end_block, name);
  468. MOVE_BLOCK_AFTER(block->llvm_end_block, block->llvm_entry_block);
  469. if (else_addr) {
  470. /* Create else block */
  471. format_block_name(name, sizeof(name), block->block_index,
  472. label_type, LABEL_ELSE);
  473. CREATE_BLOCK(block->llvm_else_block, name);
  474. MOVE_BLOCK_AFTER(block->llvm_else_block,
  475. block->llvm_entry_block);
  476. /* Create condition br IR */
  477. BUILD_COND_BR(value, block->llvm_entry_block,
  478. block->llvm_else_block);
  479. }
  480. else {
  481. /* Create condition br IR */
  482. BUILD_COND_BR(value, block->llvm_entry_block,
  483. block->llvm_end_block);
  484. block->is_reachable = true;
  485. }
  486. if (!push_aot_block_to_stack_and_pass_params(comp_ctx, func_ctx,
  487. block))
  488. goto fail;
  489. /* Start to translate if branch of BLOCK if */
  490. SET_BUILDER_POS(block->llvm_entry_block);
  491. }
  492. else {
  493. if ((int32)LLVMConstIntGetZExtValue(value) != 0) {
  494. /* Compare value is not 0, condition is true, else branch of
  495. BLOCK if cannot be reached */
  496. block->skip_wasm_code_else = true;
  497. /* Create entry block */
  498. format_block_name(name, sizeof(name), block->block_index,
  499. label_type, LABEL_BEGIN);
  500. CREATE_BLOCK(block->llvm_entry_block, name);
  501. MOVE_BLOCK_AFTER_CURR(block->llvm_entry_block);
  502. /* Jump to the entry block */
  503. BUILD_BR(block->llvm_entry_block);
  504. if (!push_aot_block_to_stack_and_pass_params(comp_ctx, func_ctx,
  505. block))
  506. goto fail;
  507. /* Start to translate the if branch */
  508. SET_BUILDER_POS(block->llvm_entry_block);
  509. }
  510. else {
  511. /* Compare value is not 0, condition is false, if branch of
  512. BLOCK if cannot be reached */
  513. if (else_addr) {
  514. /* Create else block */
  515. format_block_name(name, sizeof(name), block->block_index,
  516. label_type, LABEL_ELSE);
  517. CREATE_BLOCK(block->llvm_else_block, name);
  518. MOVE_BLOCK_AFTER_CURR(block->llvm_else_block);
  519. /* Jump to the else block */
  520. BUILD_BR(block->llvm_else_block);
  521. if (!push_aot_block_to_stack_and_pass_params(
  522. comp_ctx, func_ctx, block))
  523. goto fail;
  524. /* Start to translate the else branch */
  525. SET_BUILDER_POS(block->llvm_else_block);
  526. *p_frame_ip = else_addr + 1;
  527. }
  528. else {
  529. /* skip the block */
  530. aot_block_destroy(block);
  531. *p_frame_ip = end_addr + 1;
  532. }
  533. }
  534. }
  535. }
  536. else {
  537. aot_set_last_error("Invalid block type.");
  538. goto fail;
  539. }
  540. return true;
  541. fail:
  542. aot_block_destroy(block);
  543. return false;
  544. }
  545. bool
  546. aot_compile_op_else(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  547. uint8 **p_frame_ip)
  548. {
  549. AOTBlock *block = func_ctx->block_stack.block_list_end;
  550. LLVMValueRef value;
  551. char name[32];
  552. uint32 i, result_index;
  553. /* Check block */
  554. if (!block) {
  555. aot_set_last_error("WASM block stack underflow.");
  556. return false;
  557. }
  558. if (block->label_type != LABEL_TYPE_IF
  559. || (!block->skip_wasm_code_else && !block->llvm_else_block)) {
  560. aot_set_last_error("Invalid WASM block type.");
  561. return false;
  562. }
  563. /* Create end block if needed */
  564. if (!block->llvm_end_block) {
  565. format_block_name(name, sizeof(name), block->block_index,
  566. block->label_type, LABEL_END);
  567. CREATE_BLOCK(block->llvm_end_block, name);
  568. if (block->llvm_else_block)
  569. MOVE_BLOCK_AFTER(block->llvm_end_block, block->llvm_else_block);
  570. else
  571. MOVE_BLOCK_AFTER_CURR(block->llvm_end_block);
  572. }
  573. block->is_reachable = true;
  574. /* Comes from the if branch of BLOCK if */
  575. CREATE_RESULT_VALUE_PHIS(block);
  576. for (i = 0; i < block->result_count; i++) {
  577. result_index = block->result_count - 1 - i;
  578. POP(value, block->result_types[result_index]);
  579. ADD_TO_RESULT_PHIS(block, value, result_index);
  580. }
  581. /* Jump to end block */
  582. BUILD_BR(block->llvm_end_block);
  583. if (!block->skip_wasm_code_else && block->llvm_else_block) {
  584. /* Clear value stack, recover param values
  585. * and start to translate else branch.
  586. */
  587. aot_value_stack_destroy(&block->value_stack);
  588. for (i = 0; i < block->param_count; i++)
  589. PUSH(block->else_param_phis[i], block->param_types[i]);
  590. SET_BUILDER_POS(block->llvm_else_block);
  591. aot_checked_addr_list_destroy(func_ctx);
  592. return true;
  593. }
  594. /* No else branch or no need to translate else branch */
  595. block->is_reachable = true;
  596. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  597. fail:
  598. return false;
  599. }
  600. bool
  601. aot_compile_op_end(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  602. uint8 **p_frame_ip)
  603. {
  604. AOTBlock *block;
  605. LLVMValueRef value;
  606. LLVMBasicBlockRef next_llvm_end_block;
  607. char name[32];
  608. uint32 i, result_index;
  609. /* Check block stack */
  610. if (!(block = func_ctx->block_stack.block_list_end)) {
  611. aot_set_last_error("WASM block stack underflow.");
  612. return false;
  613. }
  614. /* Create the end block */
  615. if (!block->llvm_end_block) {
  616. format_block_name(name, sizeof(name), block->block_index,
  617. block->label_type, LABEL_END);
  618. CREATE_BLOCK(block->llvm_end_block, name);
  619. if ((next_llvm_end_block = find_next_llvm_end_block(block)))
  620. MOVE_BLOCK_BEFORE(block->llvm_end_block, next_llvm_end_block);
  621. }
  622. /* Handle block result values */
  623. CREATE_RESULT_VALUE_PHIS(block);
  624. for (i = 0; i < block->result_count; i++) {
  625. value = NULL;
  626. result_index = block->result_count - 1 - i;
  627. POP(value, block->result_types[result_index]);
  628. bh_assert(value);
  629. ADD_TO_RESULT_PHIS(block, value, result_index);
  630. }
  631. /* Jump to the end block */
  632. BUILD_BR(block->llvm_end_block);
  633. block->is_reachable = true;
  634. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  635. fail:
  636. return false;
  637. }
  638. #if WASM_ENABLE_THREAD_MGR != 0
  639. bool
  640. check_suspend_flags(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  641. {
  642. LLVMValueRef terminate_addr, terminate_flags, flag, offset, res;
  643. LLVMBasicBlockRef terminate_block, non_terminate_block;
  644. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  645. bool is_shared_memory =
  646. comp_ctx->comp_data->memories[0].memory_flags & 0x02 ? true : false;
  647. /* Only need to check the suspend flags when memory is shared since
  648. shared memory must be enabled for multi-threading */
  649. if (!is_shared_memory) {
  650. return true;
  651. }
  652. /* Offset of suspend_flags */
  653. offset = I32_FIVE;
  654. if (!(terminate_addr = LLVMBuildInBoundsGEP2(
  655. comp_ctx->builder, OPQ_PTR_TYPE, func_ctx->exec_env, &offset, 1,
  656. "terminate_addr"))) {
  657. aot_set_last_error("llvm build in bounds gep failed");
  658. return false;
  659. }
  660. if (!(terminate_addr =
  661. LLVMBuildBitCast(comp_ctx->builder, terminate_addr,
  662. INT32_PTR_TYPE, "terminate_addr_ptr"))) {
  663. aot_set_last_error("llvm build bit cast failed");
  664. return false;
  665. }
  666. if (!(terminate_flags =
  667. LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, terminate_addr,
  668. "terminate_flags"))) {
  669. aot_set_last_error("llvm build LOAD failed");
  670. return false;
  671. }
  672. /* Set terminate_flags memory accecc to volatile, so that the value
  673. will always be loaded from memory rather than register */
  674. LLVMSetVolatile(terminate_flags, true);
  675. if (!(flag = LLVMBuildAnd(comp_ctx->builder, terminate_flags, I32_ONE,
  676. "termination_flag"))) {
  677. aot_set_last_error("llvm build AND failed");
  678. return false;
  679. }
  680. CREATE_BLOCK(non_terminate_block, "non_terminate");
  681. MOVE_BLOCK_AFTER_CURR(non_terminate_block);
  682. CREATE_BLOCK(terminate_block, "terminate");
  683. MOVE_BLOCK_AFTER_CURR(terminate_block);
  684. BUILD_ICMP(LLVMIntEQ, flag, I32_ZERO, res, "flag_terminate");
  685. BUILD_COND_BR(res, non_terminate_block, terminate_block);
  686. /* Move builder to terminate block */
  687. SET_BUILDER_POS(terminate_block);
  688. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  689. goto fail;
  690. }
  691. /* Move builder to non terminate block */
  692. SET_BUILDER_POS(non_terminate_block);
  693. return true;
  694. fail:
  695. return false;
  696. }
  697. #endif /* End of WASM_ENABLE_THREAD_MGR */
  698. bool
  699. aot_compile_op_br(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  700. uint32 br_depth, uint8 **p_frame_ip)
  701. {
  702. AOTBlock *block_dst;
  703. LLVMValueRef value_ret, value_param;
  704. LLVMBasicBlockRef next_llvm_end_block;
  705. char name[32];
  706. uint32 i, param_index, result_index;
  707. #if WASM_ENABLE_THREAD_MGR != 0
  708. /* Insert suspend check point */
  709. if (comp_ctx->enable_thread_mgr) {
  710. if (!check_suspend_flags(comp_ctx, func_ctx))
  711. return false;
  712. }
  713. #endif
  714. if (!(block_dst = get_target_block(func_ctx, br_depth))) {
  715. return false;
  716. }
  717. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  718. /* Dest block is Loop block */
  719. /* Handle Loop parameters */
  720. for (i = 0; i < block_dst->param_count; i++) {
  721. param_index = block_dst->param_count - 1 - i;
  722. POP(value_param, block_dst->param_types[param_index]);
  723. ADD_TO_PARAM_PHIS(block_dst, value_param, param_index);
  724. }
  725. BUILD_BR(block_dst->llvm_entry_block);
  726. }
  727. else {
  728. /* Dest block is Block/If/Function block */
  729. /* Create the end block */
  730. if (!block_dst->llvm_end_block) {
  731. format_block_name(name, sizeof(name), block_dst->block_index,
  732. block_dst->label_type, LABEL_END);
  733. CREATE_BLOCK(block_dst->llvm_end_block, name);
  734. if ((next_llvm_end_block = find_next_llvm_end_block(block_dst)))
  735. MOVE_BLOCK_BEFORE(block_dst->llvm_end_block,
  736. next_llvm_end_block);
  737. }
  738. block_dst->is_reachable = true;
  739. /* Handle result values */
  740. CREATE_RESULT_VALUE_PHIS(block_dst);
  741. for (i = 0; i < block_dst->result_count; i++) {
  742. result_index = block_dst->result_count - 1 - i;
  743. POP(value_ret, block_dst->result_types[result_index]);
  744. ADD_TO_RESULT_PHIS(block_dst, value_ret, result_index);
  745. }
  746. /* Jump to the end block */
  747. BUILD_BR(block_dst->llvm_end_block);
  748. }
  749. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  750. fail:
  751. return false;
  752. }
  753. bool
  754. aot_compile_op_br_if(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  755. uint32 br_depth, uint8 **p_frame_ip)
  756. {
  757. AOTBlock *block_dst;
  758. LLVMValueRef value_cmp, value, *values = NULL;
  759. LLVMBasicBlockRef llvm_else_block, next_llvm_end_block;
  760. char name[32];
  761. uint32 i, param_index, result_index;
  762. uint64 size;
  763. #if WASM_ENABLE_THREAD_MGR != 0
  764. /* Insert suspend check point */
  765. if (comp_ctx->enable_thread_mgr) {
  766. if (!check_suspend_flags(comp_ctx, func_ctx))
  767. return false;
  768. }
  769. #endif
  770. POP_COND(value_cmp);
  771. if (LLVMIsUndef(value_cmp)
  772. #if LLVM_VERSION_NUMBER >= 12
  773. || LLVMIsPoison(value_cmp)
  774. #endif
  775. ) {
  776. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INTEGER_OVERFLOW,
  777. false, NULL, NULL))) {
  778. goto fail;
  779. }
  780. return aot_handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  781. }
  782. if (!LLVMIsEfficientConstInt(value_cmp)) {
  783. /* Compare value is not constant, create condition br IR */
  784. if (!(block_dst = get_target_block(func_ctx, br_depth))) {
  785. return false;
  786. }
  787. /* Create llvm else block */
  788. CREATE_BLOCK(llvm_else_block, "br_if_else");
  789. MOVE_BLOCK_AFTER_CURR(llvm_else_block);
  790. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  791. /* Dest block is Loop block */
  792. /* Handle Loop parameters */
  793. if (block_dst->param_count) {
  794. size = sizeof(LLVMValueRef) * (uint64)block_dst->param_count;
  795. if (size >= UINT32_MAX
  796. || !(values = wasm_runtime_malloc((uint32)size))) {
  797. aot_set_last_error("allocate memory failed.");
  798. goto fail;
  799. }
  800. for (i = 0; i < block_dst->param_count; i++) {
  801. param_index = block_dst->param_count - 1 - i;
  802. POP(value, block_dst->param_types[param_index]);
  803. ADD_TO_PARAM_PHIS(block_dst, value, param_index);
  804. values[param_index] = value;
  805. }
  806. for (i = 0; i < block_dst->param_count; i++) {
  807. PUSH(values[i], block_dst->param_types[i]);
  808. }
  809. wasm_runtime_free(values);
  810. values = NULL;
  811. }
  812. BUILD_COND_BR(value_cmp, block_dst->llvm_entry_block,
  813. llvm_else_block);
  814. /* Move builder to else block */
  815. SET_BUILDER_POS(llvm_else_block);
  816. }
  817. else {
  818. /* Dest block is Block/If/Function block */
  819. /* Create the end block */
  820. if (!block_dst->llvm_end_block) {
  821. format_block_name(name, sizeof(name), block_dst->block_index,
  822. block_dst->label_type, LABEL_END);
  823. CREATE_BLOCK(block_dst->llvm_end_block, name);
  824. if ((next_llvm_end_block = find_next_llvm_end_block(block_dst)))
  825. MOVE_BLOCK_BEFORE(block_dst->llvm_end_block,
  826. next_llvm_end_block);
  827. }
  828. /* Set reachable flag and create condition br IR */
  829. block_dst->is_reachable = true;
  830. /* Handle result values */
  831. if (block_dst->result_count) {
  832. size = sizeof(LLVMValueRef) * (uint64)block_dst->result_count;
  833. if (size >= UINT32_MAX
  834. || !(values = wasm_runtime_malloc((uint32)size))) {
  835. aot_set_last_error("allocate memory failed.");
  836. goto fail;
  837. }
  838. CREATE_RESULT_VALUE_PHIS(block_dst);
  839. for (i = 0; i < block_dst->result_count; i++) {
  840. result_index = block_dst->result_count - 1 - i;
  841. POP(value, block_dst->result_types[result_index]);
  842. values[result_index] = value;
  843. ADD_TO_RESULT_PHIS(block_dst, value, result_index);
  844. }
  845. for (i = 0; i < block_dst->result_count; i++) {
  846. PUSH(values[i], block_dst->result_types[i]);
  847. }
  848. wasm_runtime_free(values);
  849. values = NULL;
  850. }
  851. /* Condition jump to end block */
  852. BUILD_COND_BR(value_cmp, block_dst->llvm_end_block,
  853. llvm_else_block);
  854. /* Move builder to else block */
  855. SET_BUILDER_POS(llvm_else_block);
  856. }
  857. }
  858. else {
  859. if ((int32)LLVMConstIntGetZExtValue(value_cmp) != 0) {
  860. /* Compare value is not 0, condition is true, same as op_br */
  861. return aot_compile_op_br(comp_ctx, func_ctx, br_depth, p_frame_ip);
  862. }
  863. else {
  864. /* Compare value is not 0, condition is false, skip br_if */
  865. return true;
  866. }
  867. }
  868. return true;
  869. fail:
  870. if (values)
  871. wasm_runtime_free(values);
  872. return false;
  873. }
  874. bool
  875. aot_compile_op_br_table(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  876. uint32 *br_depths, uint32 br_count, uint8 **p_frame_ip)
  877. {
  878. uint32 i, j;
  879. LLVMValueRef value_switch, value_cmp, value_case, value, *values = NULL;
  880. LLVMBasicBlockRef default_llvm_block = NULL, target_llvm_block;
  881. LLVMBasicBlockRef next_llvm_end_block;
  882. AOTBlock *target_block;
  883. uint32 br_depth, depth_idx;
  884. uint32 param_index, result_index;
  885. uint64 size;
  886. char name[32];
  887. #if WASM_ENABLE_THREAD_MGR != 0
  888. /* Insert suspend check point */
  889. if (comp_ctx->enable_thread_mgr) {
  890. if (!check_suspend_flags(comp_ctx, func_ctx))
  891. return false;
  892. }
  893. #endif
  894. POP_I32(value_cmp);
  895. if (LLVMIsUndef(value_cmp)
  896. #if LLVM_VERSION_NUMBER >= 12
  897. || LLVMIsPoison(value_cmp)
  898. #endif
  899. ) {
  900. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INTEGER_OVERFLOW,
  901. false, NULL, NULL))) {
  902. goto fail;
  903. }
  904. return aot_handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  905. }
  906. if (!LLVMIsEfficientConstInt(value_cmp)) {
  907. /* Compare value is not constant, create switch IR */
  908. for (i = 0; i <= br_count; i++) {
  909. target_block = get_target_block(func_ctx, br_depths[i]);
  910. if (!target_block)
  911. return false;
  912. if (target_block->label_type != LABEL_TYPE_LOOP) {
  913. /* Dest block is Block/If/Function block */
  914. /* Create the end block */
  915. if (!target_block->llvm_end_block) {
  916. format_block_name(name, sizeof(name),
  917. target_block->block_index,
  918. target_block->label_type, LABEL_END);
  919. CREATE_BLOCK(target_block->llvm_end_block, name);
  920. if ((next_llvm_end_block =
  921. find_next_llvm_end_block(target_block)))
  922. MOVE_BLOCK_BEFORE(target_block->llvm_end_block,
  923. next_llvm_end_block);
  924. }
  925. /* Handle result values */
  926. if (target_block->result_count) {
  927. size = sizeof(LLVMValueRef)
  928. * (uint64)target_block->result_count;
  929. if (size >= UINT32_MAX
  930. || !(values = wasm_runtime_malloc((uint32)size))) {
  931. aot_set_last_error("allocate memory failed.");
  932. goto fail;
  933. }
  934. CREATE_RESULT_VALUE_PHIS(target_block);
  935. for (j = 0; j < target_block->result_count; j++) {
  936. result_index = target_block->result_count - 1 - j;
  937. POP(value, target_block->result_types[result_index]);
  938. values[result_index] = value;
  939. ADD_TO_RESULT_PHIS(target_block, value, result_index);
  940. }
  941. for (j = 0; j < target_block->result_count; j++) {
  942. PUSH(values[j], target_block->result_types[j]);
  943. }
  944. wasm_runtime_free(values);
  945. }
  946. target_block->is_reachable = true;
  947. if (i == br_count)
  948. default_llvm_block = target_block->llvm_end_block;
  949. }
  950. else {
  951. /* Handle Loop parameters */
  952. if (target_block->param_count) {
  953. size = sizeof(LLVMValueRef)
  954. * (uint64)target_block->param_count;
  955. if (size >= UINT32_MAX
  956. || !(values = wasm_runtime_malloc((uint32)size))) {
  957. aot_set_last_error("allocate memory failed.");
  958. goto fail;
  959. }
  960. for (j = 0; j < target_block->param_count; j++) {
  961. param_index = target_block->param_count - 1 - j;
  962. POP(value, target_block->param_types[param_index]);
  963. values[param_index] = value;
  964. ADD_TO_PARAM_PHIS(target_block, value, param_index);
  965. }
  966. for (j = 0; j < target_block->param_count; j++) {
  967. PUSH(values[j], target_block->param_types[j]);
  968. }
  969. wasm_runtime_free(values);
  970. }
  971. if (i == br_count)
  972. default_llvm_block = target_block->llvm_entry_block;
  973. }
  974. }
  975. /* Create switch IR */
  976. if (!(value_switch = LLVMBuildSwitch(comp_ctx->builder, value_cmp,
  977. default_llvm_block, br_count))) {
  978. aot_set_last_error("llvm build switch failed.");
  979. return false;
  980. }
  981. /* Add each case for switch IR */
  982. for (i = 0; i < br_count; i++) {
  983. value_case = I32_CONST(i);
  984. CHECK_LLVM_CONST(value_case);
  985. target_block = get_target_block(func_ctx, br_depths[i]);
  986. if (!target_block)
  987. return false;
  988. target_llvm_block = target_block->label_type != LABEL_TYPE_LOOP
  989. ? target_block->llvm_end_block
  990. : target_block->llvm_entry_block;
  991. LLVMAddCase(value_switch, value_case, target_llvm_block);
  992. }
  993. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  994. }
  995. else {
  996. /* Compare value is constant, create br IR */
  997. depth_idx = (uint32)LLVMConstIntGetZExtValue(value_cmp);
  998. br_depth = br_depths[br_count];
  999. if (depth_idx < br_count) {
  1000. br_depth = br_depths[depth_idx];
  1001. }
  1002. return aot_compile_op_br(comp_ctx, func_ctx, br_depth, p_frame_ip);
  1003. }
  1004. fail:
  1005. if (values)
  1006. wasm_runtime_free(values);
  1007. return false;
  1008. }
  1009. bool
  1010. aot_compile_op_return(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1011. uint8 **p_frame_ip)
  1012. {
  1013. AOTBlock *block_func = func_ctx->block_stack.block_list_head;
  1014. LLVMValueRef value;
  1015. LLVMValueRef ret;
  1016. AOTFuncType *func_type;
  1017. uint32 i, param_index, result_index;
  1018. #if WASM_ENABLE_DEBUG_AOT != 0
  1019. LLVMMetadataRef return_location;
  1020. #endif
  1021. bh_assert(block_func);
  1022. func_type = func_ctx->aot_func->func_type;
  1023. #if WASM_ENABLE_DEBUG_AOT != 0
  1024. return_location = dwarf_gen_location(
  1025. comp_ctx, func_ctx,
  1026. (*p_frame_ip - 1) - comp_ctx->comp_data->wasm_module->buf_code);
  1027. #endif
  1028. if (block_func->result_count) {
  1029. /* Store extra result values to function parameters */
  1030. for (i = 0; i < block_func->result_count - 1; i++) {
  1031. LLVMValueRef res;
  1032. result_index = block_func->result_count - 1 - i;
  1033. POP(value, block_func->result_types[result_index]);
  1034. param_index = func_type->param_count + result_index;
  1035. if (!(res = LLVMBuildStore(
  1036. comp_ctx->builder, value,
  1037. LLVMGetParam(func_ctx->func, param_index)))) {
  1038. aot_set_last_error("llvm build store failed.");
  1039. goto fail;
  1040. }
  1041. LLVMSetAlignment(res, 1);
  1042. }
  1043. /* Return the first result value */
  1044. POP(value, block_func->result_types[0]);
  1045. if (!(ret = LLVMBuildRet(comp_ctx->builder, value))) {
  1046. aot_set_last_error("llvm build return failed.");
  1047. goto fail;
  1048. }
  1049. #if WASM_ENABLE_DEBUG_AOT != 0
  1050. LLVMInstructionSetDebugLoc(ret, return_location);
  1051. #endif
  1052. }
  1053. else {
  1054. if (!(ret = LLVMBuildRetVoid(comp_ctx->builder))) {
  1055. aot_set_last_error("llvm build return void failed.");
  1056. goto fail;
  1057. }
  1058. #if WASM_ENABLE_DEBUG_AOT != 0
  1059. LLVMInstructionSetDebugLoc(ret, return_location);
  1060. #endif
  1061. }
  1062. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  1063. fail:
  1064. return false;
  1065. }
  1066. bool
  1067. aot_compile_op_unreachable(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1068. uint8 **p_frame_ip)
  1069. {
  1070. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_UNREACHABLE, false, NULL,
  1071. NULL))
  1072. return false;
  1073. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  1074. }
  1075. bool
  1076. aot_handle_next_reachable_block(AOTCompContext *comp_ctx,
  1077. AOTFuncContext *func_ctx, uint8 **p_frame_ip)
  1078. {
  1079. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  1080. }