aot_emit_exception.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_exception.h"
  6. #include "../interpreter/wasm_runtime.h"
  7. #include "../aot/aot_runtime.h"
  8. static bool
  9. commit_ip(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  10. LLVMValueRef exce_ip, bool is_64bit)
  11. {
  12. LLVMValueRef cur_frame = func_ctx->cur_frame;
  13. LLVMValueRef value_offset, value_addr, value_ptr;
  14. uint32 offset_ip;
  15. if (!comp_ctx->is_jit_mode)
  16. offset_ip = comp_ctx->pointer_size * 4;
  17. else
  18. offset_ip = offsetof(WASMInterpFrame, ip);
  19. if (!(value_offset = I32_CONST(offset_ip))) {
  20. aot_set_last_error("llvm build const failed");
  21. return false;
  22. }
  23. if (!(value_addr =
  24. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
  25. &value_offset, 1, "ip_addr"))) {
  26. aot_set_last_error("llvm build in bounds gep failed");
  27. return false;
  28. }
  29. if (!(value_ptr = LLVMBuildBitCast(
  30. comp_ctx->builder, value_addr,
  31. is_64bit ? INT64_PTR_TYPE : INT32_PTR_TYPE, "ip_ptr"))) {
  32. aot_set_last_error("llvm build bit cast failed");
  33. return false;
  34. }
  35. if (!LLVMBuildStore(comp_ctx->builder, exce_ip, value_ptr)) {
  36. aot_set_last_error("llvm build store failed");
  37. return false;
  38. }
  39. return true;
  40. }
  41. bool
  42. aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  43. int32 exception_id, bool is_cond_br, LLVMValueRef cond_br_if,
  44. LLVMBasicBlockRef cond_br_else_block)
  45. {
  46. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  47. LLVMValueRef exce_id = I32_CONST((uint32)exception_id), func_const, func;
  48. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  49. LLVMValueRef param_values[2];
  50. bool is_64bit = (comp_ctx->pointer_size == sizeof(uint64)) ? true : false;
  51. bh_assert(exception_id >= 0 && exception_id < EXCE_NUM);
  52. CHECK_LLVM_CONST(exce_id);
  53. /* Create got_exception block if needed */
  54. if (!func_ctx->got_exception_block) {
  55. if (!(func_ctx->got_exception_block = LLVMAppendBasicBlockInContext(
  56. comp_ctx->context, func_ctx->func, "got_exception"))) {
  57. aot_set_last_error("add LLVM basic block failed.");
  58. return false;
  59. }
  60. LLVMPositionBuilderAtEnd(comp_ctx->builder,
  61. func_ctx->got_exception_block);
  62. /* Create exception id phi */
  63. if (!(func_ctx->exception_id_phi = LLVMBuildPhi(
  64. comp_ctx->builder, I32_TYPE, "exception_id_phi"))) {
  65. aot_set_last_error("llvm build phi failed.");
  66. return false;
  67. }
  68. if (comp_ctx->aot_frame) {
  69. /* Create exception ip phi */
  70. if (!(func_ctx->exception_ip_phi = LLVMBuildPhi(
  71. comp_ctx->builder, is_64bit ? I64_TYPE : I32_TYPE,
  72. "exception_ip_phi"))) {
  73. aot_set_last_error("llvm build phi failed.");
  74. return false;
  75. }
  76. /* Commit ip to current frame */
  77. if (!commit_ip(comp_ctx, func_ctx, func_ctx->exception_ip_phi,
  78. is_64bit)) {
  79. return false;
  80. }
  81. }
  82. /* Call aot_set_exception_with_id() to throw exception */
  83. param_types[0] = INT8_PTR_TYPE;
  84. param_types[1] = I32_TYPE;
  85. ret_type = VOID_TYPE;
  86. /* Create function type */
  87. if (!(func_type = LLVMFunctionType(ret_type, param_types, 2, false))) {
  88. aot_set_last_error("create LLVM function type failed.");
  89. return false;
  90. }
  91. if (comp_ctx->is_jit_mode) {
  92. /* Create function type */
  93. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  94. aot_set_last_error("create LLVM function type failed.");
  95. return false;
  96. }
  97. /* Create LLVM function with const function pointer */
  98. if (!(func_const =
  99. I64_CONST((uint64)(uintptr_t)jit_set_exception_with_id))
  100. || !(func = LLVMConstIntToPtr(func_const, func_ptr_type))) {
  101. aot_set_last_error("create LLVM value failed.");
  102. return false;
  103. }
  104. }
  105. else if (comp_ctx->is_indirect_mode) {
  106. int32 func_index;
  107. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  108. aot_set_last_error("create LLVM function type failed.");
  109. return false;
  110. }
  111. func_index = aot_get_native_symbol_index(
  112. comp_ctx, "aot_set_exception_with_id");
  113. if (func_index < 0) {
  114. return false;
  115. }
  116. if (!(func =
  117. aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  118. func_ptr_type, func_index))) {
  119. return false;
  120. }
  121. }
  122. else {
  123. /* Create LLVM function with external function pointer */
  124. if (!(func = LLVMGetNamedFunction(func_ctx->module,
  125. "aot_set_exception_with_id"))
  126. && !(func = LLVMAddFunction(func_ctx->module,
  127. "aot_set_exception_with_id",
  128. func_type))) {
  129. aot_set_last_error("add LLVM function failed.");
  130. return false;
  131. }
  132. }
  133. /* Call the aot_set_exception_with_id() function */
  134. param_values[0] = func_ctx->aot_inst;
  135. param_values[1] = func_ctx->exception_id_phi;
  136. if (!LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values, 2,
  137. "")) {
  138. aot_set_last_error("llvm build call failed.");
  139. return false;
  140. }
  141. /* Create return IR */
  142. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  143. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  144. return false;
  145. }
  146. /* Resume the builder position */
  147. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  148. }
  149. /* Add phi incoming value to got_exception block */
  150. LLVMAddIncoming(func_ctx->exception_id_phi, &exce_id, &block_curr, 1);
  151. if (comp_ctx->aot_frame) {
  152. const uint8 *ip = comp_ctx->aot_frame->frame_ip;
  153. LLVMValueRef exce_ip = NULL;
  154. if (!comp_ctx->is_jit_mode) {
  155. WASMModule *module = comp_ctx->comp_data->wasm_module;
  156. if (is_64bit)
  157. exce_ip =
  158. I64_CONST((uint64)(uintptr_t)(ip - module->load_addr));
  159. else
  160. exce_ip =
  161. I32_CONST((uint32)(uintptr_t)(ip - module->load_addr));
  162. }
  163. else {
  164. if (is_64bit)
  165. exce_ip = I64_CONST((uint64)(uintptr_t)ip);
  166. else
  167. exce_ip = I32_CONST((uint32)(uintptr_t)ip);
  168. }
  169. if (!exce_ip) {
  170. aot_set_last_error("llvm build const failed");
  171. return false;
  172. }
  173. /* Add phi incoming value to got_exception block */
  174. LLVMAddIncoming(func_ctx->exception_ip_phi, &exce_ip, &block_curr, 1);
  175. }
  176. if (!is_cond_br) {
  177. /* not condition br, create br IR */
  178. if (!LLVMBuildBr(comp_ctx->builder, func_ctx->got_exception_block)) {
  179. aot_set_last_error("llvm build br failed.");
  180. return false;
  181. }
  182. }
  183. else {
  184. /* Create condition br */
  185. if (!LLVMBuildCondBr(comp_ctx->builder, cond_br_if,
  186. func_ctx->got_exception_block,
  187. cond_br_else_block)) {
  188. aot_set_last_error("llvm build cond br failed.");
  189. return false;
  190. }
  191. /* Start to translate the else block */
  192. LLVMPositionBuilderAtEnd(comp_ctx->builder, cond_br_else_block);
  193. }
  194. return true;
  195. fail:
  196. return false;
  197. }