aot_emit_exception.c 7.2 KB

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