aot_emit_exception.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. bool
  9. aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  10. int32 exception_id, bool is_cond_br, LLVMValueRef cond_br_if,
  11. LLVMBasicBlockRef cond_br_else_block)
  12. {
  13. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  14. LLVMValueRef exce_id = I32_CONST((uint32)exception_id), func_const, func;
  15. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  16. LLVMValueRef param_values[2];
  17. bh_assert(exception_id >= 0 && exception_id < EXCE_NUM);
  18. CHECK_LLVM_CONST(exce_id);
  19. /* Create got_exception block if needed */
  20. if (!func_ctx->got_exception_block) {
  21. if (!(func_ctx->got_exception_block = LLVMAppendBasicBlockInContext(
  22. comp_ctx->context, func_ctx->func, "got_exception"))) {
  23. aot_set_last_error("add LLVM basic block failed.");
  24. return false;
  25. }
  26. LLVMPositionBuilderAtEnd(comp_ctx->builder,
  27. func_ctx->got_exception_block);
  28. /* Create exection id phi */
  29. if (!(func_ctx->exception_id_phi = LLVMBuildPhi(
  30. comp_ctx->builder, I32_TYPE, "exception_id_phi"))) {
  31. aot_set_last_error("llvm build phi failed.");
  32. return false;
  33. }
  34. /* Call aot_set_exception_with_id() to throw exception */
  35. param_types[0] = INT8_PTR_TYPE;
  36. param_types[1] = I32_TYPE;
  37. ret_type = VOID_TYPE;
  38. /* Create function type */
  39. if (!(func_type = LLVMFunctionType(ret_type, param_types, 2, false))) {
  40. aot_set_last_error("create LLVM function type failed.");
  41. return false;
  42. }
  43. if (comp_ctx->is_jit_mode) {
  44. /* Create function type */
  45. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  46. aot_set_last_error("create LLVM function type failed.");
  47. return false;
  48. }
  49. /* Create LLVM function with const function pointer */
  50. if (!(func_const =
  51. I64_CONST((uint64)(uintptr_t)jit_set_exception_with_id))
  52. || !(func = LLVMConstIntToPtr(func_const, func_ptr_type))) {
  53. aot_set_last_error("create LLVM value failed.");
  54. return false;
  55. }
  56. }
  57. else if (comp_ctx->is_indirect_mode) {
  58. int32 func_index;
  59. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  60. aot_set_last_error("create LLVM function type failed.");
  61. return false;
  62. }
  63. func_index = aot_get_native_symbol_index(
  64. comp_ctx, "aot_set_exception_with_id");
  65. if (func_index < 0) {
  66. return false;
  67. }
  68. if (!(func =
  69. aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  70. func_ptr_type, func_index))) {
  71. return false;
  72. }
  73. }
  74. else {
  75. /* Create LLVM function with external function pointer */
  76. if (!(func = LLVMGetNamedFunction(func_ctx->module,
  77. "aot_set_exception_with_id"))
  78. && !(func = LLVMAddFunction(func_ctx->module,
  79. "aot_set_exception_with_id",
  80. func_type))) {
  81. aot_set_last_error("add LLVM function failed.");
  82. return false;
  83. }
  84. }
  85. /* Call the aot_set_exception_with_id() function */
  86. param_values[0] = func_ctx->aot_inst;
  87. param_values[1] = func_ctx->exception_id_phi;
  88. if (!LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values, 2,
  89. "")) {
  90. aot_set_last_error("llvm build call failed.");
  91. return false;
  92. }
  93. /* Create return IR */
  94. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  95. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  96. return false;
  97. }
  98. /* Resume the builder position */
  99. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  100. }
  101. /* Add phi incoming value to got_exception block */
  102. LLVMAddIncoming(func_ctx->exception_id_phi, &exce_id, &block_curr, 1);
  103. if (!is_cond_br) {
  104. /* not condition br, create br IR */
  105. if (!LLVMBuildBr(comp_ctx->builder, func_ctx->got_exception_block)) {
  106. aot_set_last_error("llvm build br failed.");
  107. return false;
  108. }
  109. }
  110. else {
  111. /* Create condition br */
  112. if (!LLVMBuildCondBr(comp_ctx->builder, cond_br_if,
  113. func_ctx->got_exception_block,
  114. cond_br_else_block)) {
  115. aot_set_last_error("llvm build cond br failed.");
  116. return false;
  117. }
  118. /* Start to translate the else block */
  119. LLVMPositionBuilderAtEnd(comp_ctx->builder, cond_br_else_block);
  120. }
  121. return true;
  122. fail:
  123. return false;
  124. }