aot_emit_function.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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[4];
  127. LLVMTypeRef ret_ptr_type, elem_ptr_type;
  128. LLVMValueRef func, elem_idx, elem_ptr;
  129. LLVMValueRef func_param_values[4], value_ret = NULL, 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] = I32_TYPE; /* argc */
  136. func_param_types[3] = INT32_PTR_TYPE; /* argv */
  137. if (!(func_type = LLVMFunctionType(INT8_TYPE, func_param_types, 4, false))) {
  138. aot_set_last_error("llvm add function type failed.");
  139. return false;
  140. }
  141. /* prepare function pointer */
  142. if (comp_ctx->is_jit_mode) {
  143. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  144. aot_set_last_error("create LLVM function type failed.");
  145. return false;
  146. }
  147. /* JIT mode, call the function directly */
  148. if (!(func = I64_CONST((uint64)(uintptr_t)aot_invoke_native))
  149. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  150. aot_set_last_error("create LLVM value failed.");
  151. return false;
  152. }
  153. }
  154. else {
  155. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  156. && !(func = LLVMAddFunction(comp_ctx->module,
  157. func_name, func_type))) {
  158. aot_set_last_error("add LLVM function failed.");
  159. return false;
  160. }
  161. }
  162. if (param_count > 64) {
  163. aot_set_last_error("prepare native arguments failed: "
  164. "maximum 64 parameter cell number supported.");
  165. return false;
  166. }
  167. /* prepare frame_lp */
  168. for (i = 0; i < param_count; i++) {
  169. if (!(elem_idx = I32_CONST(cell_num))
  170. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  171. aot_set_last_error("llvm add const or pointer type failed.");
  172. return false;
  173. }
  174. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  175. if (!(elem_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  176. func_ctx->argv_buf, &elem_idx, 1, buf))
  177. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  178. elem_ptr_type, buf))) {
  179. aot_set_last_error("llvm build bit cast failed.");
  180. return false;
  181. }
  182. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i], elem_ptr))) {
  183. aot_set_last_error("llvm build store failed.");
  184. return false;
  185. }
  186. LLVMSetAlignment(res, 1);
  187. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  188. }
  189. func_param_values[0] = func_ctx->exec_env;
  190. func_param_values[1] = func_idx;
  191. func_param_values[2] = I32_CONST(param_cell_num);
  192. func_param_values[3] = func_ctx->argv_buf;
  193. if (!func_param_values[2]) {
  194. aot_set_last_error("llvm create const failed.");
  195. return false;
  196. }
  197. /* call aot_invoke_native() function */
  198. if (!(res = LLVMBuildCall(comp_ctx->builder, func,
  199. func_param_values, 4, "res"))) {
  200. aot_set_last_error("llvm build call failed.");
  201. return false;
  202. }
  203. /* get function return value */
  204. if (wasm_ret_type != VALUE_TYPE_VOID) {
  205. if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  206. aot_set_last_error("llvm add pointer type failed.");
  207. return false;
  208. }
  209. if (!(value_ret = LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
  210. ret_ptr_type, "argv_ret"))) {
  211. aot_set_last_error("llvm build bit cast failed.");
  212. return false;
  213. }
  214. if (!(*p_value_ret = LLVMBuildLoad(comp_ctx->builder, value_ret,
  215. "value_ret"))) {
  216. aot_set_last_error("llvm build load failed.");
  217. return false;
  218. }
  219. }
  220. *p_res = res;
  221. return true;
  222. }
  223. static bool
  224. check_stack_boundary(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  225. uint32 callee_cell_num)
  226. {
  227. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  228. LLVMBasicBlockRef check_stack;
  229. LLVMValueRef callee_local_size, stack_bound, cmp;
  230. if (!(callee_local_size = I32_CONST(callee_cell_num * 4))) {
  231. aot_set_last_error("llvm build const failed.");
  232. return false;
  233. }
  234. if (!(stack_bound = LLVMBuildInBoundsGEP(comp_ctx->builder,
  235. func_ctx->native_stack_bound,
  236. &callee_local_size, 1,
  237. "stack_bound"))) {
  238. aot_set_last_error("llvm build inbound gep failed.");
  239. return false;
  240. }
  241. if (!(check_stack = LLVMAppendBasicBlockInContext(comp_ctx->context,
  242. func_ctx->func,
  243. "check_stack"))) {
  244. aot_set_last_error("llvm add basic block failed.");
  245. return false;
  246. }
  247. LLVMMoveBasicBlockAfter(check_stack, block_curr);
  248. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT,
  249. func_ctx->last_alloca, stack_bound,
  250. "cmp"))) {
  251. aot_set_last_error("llvm build icmp failed.");
  252. return false;
  253. }
  254. if (!aot_emit_exception(comp_ctx, func_ctx,
  255. EXCE_NATIVE_STACK_OVERFLOW,
  256. true, cmp, check_stack)) {
  257. return false;
  258. }
  259. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_stack);
  260. return true;
  261. }
  262. bool
  263. aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  264. uint32 func_idx, uint8 **p_frame_ip)
  265. {
  266. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  267. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  268. uint32 func_count = comp_ctx->func_ctx_count, param_cell_num = 0;
  269. AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
  270. AOTFuncType *func_type;
  271. AOTFunc *aot_func;
  272. LLVMTypeRef *param_types = NULL, ret_type;
  273. LLVMValueRef *param_values = NULL, value_ret = NULL, func;
  274. LLVMValueRef import_func_idx, res;
  275. int32 i, j = 0, param_count;
  276. uint64 total_size;
  277. uint32 callee_cell_num;
  278. uint8 wasm_ret_type;
  279. bool ret = false;
  280. /* Check function index */
  281. if (func_idx >= import_func_count + func_count) {
  282. aot_set_last_error("Function index out of range.");
  283. return false;
  284. }
  285. /* Get function type */
  286. if (func_idx < import_func_count)
  287. func_type = import_funcs[func_idx].func_type;
  288. else
  289. func_type = func_ctxes[func_idx - import_func_count]->
  290. aot_func->func_type;
  291. /* Get param cell number */
  292. param_cell_num = wasm_type_param_cell_num(func_type);
  293. /* Allocate memory for parameters */
  294. param_count = (int32)func_type->param_count;
  295. total_size = sizeof(LLVMValueRef) * (uint64)(param_count + 1);
  296. if (total_size >= UINT32_MAX
  297. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  298. aot_set_last_error("Allocate memory failed.");
  299. return false;
  300. }
  301. /* First parameter is exec env */
  302. param_values[j++] = func_ctx->exec_env;
  303. /* Pop parameters from stack */
  304. for (i = param_count - 1; i >= 0; i--)
  305. POP(param_values[i + j], func_type->types[i]);
  306. if (func_idx < import_func_count) {
  307. if (!(import_func_idx = I32_CONST(func_idx))) {
  308. aot_set_last_error("llvm build inbounds gep failed.");
  309. goto fail;
  310. }
  311. /* Initialize parameter types of the LLVM function */
  312. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  313. if (total_size >= UINT32_MAX
  314. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  315. aot_set_last_error("Allocate memory failed.");
  316. goto fail;
  317. }
  318. j = 0;
  319. param_types[j++] = comp_ctx->exec_env_type;
  320. for (i = 0; i < param_count; i++)
  321. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  322. if (func_type->result_count) {
  323. wasm_ret_type = func_type->types[func_type->param_count];
  324. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  325. }
  326. else {
  327. wasm_ret_type = VALUE_TYPE_VOID;
  328. ret_type = VOID_TYPE;
  329. }
  330. /* call aot_invoke_native() */
  331. if (!call_aot_invoke_native_func(comp_ctx, func_ctx, import_func_idx, func_type,
  332. param_types + 1, param_values + 1,
  333. param_count, param_cell_num,
  334. ret_type, wasm_ret_type, &value_ret, &res))
  335. goto fail;
  336. /* Check whether there was exception thrown when executing the function */
  337. if (!check_call_return(comp_ctx, func_ctx, res))
  338. goto fail;
  339. }
  340. else {
  341. func = func_ctxes[func_idx - import_func_count]->func;
  342. aot_func = func_ctxes[func_idx - import_func_count]->aot_func;
  343. callee_cell_num = aot_func->param_cell_num + aot_func->local_cell_num + 1;
  344. if (!check_stack_boundary(comp_ctx, func_ctx, callee_cell_num))
  345. goto fail;
  346. /* Call the function */
  347. if (!(value_ret = LLVMBuildCall(comp_ctx->builder, func,
  348. param_values, (uint32)param_count + 1,
  349. (func_type->result_count > 0
  350. ? "call" : "")))) {
  351. aot_set_last_error("LLVM build call failed.");
  352. goto fail;
  353. }
  354. /* Set calling convention for the call with the func's calling convention */
  355. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  356. /* Check whether there was exception thrown when executing the function */
  357. if (!check_exception_thrown(comp_ctx, func_ctx))
  358. goto fail;
  359. }
  360. if (func_type->result_count > 0)
  361. PUSH(value_ret, func_type->types[func_type->param_count]);
  362. ret = true;
  363. fail:
  364. if (param_types)
  365. wasm_runtime_free(param_types);
  366. if (param_values)
  367. wasm_runtime_free(param_values);
  368. return ret;
  369. }
  370. static bool
  371. call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  372. AOTFuncType *aot_func_type,
  373. LLVMValueRef func_type_idx, LLVMValueRef table_elem_idx,
  374. LLVMTypeRef *param_types, LLVMValueRef *param_values,
  375. uint32 param_count, uint32 param_cell_num,
  376. LLVMTypeRef ret_type, uint8 wasm_ret_type,
  377. LLVMValueRef *p_value_ret, LLVMValueRef *p_res)
  378. {
  379. LLVMTypeRef func_type, func_ptr_type, func_param_types[6];
  380. LLVMTypeRef ret_ptr_type, elem_ptr_type;
  381. LLVMValueRef func, elem_idx, elem_ptr;
  382. LLVMValueRef func_param_values[6], value_ret = NULL, res = NULL;
  383. char buf[32], *func_name = "aot_call_indirect";
  384. uint32 i, cell_num = 0;
  385. /* prepare function type of aot_call_indirect */
  386. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  387. func_param_types[1] = INT8_TYPE; /* check_func_type */
  388. func_param_types[2] = I32_TYPE; /* func_type_idx */
  389. func_param_types[3] = I32_TYPE; /* table_elem_idx */
  390. func_param_types[4] = I32_TYPE; /* argc */
  391. func_param_types[5] = INT32_PTR_TYPE; /* argv */
  392. if (!(func_type = LLVMFunctionType(INT8_TYPE, func_param_types, 6, false))) {
  393. aot_set_last_error("llvm add function type failed.");
  394. return false;
  395. }
  396. /* prepare function pointer */
  397. if (comp_ctx->is_jit_mode) {
  398. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  399. aot_set_last_error("create LLVM function type failed.");
  400. return false;
  401. }
  402. /* JIT mode, call the function directly */
  403. if (!(func = I64_CONST((uint64)(uintptr_t)aot_call_indirect))
  404. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  405. aot_set_last_error("create LLVM value failed.");
  406. return false;
  407. }
  408. }
  409. else {
  410. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  411. && !(func = LLVMAddFunction(comp_ctx->module,
  412. func_name, func_type))) {
  413. aot_set_last_error("add LLVM function failed.");
  414. return false;
  415. }
  416. }
  417. if (param_count > 64) {
  418. aot_set_last_error("prepare native arguments failed: "
  419. "maximum 64 parameter cell number supported.");
  420. return false;
  421. }
  422. /* prepare frame_lp */
  423. for (i = 0; i < param_count; i++) {
  424. if (!(elem_idx = I32_CONST(cell_num))
  425. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  426. aot_set_last_error("llvm add const or pointer type failed.");
  427. return false;
  428. }
  429. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  430. if (!(elem_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  431. func_ctx->argv_buf, &elem_idx, 1, buf))
  432. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  433. elem_ptr_type, buf))) {
  434. aot_set_last_error("llvm build bit cast failed.");
  435. return false;
  436. }
  437. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i], elem_ptr))) {
  438. aot_set_last_error("llvm build store failed.");
  439. return false;
  440. }
  441. LLVMSetAlignment(res, 1);
  442. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  443. }
  444. func_param_values[0] = func_ctx->exec_env;
  445. func_param_values[1] = I8_CONST(true);
  446. func_param_values[2] = func_type_idx;
  447. func_param_values[3] = table_elem_idx;
  448. func_param_values[4] = I32_CONST(param_cell_num);
  449. func_param_values[5] = func_ctx->argv_buf;
  450. if (!func_param_values[1] || !func_param_values[4]) {
  451. aot_set_last_error("llvm create const failed.");
  452. return false;
  453. }
  454. /* call aot_call_indirect() function */
  455. if (!(res = LLVMBuildCall(comp_ctx->builder, func,
  456. func_param_values, 6, "res"))) {
  457. aot_set_last_error("llvm build call failed.");
  458. return false;
  459. }
  460. /* get function return value */
  461. if (wasm_ret_type != VALUE_TYPE_VOID) {
  462. if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  463. aot_set_last_error("llvm add pointer type failed.");
  464. return false;
  465. }
  466. if (!(value_ret = LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
  467. ret_ptr_type, "argv_ret"))) {
  468. aot_set_last_error("llvm build bit cast failed.");
  469. return false;
  470. }
  471. if (!(*p_value_ret = LLVMBuildLoad(comp_ctx->builder, value_ret,
  472. "value_ret"))) {
  473. aot_set_last_error("llvm build load failed.");
  474. return false;
  475. }
  476. }
  477. *p_res = res;
  478. return true;
  479. }
  480. bool
  481. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  482. uint32 type_idx)
  483. {
  484. AOTFuncType *func_type;
  485. LLVMValueRef elem_idx, ftype_idx;
  486. LLVMValueRef *param_values = NULL, value_ret = NULL, res = NULL;
  487. LLVMTypeRef *param_types = NULL, ret_type;
  488. int32 i, param_count;
  489. uint32 param_cell_num;
  490. uint64 total_size;
  491. uint8 wasm_ret_type;
  492. bool ret;
  493. /* Check function type index */
  494. if (type_idx >= comp_ctx->comp_data->func_type_count) {
  495. aot_set_last_error("type index is overflow");
  496. return false;
  497. }
  498. ftype_idx = I32_CONST(type_idx);
  499. CHECK_LLVM_CONST(ftype_idx);
  500. func_type = comp_ctx->comp_data->func_types[type_idx];
  501. param_cell_num = wasm_type_param_cell_num(func_type);
  502. POP_I32(elem_idx);
  503. /* Initialize parameter types of the LLVM function */
  504. param_count = (int32)func_type->param_count;
  505. total_size = sizeof(LLVMTypeRef) * (uint64)param_count;
  506. if (total_size >= UINT32_MAX
  507. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  508. aot_set_last_error("Allocate memory failed.");
  509. goto fail;
  510. }
  511. for (i = 0; i < param_count; i++)
  512. param_types[i] = TO_LLVM_TYPE(func_type->types[i]);
  513. /* Resolve return type of the LLVM function */
  514. if (func_type->result_count) {
  515. wasm_ret_type = func_type->types[func_type->param_count];
  516. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  517. }
  518. else {
  519. wasm_ret_type = VALUE_TYPE_VOID;
  520. ret_type = VOID_TYPE;
  521. }
  522. /* Allocate memory for parameters */
  523. total_size = sizeof(LLVMValueRef) * (uint64)param_count;
  524. if (total_size >= UINT32_MAX
  525. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  526. aot_set_last_error("Allocate memory failed.");
  527. goto fail;
  528. }
  529. /* Pop parameters from stack */
  530. for (i = param_count - 1; i >= 0; i--)
  531. POP(param_values[i], func_type->types[i]);
  532. if (!call_aot_call_indirect_func(comp_ctx, func_ctx,
  533. func_type, ftype_idx, elem_idx,
  534. param_types, param_values,
  535. param_count, param_cell_num,
  536. ret_type, wasm_ret_type,
  537. &value_ret, &res))
  538. goto fail;
  539. if (func_type->result_count > 0)
  540. PUSH(value_ret, func_type->types[func_type->param_count]);
  541. /* Check whether there was exception thrown when executing the function */
  542. if (!check_call_return(comp_ctx, func_ctx, res))
  543. goto fail;
  544. ret = true;
  545. fail:
  546. if (param_values)
  547. wasm_runtime_free(param_values);
  548. if (param_types)
  549. wasm_runtime_free(param_types);
  550. return ret;
  551. }