aot_emit_exception.c 6.9 KB

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