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. 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[6];
  343. LLVMTypeRef ret_ptr_type, elem_ptr_type;
  344. LLVMValueRef func, elem_idx, elem_ptr;
  345. LLVMValueRef func_param_values[6], 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] = I32_TYPE; /* func_type_idx */
  351. func_param_types[2] = I32_TYPE; /* table_elem_idx */
  352. func_param_types[3] = INT32_PTR_TYPE; /* frame_lp */
  353. func_param_types[4] = I32_TYPE; /* argc */
  354. func_param_types[5] = INT32_PTR_TYPE; /* argv_ret */
  355. if (!(func_type = LLVMFunctionType(INT8_TYPE, func_param_types, 6, false))) {
  356. aot_set_last_error("llvm add function type failed.");
  357. return false;
  358. }
  359. /* prepare function pointer */
  360. if (comp_ctx->is_jit_mode) {
  361. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  362. aot_set_last_error("create LLVM function type failed.");
  363. return false;
  364. }
  365. /* JIT mode, call the function directly */
  366. if (!(func = I64_CONST((uint64)(uintptr_t)aot_call_indirect))
  367. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  368. aot_set_last_error("create LLVM value failed.");
  369. return false;
  370. }
  371. }
  372. else {
  373. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  374. && !(func = LLVMAddFunction(comp_ctx->module,
  375. func_name, func_type))) {
  376. aot_set_last_error("add LLVM function failed.");
  377. return false;
  378. }
  379. }
  380. if (param_count > 64) {
  381. aot_set_last_error("prepare native arguments failed: "
  382. "maximum 64 parameter cell number supported.");
  383. return false;
  384. }
  385. /* prepare frame_lp */
  386. for (i = 0; i < param_count; i++) {
  387. if (!(elem_idx = I32_CONST(cell_num))
  388. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  389. aot_set_last_error("llvm add const or pointer type failed.");
  390. return false;
  391. }
  392. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  393. if (!(elem_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  394. func_ctx->argv_buf, &elem_idx, 1, buf))
  395. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  396. elem_ptr_type, buf))) {
  397. aot_set_last_error("llvm build bit cast failed.");
  398. return false;
  399. }
  400. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i], elem_ptr))) {
  401. aot_set_last_error("llvm build store failed.");
  402. return false;
  403. }
  404. LLVMSetAlignment(res, 1);
  405. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  406. }
  407. if (wasm_ret_type != VALUE_TYPE_VOID) {
  408. if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  409. aot_set_last_error("llvm add pointer type failed.");
  410. return false;
  411. }
  412. if (!(value_ret = LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
  413. ret_ptr_type, "argv_ret"))) {
  414. aot_set_last_error("llvm build bit cast failed.");
  415. return false;
  416. }
  417. /* convert to int32 pointer */
  418. if (!(value_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, value_ret,
  419. INT32_PTR_TYPE, "argv_ret_ptr"))) {
  420. aot_set_last_error("llvm build store failed.");
  421. return false;
  422. }
  423. }
  424. else {
  425. value_ret_ptr = LLVMConstNull(INT32_PTR_TYPE);
  426. }
  427. func_param_values[0] = func_ctx->exec_env;
  428. func_param_values[1] = func_type_idx;
  429. func_param_values[2] = table_elem_idx;
  430. func_param_values[3] = func_ctx->argv_buf;
  431. func_param_values[4] = I32_CONST(param_cell_num);
  432. func_param_values[5] = value_ret_ptr;
  433. if (!func_param_values[4]) {
  434. aot_set_last_error("llvm create const failed.");
  435. return false;
  436. }
  437. /* call aot_call_indirect() function */
  438. if (!(res = LLVMBuildCall(comp_ctx->builder, func,
  439. func_param_values, 6, "res"))) {
  440. aot_set_last_error("llvm build call failed.");
  441. return false;
  442. }
  443. if (wasm_ret_type != VALUE_TYPE_VOID)
  444. /* get function return value */
  445. *p_value_ret = LLVMBuildLoad(comp_ctx->builder, value_ret, "value_ret");
  446. *p_res = res;
  447. return true;
  448. }
  449. bool
  450. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  451. uint32 type_idx)
  452. {
  453. AOTFuncType *func_type;
  454. LLVMValueRef elem_idx, ftype_idx;
  455. LLVMValueRef *param_values = NULL, value_ret = NULL, res = NULL;
  456. LLVMTypeRef *param_types = NULL, ret_type;
  457. int32 i, param_count;
  458. uint32 param_cell_num;
  459. uint64 total_size;
  460. uint8 wasm_ret_type;
  461. bool ret;
  462. /* Check function type index */
  463. if (type_idx >= comp_ctx->comp_data->func_type_count) {
  464. aot_set_last_error("type index is overflow");
  465. return false;
  466. }
  467. ftype_idx = I32_CONST(type_idx);
  468. CHECK_LLVM_CONST(ftype_idx);
  469. func_type = comp_ctx->comp_data->func_types[type_idx];
  470. param_cell_num = wasm_type_param_cell_num(func_type);
  471. POP_I32(elem_idx);
  472. /* Initialize parameter types of the LLVM function */
  473. param_count = (int32)func_type->param_count;
  474. total_size = sizeof(LLVMTypeRef) * (uint64)param_count;
  475. if (total_size >= UINT32_MAX
  476. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  477. aot_set_last_error("Allocate memory failed.");
  478. goto fail;
  479. }
  480. for (i = 0; i < param_count; i++)
  481. param_types[i] = TO_LLVM_TYPE(func_type->types[i]);
  482. /* Resolve return type of the LLVM function */
  483. if (func_type->result_count) {
  484. wasm_ret_type = func_type->types[func_type->param_count];
  485. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  486. }
  487. else {
  488. wasm_ret_type = VALUE_TYPE_VOID;
  489. ret_type = VOID_TYPE;
  490. }
  491. /* Allocate memory for parameters */
  492. total_size = sizeof(LLVMValueRef) * (uint64)param_count;
  493. if (total_size >= UINT32_MAX
  494. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  495. aot_set_last_error("Allocate memory failed.");
  496. goto fail;
  497. }
  498. /* Pop parameters from stack */
  499. for (i = param_count - 1; i >= 0; i--)
  500. POP(param_values[i], func_type->types[i]);
  501. if (!call_aot_call_indirect_func(comp_ctx, func_ctx,
  502. func_type, ftype_idx, elem_idx,
  503. param_types, param_values,
  504. param_count, param_cell_num,
  505. ret_type, wasm_ret_type,
  506. &value_ret, &res))
  507. goto fail;
  508. if (func_type->result_count > 0)
  509. PUSH(value_ret, func_type->types[func_type->param_count]);
  510. /* Check whether there was exception thrown when executing the function */
  511. if (!check_call_return(comp_ctx, func_ctx, res))
  512. goto fail;
  513. ret = true;
  514. fail:
  515. if (param_values)
  516. wasm_runtime_free(param_values);
  517. if (param_types)
  518. wasm_runtime_free(param_types);
  519. return ret;
  520. }