aot_emit_function.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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_function.h"
  6. #include "aot_emit_exception.h"
  7. #include "aot_emit_control.h"
  8. #include "../aot/aot_runtime.h"
  9. /* Check whether there was exception thrown, if yes, return directly */
  10. static bool
  11. check_exception_thrown(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  12. {
  13. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  14. LLVMBasicBlockRef block_curr, check_exce_succ;
  15. LLVMValueRef value, cmp;
  16. /* Load the first byte of aot_module_inst->cur_exception, and check
  17. whether it is '\0'. If yes, no exception was thrown. */
  18. if (!(value = LLVMBuildLoad(comp_ctx->builder, func_ctx->cur_exception,
  19. "exce_value"))
  20. || !(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  21. value, I8_ZERO, "cmp"))) {
  22. aot_set_last_error("llvm build icmp failed.");
  23. return false;
  24. }
  25. /* Add check exection success block */
  26. if (!(check_exce_succ = LLVMAppendBasicBlockInContext(comp_ctx->context,
  27. func_ctx->func,
  28. "check_exce_succ"))) {
  29. aot_set_last_error("llvm add basic block failed.");
  30. return false;
  31. }
  32. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  33. LLVMMoveBasicBlockAfter(check_exce_succ, block_curr);
  34. /* Create function return block if it isn't created */
  35. if (!func_ctx->func_return_block) {
  36. if (!(func_ctx->func_return_block =
  37. LLVMAppendBasicBlockInContext(comp_ctx->context,
  38. func_ctx->func,
  39. "func_ret"))) {
  40. aot_set_last_error("llvm add basic block failed.");
  41. return false;
  42. }
  43. /* Create return IR */
  44. LLVMPositionBuilderAtEnd(comp_ctx->builder, func_ctx->func_return_block);
  45. if (aot_func_type->result_count) {
  46. switch (aot_func_type->types[aot_func_type->param_count]) {
  47. case VALUE_TYPE_I32:
  48. LLVMBuildRet(comp_ctx->builder, I32_ZERO);
  49. break;
  50. case VALUE_TYPE_I64:
  51. LLVMBuildRet(comp_ctx->builder, I64_ZERO);
  52. break;
  53. case VALUE_TYPE_F32:
  54. LLVMBuildRet(comp_ctx->builder, F32_ZERO);
  55. break;
  56. case VALUE_TYPE_F64:
  57. LLVMBuildRet(comp_ctx->builder, F64_ZERO);
  58. break;
  59. }
  60. }
  61. else {
  62. LLVMBuildRetVoid(comp_ctx->builder);
  63. }
  64. }
  65. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  66. /* Create condition br */
  67. if (!LLVMBuildCondBr(comp_ctx->builder, cmp,
  68. check_exce_succ, func_ctx->func_return_block)) {
  69. aot_set_last_error("llvm build cond br failed.");
  70. return false;
  71. }
  72. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_exce_succ);
  73. return true;
  74. }
  75. static bool
  76. call_aot_invoke_native_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  77. LLVMValueRef func_idx, AOTFuncType *aot_func_type,
  78. LLVMTypeRef *param_types, LLVMValueRef *param_values,
  79. uint32 param_count, uint32 param_cell_num,
  80. LLVMTypeRef ret_type, uint8 wasm_ret_type,
  81. LLVMValueRef *p_value_ret)
  82. {
  83. LLVMTypeRef func_type, func_ptr_type, func_param_types[5];
  84. LLVMTypeRef ret_ptr_type, elem_ptr_type;
  85. LLVMValueRef func, elem_idx, elem_ptr;
  86. LLVMValueRef func_param_values[5], value_ret, value_ret_ptr, res;
  87. char buf[32], *func_name = "aot_invoke_native";
  88. uint32 i, cell_num = 0;
  89. /* prepare function type of aot_invoke_native */
  90. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  91. func_param_types[1] = I32_TYPE; /* func_idx */
  92. func_param_types[2] = INT32_PTR_TYPE; /* frame_lp */
  93. func_param_types[3] = I32_TYPE; /* argc */
  94. func_param_types[4] = INT32_PTR_TYPE; /* argv_ret */
  95. if (!(func_type = LLVMFunctionType(VOID_TYPE, func_param_types, 5, false))) {
  96. aot_set_last_error("llvm add function type failed.");
  97. return false;
  98. }
  99. /* prepare function pointer */
  100. if (comp_ctx->is_jit_mode) {
  101. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  102. aot_set_last_error("create LLVM function type failed.");
  103. return false;
  104. }
  105. /* JIT mode, call the function directly */
  106. if (!(func = I64_CONST((uint64)(uintptr_t)aot_invoke_native))
  107. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  108. aot_set_last_error("create LLVM value failed.");
  109. return false;
  110. }
  111. }
  112. else {
  113. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  114. && !(func = LLVMAddFunction(comp_ctx->module,
  115. func_name, func_type))) {
  116. aot_set_last_error("add LLVM function failed.");
  117. return false;
  118. }
  119. }
  120. if (param_count > 64) {
  121. aot_set_last_error("prepare native arguments failed: "
  122. "maximum 64 parameter cell number supported.");
  123. return false;
  124. }
  125. /* prepare frame_lp */
  126. for (i = 0; i < param_count; i++) {
  127. if (!(elem_idx = I32_CONST(cell_num))
  128. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  129. aot_set_last_error("llvm add const or pointer type failed.");
  130. return false;
  131. }
  132. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  133. if (!(elem_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  134. func_ctx->argv_buf, &elem_idx, 1, buf))
  135. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  136. elem_ptr_type, buf))) {
  137. aot_set_last_error("llvm build bit cast failed.");
  138. return false;
  139. }
  140. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i], elem_ptr))) {
  141. aot_set_last_error("llvm build store failed.");
  142. return false;
  143. }
  144. LLVMSetAlignment(res, 1);
  145. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  146. }
  147. if (wasm_ret_type != VALUE_TYPE_VOID) {
  148. if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  149. aot_set_last_error("llvm add pointer type failed.");
  150. return false;
  151. }
  152. if (!(value_ret = LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
  153. ret_ptr_type, "argv_ret"))) {
  154. aot_set_last_error("llvm build bit cast failed.");
  155. return false;
  156. }
  157. /* convert to int32 pointer */
  158. if (!(value_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, value_ret,
  159. INT32_PTR_TYPE, "argv_ret_ptr"))) {
  160. aot_set_last_error("llvm build store failed.");
  161. return false;
  162. }
  163. }
  164. else {
  165. value_ret_ptr = LLVMConstNull(INT32_PTR_TYPE);
  166. }
  167. func_param_values[0] = func_ctx->exec_env;
  168. func_param_values[1] = func_idx;
  169. func_param_values[2] = func_ctx->argv_buf;
  170. func_param_values[3] = I32_CONST(param_cell_num);
  171. func_param_values[4] = value_ret_ptr;
  172. if (!func_param_values[3]) {
  173. aot_set_last_error("llvm create const failed.");
  174. return false;
  175. }
  176. /* call aot_invoke_native() function */
  177. if (!(LLVMBuildCall(comp_ctx->builder, func, func_param_values, 5, ""))) {
  178. aot_set_last_error("llvm build call failed.");
  179. return false;
  180. }
  181. if (wasm_ret_type != VALUE_TYPE_VOID)
  182. /* get function return value */
  183. *p_value_ret = LLVMBuildLoad(comp_ctx->builder, value_ret, "value_ret");
  184. return true;
  185. }
  186. bool
  187. aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  188. uint32 func_idx, uint8 **p_frame_ip)
  189. {
  190. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  191. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  192. uint32 func_count = comp_ctx->func_ctx_count, param_cell_num = 0;
  193. AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
  194. AOTFuncType *func_type;
  195. LLVMTypeRef *param_types = NULL, ret_type;
  196. LLVMValueRef *param_values = NULL, value_ret = NULL, func;
  197. LLVMValueRef import_func_idx;
  198. int32 i, j = 0, param_count;
  199. uint64 total_size;
  200. uint8 wasm_ret_type;
  201. bool ret = false;
  202. /* Check function index */
  203. if (func_idx >= import_func_count + func_count) {
  204. aot_set_last_error("Function index out of range.");
  205. return false;
  206. }
  207. /* Get function type */
  208. if (func_idx < import_func_count)
  209. func_type = import_funcs[func_idx].func_type;
  210. else
  211. func_type = func_ctxes[func_idx - import_func_count]->
  212. aot_func->func_type;
  213. /* Get param cell number */
  214. param_cell_num = wasm_type_param_cell_num(func_type);
  215. /* Allocate memory for parameters */
  216. param_count = (int32)func_type->param_count;
  217. total_size = sizeof(LLVMValueRef) * (uint64)(param_count + 1);
  218. if (total_size >= UINT32_MAX
  219. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  220. aot_set_last_error("Allocate memory failed.");
  221. return false;
  222. }
  223. /* First parameter is exec env */
  224. param_values[j++] = func_ctx->exec_env;
  225. /* Pop parameters from stack */
  226. for (i = param_count - 1; i >= 0; i--)
  227. POP(param_values[i + j], func_type->types[i]);
  228. if (func_idx < import_func_count) {
  229. if (!(import_func_idx = I32_CONST(func_idx))) {
  230. aot_set_last_error("llvm build inbounds gep failed.");
  231. goto fail;
  232. }
  233. /* Initialize parameter types of the LLVM function */
  234. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  235. if (total_size >= UINT32_MAX
  236. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  237. aot_set_last_error("Allocate memory failed.");
  238. goto fail;
  239. }
  240. j = 0;
  241. param_types[j++] = comp_ctx->exec_env_type;
  242. for (i = 0; i < param_count; i++)
  243. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  244. if (func_type->result_count) {
  245. wasm_ret_type = func_type->types[func_type->param_count];
  246. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  247. }
  248. else {
  249. wasm_ret_type = VALUE_TYPE_VOID;
  250. ret_type = VOID_TYPE;
  251. }
  252. /* call aot_invoke_native() */
  253. if (!call_aot_invoke_native_func(comp_ctx, func_ctx, import_func_idx, func_type,
  254. param_types + 1, param_values + 1,
  255. param_count, param_cell_num,
  256. ret_type, wasm_ret_type, &value_ret))
  257. goto fail;
  258. }
  259. else {
  260. func = func_ctxes[func_idx - import_func_count]->func;
  261. /* Call the function */
  262. if (!(value_ret = LLVMBuildCall(comp_ctx->builder, func,
  263. param_values, (uint32)param_count + 1,
  264. (func_type->result_count > 0
  265. ? "call" : "")))) {
  266. aot_set_last_error("LLVM build call failed.");
  267. goto fail;
  268. }
  269. /* Set calling convention for the call with the func's calling convention */
  270. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  271. }
  272. if (func_type->result_count > 0)
  273. PUSH(value_ret, func_type->types[func_type->param_count]);
  274. /* Check whether there was exception thrown when executing the function */
  275. if (!check_exception_thrown(comp_ctx, func_ctx))
  276. goto fail;
  277. ret = true;
  278. fail:
  279. if (param_types)
  280. wasm_runtime_free(param_types);
  281. if (param_values)
  282. wasm_runtime_free(param_values);
  283. return ret;
  284. }
  285. bool
  286. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  287. uint32 type_idx)
  288. {
  289. AOTFuncType *func_type;
  290. LLVMValueRef elem_idx, table_elem, func_idx, ftype_idx_ptr, ftype_idx;
  291. LLVMValueRef cmp_elem_idx, cmp_func_idx, is_ftype_match, is_ftype_mismatch;
  292. LLVMValueRef func, func_ptr, func_const, table_size_const, cmp_func_ptr;
  293. LLVMValueRef *param_values = NULL, param_values_tmp[3], value_ret;
  294. LLVMTypeRef *param_types = NULL, param_types_tmp[3], ret_type,
  295. f_type, f_ptr_type;
  296. LLVMBasicBlockRef check_elem_idx_succ, check_ftype_idx_succ;
  297. LLVMBasicBlockRef check_func_idx_succ, check_func_ptr_succ;
  298. char *func_name = "aot_is_wasm_type_equal";
  299. int32 i, j = 0, param_count;
  300. uint32 param_cell_num;
  301. uint64 total_size;
  302. uint8 wasm_ret_type;
  303. bool ret;
  304. /* Check function type index */
  305. if (type_idx >= comp_ctx->comp_data->func_type_count) {
  306. aot_set_last_error("type index is overflow");
  307. return false;
  308. }
  309. func_type = comp_ctx->comp_data->func_types[type_idx];
  310. param_cell_num = wasm_type_param_cell_num(func_type);
  311. POP_I32(elem_idx);
  312. table_size_const = I32_CONST(comp_ctx->comp_data->table_size);
  313. CHECK_LLVM_CONST(table_size_const);
  314. /* Check if (uint32)elem index >= table size */
  315. if (!(cmp_elem_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGE,
  316. elem_idx, table_size_const,
  317. "cmp_elem_idx"))) {
  318. aot_set_last_error("llvm build icmp failed.");
  319. goto fail;
  320. }
  321. /* Throw exception if elem index >= table size */
  322. if (!(check_elem_idx_succ =
  323. LLVMAppendBasicBlockInContext(comp_ctx->context,
  324. func_ctx->func,
  325. "check_elem_idx_succ"))) {
  326. aot_set_last_error("llvm add basic block failed.");
  327. goto fail;
  328. }
  329. LLVMMoveBasicBlockAfter(check_elem_idx_succ,
  330. LLVMGetInsertBlock(comp_ctx->builder));
  331. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNDEFINED_ELEMENT,
  332. true, cmp_elem_idx, check_elem_idx_succ)))
  333. goto fail;
  334. /* Load function index */
  335. if (!(table_elem = LLVMBuildInBoundsGEP(comp_ctx->builder,
  336. func_ctx->table_base,
  337. &elem_idx, 1, "table_elem"))) {
  338. aot_set_last_error("llvm build add failed.");
  339. goto fail;
  340. }
  341. if (!(func_idx = LLVMBuildLoad(comp_ctx->builder, table_elem, "func_idx"))) {
  342. aot_set_last_error("llvm build load failed.");
  343. goto fail;
  344. }
  345. /* Check if func_idx == -1 */
  346. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  347. func_idx, I32_NEG_ONE,
  348. "cmp_func_idx"))) {
  349. aot_set_last_error("llvm build icmp failed.");
  350. goto fail;
  351. }
  352. /* Throw exception if func_idx == -1 */
  353. if (!(check_func_idx_succ =
  354. LLVMAppendBasicBlockInContext(comp_ctx->context,
  355. func_ctx->func,
  356. "check_func_idx_succ"))) {
  357. aot_set_last_error("llvm add basic block failed.");
  358. goto fail;
  359. }
  360. LLVMMoveBasicBlockAfter(check_func_idx_succ,
  361. LLVMGetInsertBlock(comp_ctx->builder));
  362. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNINITIALIZED_ELEMENT,
  363. true, cmp_func_idx, check_func_idx_succ)))
  364. goto fail;
  365. /* Load function type index */
  366. if (!(ftype_idx_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  367. func_ctx->func_type_indexes,
  368. &func_idx, 1,
  369. "ftype_idx_ptr"))) {
  370. aot_set_last_error("llvm build inbounds gep failed.");
  371. goto fail;
  372. }
  373. if (!(ftype_idx = LLVMBuildLoad(comp_ctx->builder, ftype_idx_ptr,
  374. "ftype_idx"))) {
  375. aot_set_last_error("llvm build load failed.");
  376. goto fail;
  377. }
  378. /* Call aot_is_type_equal() to check whether function type match */
  379. param_types_tmp[0] = INT8_PTR_TYPE;
  380. param_types_tmp[1] = I32_TYPE;
  381. param_types_tmp[2] = I32_TYPE;
  382. ret_type = INT8_TYPE;
  383. /* Create function type */
  384. if (!(f_type = LLVMFunctionType(ret_type, param_types_tmp,
  385. 3, false))) {
  386. aot_set_last_error("create LLVM function type failed.");
  387. goto fail;
  388. }
  389. if (comp_ctx->is_jit_mode) {
  390. /* Create function type */
  391. if (!(f_ptr_type = LLVMPointerType(f_type, 0))) {
  392. aot_set_last_error("create LLVM function type failed.");
  393. goto fail;
  394. }
  395. /* Create LLVM function with const function pointer */
  396. if (!(func_const = I64_CONST((uint64)(uintptr_t)aot_is_wasm_type_equal))
  397. || !(func = LLVMConstIntToPtr(func_const, f_ptr_type))) {
  398. aot_set_last_error("create LLVM value failed.");
  399. goto fail;
  400. }
  401. }
  402. else {
  403. /* Create LLVM function with external function pointer */
  404. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  405. && !(func = LLVMAddFunction(comp_ctx->module, func_name, f_type))) {
  406. aot_set_last_error("add LLVM function failed.");
  407. goto fail;
  408. }
  409. }
  410. /* Call the aot_is_type_equal() function */
  411. param_values_tmp[0] = func_ctx->aot_inst;
  412. param_values_tmp[1] = I32_CONST(type_idx);
  413. param_values_tmp[2] = ftype_idx;
  414. CHECK_LLVM_CONST(param_values_tmp[1]);
  415. if (!(is_ftype_match = LLVMBuildCall(comp_ctx->builder, func,
  416. param_values_tmp, 3,
  417. "is_ftype_match"))) {
  418. aot_set_last_error("llvm build icmp failed.");
  419. goto fail;
  420. }
  421. if (!(is_ftype_mismatch = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  422. is_ftype_match, I8_ZERO,
  423. "is_ftype_mismatch"))) {
  424. aot_set_last_error("llvm build icmp failed.");
  425. goto fail;
  426. }
  427. if (!(check_ftype_idx_succ =
  428. LLVMAppendBasicBlockInContext(comp_ctx->context,
  429. func_ctx->func,
  430. "check_ftype_idx_success"))) {
  431. aot_set_last_error("llvm add basic block failed.");
  432. goto fail;
  433. }
  434. LLVMMoveBasicBlockAfter(check_ftype_idx_succ,
  435. LLVMGetInsertBlock(comp_ctx->builder));
  436. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INVALID_FUNCTION_TYPE_INDEX,
  437. true, is_ftype_mismatch, check_ftype_idx_succ)))
  438. goto fail;
  439. /* Load function pointer */
  440. if (!(func_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->func_ptrs,
  441. &func_idx, 1, "func_ptr"))) {
  442. aot_set_last_error("llvm build inbounds gep failed.");
  443. goto fail;
  444. }
  445. if (!(func = LLVMBuildLoad(comp_ctx->builder, func_ptr, "func_tmp"))) {
  446. aot_set_last_error("llvm build load failed.");
  447. goto fail;
  448. }
  449. /* Check whether import function is NULL */
  450. if (!(cmp_func_ptr = LLVMBuildIsNull(comp_ctx->builder, func, "is_func_null"))) {
  451. aot_set_last_error("llvm build is null failed.");
  452. goto fail;
  453. }
  454. /* Throw exception if import function is NULL */
  455. if (!(check_func_ptr_succ =
  456. LLVMAppendBasicBlockInContext(comp_ctx->context,
  457. func_ctx->func,
  458. "check_func_ptr_succ"))) {
  459. aot_set_last_error("llvm add basic block failed.");
  460. goto fail;
  461. }
  462. LLVMMoveBasicBlockAfter(check_func_ptr_succ,
  463. LLVMGetInsertBlock(comp_ctx->builder));
  464. if (!(aot_emit_exception(comp_ctx, func_ctx,
  465. EXCE_CALL_UNLINKED_IMPORT_FUNC,
  466. true, cmp_func_ptr, check_func_ptr_succ)))
  467. goto fail;
  468. /* Initialize parameter types of the LLVM function */
  469. param_count = (int32)func_type->param_count;
  470. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  471. if (total_size >= UINT32_MAX
  472. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  473. aot_set_last_error("Allocate memory failed.");
  474. goto fail;
  475. }
  476. j = 0;
  477. param_types[j++] = comp_ctx->exec_env_type;
  478. for (i = 0; i < param_count; i++)
  479. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  480. /* Resolve return type of the LLVM function */
  481. if (func_type->result_count) {
  482. wasm_ret_type = func_type->types[func_type->param_count];
  483. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  484. }
  485. else {
  486. wasm_ret_type = VALUE_TYPE_VOID;
  487. ret_type = VOID_TYPE;
  488. }
  489. /* Allocate memory for parameters */
  490. total_size = sizeof(LLVMValueRef) * (uint64)(param_count + 1);
  491. if (total_size >= UINT32_MAX
  492. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  493. aot_set_last_error("Allocate memory failed.");
  494. goto fail;
  495. }
  496. /* First parameter is exec env */
  497. j = 0;
  498. param_values[j++] = func_ctx->exec_env;
  499. /* Pop parameters from stack */
  500. for (i = param_count - 1; i >= 0; i--)
  501. POP(param_values[i + j], func_type->types[i]);
  502. if (!call_aot_invoke_native_func(comp_ctx, func_ctx, func_idx, func_type,
  503. param_types + 1, param_values + 1,
  504. param_count, param_cell_num,
  505. ret_type, wasm_ret_type, &value_ret))
  506. goto fail;
  507. if (func_type->result_count > 0)
  508. PUSH(value_ret, func_type->types[func_type->param_count]);
  509. /* Check whether there was exception thrown when executing the function */
  510. if (!check_exception_thrown(comp_ctx, func_ctx))
  511. goto fail;
  512. ret = true;
  513. fail:
  514. if (param_values)
  515. wasm_runtime_free(param_values);
  516. if (param_types)
  517. wasm_runtime_free(param_types);
  518. return ret;
  519. }