aot_emit_exception.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/aot_runtime.h"
  7. static char *exce_block_names[] = {
  8. "exce_unreachable", /* EXCE_UNREACHABLE */
  9. "exce_out_of_memory", /* EXCE_OUT_OF_MEMORY */
  10. "exce_out_of_bounds_mem_access",/* EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS */
  11. "exce_integer_overflow", /* EXCE_INTEGER_OVERFLOW */
  12. "exce_divide_by_zero", /* EXCE_INTEGER_DIVIDE_BY_ZERO */
  13. "exce_invalid_convert_to_int", /* EXCE_INVALID_CONVERSION_TO_INTEGER */
  14. "exce_invalid_func_type_idx", /* EXCE_INVALID_FUNCTION_TYPE_INDEX */
  15. "exce_invalid_func_idx", /* EXCE_INVALID_FUNCTION_INDEX */
  16. "exce_undefined_element", /* EXCE_UNDEFINED_ELEMENT */
  17. "exce_uninit_element", /* EXCE_UNINITIALIZED_ELEMENT */
  18. "exce_call_unlinked", /* EXCE_CALL_UNLINKED_IMPORT_FUNC */
  19. "exce_native_stack_overflow" /* EXCE_NATIVE_STACK_OVERFLOW */
  20. };
  21. bool
  22. aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  23. int32 exception_id,
  24. bool is_cond_br,
  25. LLVMValueRef cond_br_if,
  26. LLVMBasicBlockRef cond_br_else_block)
  27. {
  28. LLVMBasicBlockRef exce_block;
  29. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  30. LLVMValueRef exce_id = I32_CONST((uint32)exception_id), func_const, func;
  31. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  32. LLVMValueRef param_values[2];
  33. bh_assert(exception_id >= 0 && exception_id < EXCE_NUM);
  34. CHECK_LLVM_CONST(exce_id);
  35. /* Create got_exception block if needed */
  36. if (!func_ctx->got_exception_block) {
  37. if (!(func_ctx->got_exception_block =
  38. LLVMAppendBasicBlockInContext(comp_ctx->context,
  39. func_ctx->func,
  40. "got_exception"))) {
  41. aot_set_last_error("add LLVM basic block failed.");
  42. return false;
  43. }
  44. LLVMPositionBuilderAtEnd(comp_ctx->builder,
  45. func_ctx->got_exception_block);
  46. /* Create exection id phi */
  47. if (!(func_ctx->exception_id_phi =
  48. LLVMBuildPhi(comp_ctx->builder,
  49. comp_ctx->basic_types.int32_type,
  50. "exception_id_phi"))) {
  51. aot_set_last_error("llvm build phi failed.");
  52. return false;
  53. }
  54. /* Call aot_set_exception_with_id() to throw exception */
  55. param_types[0] = INT8_PTR_TYPE;
  56. param_types[1] = I32_TYPE;
  57. ret_type = VOID_TYPE;
  58. /* Create function type */
  59. if (!(func_type = LLVMFunctionType(ret_type, param_types,
  60. 2, false))) {
  61. aot_set_last_error("create LLVM function type failed.");
  62. return false;
  63. }
  64. if (comp_ctx->is_jit_mode) {
  65. /* Create function type */
  66. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  67. aot_set_last_error("create LLVM function type failed.");
  68. return false;
  69. }
  70. /* Create LLVM function with const function pointer */
  71. if (!(func_const =
  72. I64_CONST((uint64)(uintptr_t)aot_set_exception_with_id))
  73. || !(func = LLVMConstIntToPtr(func_const, func_ptr_type))) {
  74. aot_set_last_error("create LLVM value failed.");
  75. return false;
  76. }
  77. }
  78. else {
  79. /* Create LLVM function with external function pointer */
  80. if (!(func = LLVMGetNamedFunction(comp_ctx->module,
  81. "aot_set_exception_with_id"))
  82. && !(func = LLVMAddFunction(comp_ctx->module,
  83. "aot_set_exception_with_id",
  84. func_type))) {
  85. aot_set_last_error("add LLVM function failed.");
  86. return false;
  87. }
  88. }
  89. /* Call the aot_set_exception_with_id() function */
  90. param_values[0] = func_ctx->aot_inst;
  91. param_values[1] = func_ctx->exception_id_phi;
  92. if (!LLVMBuildCall(comp_ctx->builder, func, param_values,
  93. 2, "")) {
  94. aot_set_last_error("llvm build call failed.");
  95. return false;
  96. }
  97. /* Create return IR */
  98. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  99. if (aot_func_type->result_count) {
  100. switch (aot_func_type->types[aot_func_type->param_count]) {
  101. case VALUE_TYPE_I32:
  102. LLVMBuildRet(comp_ctx->builder, I32_ZERO);
  103. break;
  104. case VALUE_TYPE_I64:
  105. LLVMBuildRet(comp_ctx->builder, I64_ZERO);
  106. break;
  107. case VALUE_TYPE_F32:
  108. LLVMBuildRet(comp_ctx->builder, F32_ZERO);
  109. break;
  110. case VALUE_TYPE_F64:
  111. LLVMBuildRet(comp_ctx->builder, F64_ZERO);
  112. break;
  113. }
  114. }
  115. else {
  116. LLVMBuildRetVoid(comp_ctx->builder);
  117. }
  118. /* Resume the builder position */
  119. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  120. }
  121. /* Create exception block if needed */
  122. if (!(exce_block = func_ctx->exception_blocks[exception_id])) {
  123. if (!(func_ctx->exception_blocks[exception_id] = exce_block =
  124. LLVMAppendBasicBlockInContext(comp_ctx->context,
  125. func_ctx->func,
  126. exce_block_names[exception_id]))) {
  127. aot_set_last_error("add LLVM basic block failed.");
  128. return false;
  129. }
  130. /* Move before got_exception block */
  131. LLVMMoveBasicBlockBefore(exce_block, func_ctx->got_exception_block);
  132. /* Add phi incoming value to got_exception block */
  133. LLVMAddIncoming(func_ctx->exception_id_phi, &exce_id, &exce_block, 1);
  134. /* Jump to got exception block */
  135. LLVMPositionBuilderAtEnd(comp_ctx->builder, exce_block);
  136. if (!LLVMBuildBr(comp_ctx->builder, func_ctx->got_exception_block)) {
  137. aot_set_last_error("llvm build br failed.");
  138. return false;
  139. }
  140. }
  141. /* Resume builder position */
  142. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  143. if (!is_cond_br) {
  144. /* not condition br, create br IR */
  145. if (!LLVMBuildBr(comp_ctx->builder, exce_block)) {
  146. aot_set_last_error("llvm build br failed.");
  147. return false;
  148. }
  149. }
  150. else {
  151. /* Create condition br */
  152. if (!LLVMBuildCondBr(comp_ctx->builder, cond_br_if,
  153. exce_block, cond_br_else_block)) {
  154. aot_set_last_error("llvm build cond br failed.");
  155. return false;
  156. }
  157. /* Start to translate the else block */
  158. LLVMPositionBuilderAtEnd(comp_ctx->builder, cond_br_else_block);
  159. }
  160. return true;
  161. fail:
  162. return false;
  163. }