aot_emit_function.c 22 KB

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