aot_emit_function.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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_cell_num > 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. uint32 ext_ret_cell_num = 0, cell_num = 0;
  270. AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
  271. AOTFuncType *func_type;
  272. AOTFunc *aot_func;
  273. LLVMTypeRef *param_types = NULL, ret_type;
  274. LLVMTypeRef ext_ret_ptr_type;
  275. LLVMValueRef *param_values = NULL, value_ret = NULL, func;
  276. LLVMValueRef import_func_idx, res;
  277. LLVMValueRef ext_ret, ext_ret_ptr, ext_ret_idx;
  278. int32 i, j = 0, param_count, result_count, ext_ret_count;
  279. uint64 total_size;
  280. uint32 callee_cell_num;
  281. uint8 wasm_ret_type;
  282. uint8 *ext_ret_types = NULL;
  283. bool ret = false;
  284. char buf[32];
  285. #if WASM_ENABLE_THREAD_MGR != 0
  286. /* Insert suspend check point */
  287. if (comp_ctx->enable_thread_mgr) {
  288. if (!check_suspend_flags(comp_ctx, func_ctx))
  289. return false;
  290. }
  291. #endif
  292. /* Check function index */
  293. if (func_idx >= import_func_count + func_count) {
  294. aot_set_last_error("Function index out of range.");
  295. return false;
  296. }
  297. /* Get function type */
  298. if (func_idx < import_func_count)
  299. func_type = import_funcs[func_idx].func_type;
  300. else
  301. func_type = func_ctxes[func_idx - import_func_count]->
  302. aot_func->func_type;
  303. /* Get param cell number */
  304. param_cell_num = func_type->param_cell_num;
  305. /* Allocate memory for parameters.
  306. * Parameters layout:
  307. * - exec env
  308. * - wasm function's parameters
  309. * - extra results'(except the first one) addresses
  310. */
  311. param_count = (int32)func_type->param_count;
  312. result_count = (int32)func_type->result_count;
  313. ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  314. total_size = sizeof(LLVMValueRef) * (uint64)(param_count + 1
  315. + ext_ret_count);
  316. if (total_size >= UINT32_MAX
  317. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  318. aot_set_last_error("Allocate memory failed.");
  319. return false;
  320. }
  321. /* First parameter is exec env */
  322. param_values[j++] = func_ctx->exec_env;
  323. /* Pop parameters from stack */
  324. for (i = param_count - 1; i >= 0; i--)
  325. POP(param_values[i + j], func_type->types[i]);
  326. if (func_idx < import_func_count) {
  327. if (!(import_func_idx = I32_CONST(func_idx))) {
  328. aot_set_last_error("llvm build inbounds gep failed.");
  329. goto fail;
  330. }
  331. /* Initialize parameter types of the LLVM function */
  332. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  333. if (total_size >= UINT32_MAX
  334. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  335. aot_set_last_error("Allocate memory failed.");
  336. goto fail;
  337. }
  338. j = 0;
  339. param_types[j++] = comp_ctx->exec_env_type;
  340. for (i = 0; i < param_count; i++)
  341. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  342. if (func_type->result_count) {
  343. wasm_ret_type = func_type->types[func_type->param_count];
  344. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  345. }
  346. else {
  347. wasm_ret_type = VALUE_TYPE_VOID;
  348. ret_type = VOID_TYPE;
  349. }
  350. /* call aot_invoke_native() */
  351. if (!call_aot_invoke_native_func(comp_ctx, func_ctx, import_func_idx, func_type,
  352. param_types + 1, param_values + 1,
  353. param_count, param_cell_num,
  354. ret_type, wasm_ret_type, &value_ret, &res))
  355. goto fail;
  356. /* Check whether there was exception thrown when executing the function */
  357. if (!check_call_return(comp_ctx, func_ctx, res))
  358. goto fail;
  359. }
  360. else {
  361. func = func_ctxes[func_idx - import_func_count]->func;
  362. aot_func = func_ctxes[func_idx - import_func_count]->aot_func;
  363. callee_cell_num = aot_func->param_cell_num + aot_func->local_cell_num + 1;
  364. if (comp_ctx->enable_bound_check
  365. && !check_stack_boundary(comp_ctx, func_ctx, callee_cell_num))
  366. goto fail;
  367. /* Prepare parameters for extra results */
  368. if (ext_ret_count > 0) {
  369. ext_ret_types = func_type->types + param_count + 1;
  370. ext_ret_cell_num =
  371. wasm_get_cell_num(ext_ret_types, ext_ret_count);
  372. if (ext_ret_cell_num > 64) {
  373. aot_set_last_error("prepare extra results's return "
  374. "address arguments failed: "
  375. "maximum 64 parameter cell number supported.");
  376. goto fail;
  377. }
  378. for (i = 0; i < ext_ret_count; i++) {
  379. if (!(ext_ret_idx = I32_CONST(cell_num))
  380. || !(ext_ret_ptr_type =
  381. LLVMPointerType(TO_LLVM_TYPE(ext_ret_types[i]), 0))) {
  382. aot_set_last_error("llvm add const or pointer type failed.");
  383. goto fail;
  384. }
  385. snprintf(buf, sizeof(buf), "func%d_ext_ret%d_ptr", func_idx, i);
  386. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  387. func_ctx->argv_buf,
  388. &ext_ret_idx, 1, buf))) {
  389. aot_set_last_error("llvm build GEP failed.");
  390. goto fail;
  391. }
  392. snprintf(buf, sizeof(buf), "func%d_ext_ret%d_ptr_cast", func_idx, i);
  393. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder,
  394. ext_ret_ptr,
  395. ext_ret_ptr_type,
  396. buf))) {
  397. aot_set_last_error("llvm build bit cast failed.");
  398. goto fail;
  399. }
  400. param_values[1 + param_count + i] = ext_ret_ptr;
  401. cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
  402. }
  403. }
  404. /* Call the function */
  405. if (!(value_ret = LLVMBuildCall(comp_ctx->builder, func,
  406. param_values,
  407. (uint32)param_count + 1 + ext_ret_count,
  408. (func_type->result_count > 0
  409. ? "call" : "")))) {
  410. aot_set_last_error("LLVM build call failed.");
  411. goto fail;
  412. }
  413. /* Set calling convention for the call with the func's calling convention */
  414. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  415. /* Check whether there was exception thrown when executing the function */
  416. if (!check_exception_thrown(comp_ctx, func_ctx))
  417. goto fail;
  418. }
  419. if (func_type->result_count > 0) {
  420. /* Push the first result to stack */
  421. PUSH(value_ret, func_type->types[func_type->param_count]);
  422. /* Load extra result from its address and push to stack */
  423. for (i = 0; i < ext_ret_count; i++) {
  424. snprintf(buf, sizeof(buf), "func%d_ext_ret%d", func_idx, i);
  425. if (!(ext_ret = LLVMBuildLoad(comp_ctx->builder,
  426. param_values[1 + param_count + i],
  427. buf))) {
  428. aot_set_last_error("llvm build load failed.");
  429. goto fail;
  430. }
  431. PUSH(ext_ret, ext_ret_types[i]);
  432. }
  433. }
  434. ret = true;
  435. fail:
  436. if (param_types)
  437. wasm_runtime_free(param_types);
  438. if (param_values)
  439. wasm_runtime_free(param_values);
  440. return ret;
  441. }
  442. static bool
  443. call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  444. AOTFuncType *aot_func_type,
  445. LLVMValueRef func_type_idx, LLVMValueRef table_elem_idx,
  446. LLVMTypeRef *param_types, LLVMValueRef *param_values,
  447. uint32 param_count, uint32 param_cell_num,
  448. uint32 result_count, uint8 *wasm_ret_types,
  449. LLVMValueRef *value_rets, LLVMValueRef *p_res)
  450. {
  451. LLVMTypeRef func_type, func_ptr_type, func_param_types[6];
  452. LLVMTypeRef ret_type, ret_ptr_type, elem_ptr_type;
  453. LLVMValueRef func, ret_idx, ret_ptr, elem_idx, elem_ptr;
  454. LLVMValueRef func_param_values[6], res = NULL;
  455. char buf[32], *func_name = "aot_call_indirect";
  456. uint32 i, cell_num = 0, ret_cell_num, argv_cell_num;
  457. #if WASM_ENABLE_THREAD_MGR != 0
  458. /* Insert suspend check point */
  459. if (comp_ctx->enable_thread_mgr) {
  460. if (!check_suspend_flags(comp_ctx, func_ctx))
  461. return false;
  462. }
  463. #endif
  464. /* prepare function type of aot_call_indirect */
  465. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  466. func_param_types[1] = INT8_TYPE; /* check_func_type */
  467. func_param_types[2] = I32_TYPE; /* func_type_idx */
  468. func_param_types[3] = I32_TYPE; /* table_elem_idx */
  469. func_param_types[4] = I32_TYPE; /* argc */
  470. func_param_types[5] = INT32_PTR_TYPE; /* argv */
  471. if (!(func_type = LLVMFunctionType(INT8_TYPE, func_param_types, 6, false))) {
  472. aot_set_last_error("llvm add function type failed.");
  473. return false;
  474. }
  475. /* prepare function pointer */
  476. if (comp_ctx->is_jit_mode) {
  477. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  478. aot_set_last_error("create LLVM function type failed.");
  479. return false;
  480. }
  481. /* JIT mode, call the function directly */
  482. if (!(func = I64_CONST((uint64)(uintptr_t)aot_call_indirect))
  483. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  484. aot_set_last_error("create LLVM value failed.");
  485. return false;
  486. }
  487. }
  488. else {
  489. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  490. && !(func = LLVMAddFunction(comp_ctx->module,
  491. func_name, func_type))) {
  492. aot_set_last_error("add LLVM function failed.");
  493. return false;
  494. }
  495. }
  496. ret_cell_num = wasm_get_cell_num(wasm_ret_types, result_count);
  497. argv_cell_num = param_cell_num > ret_cell_num ? param_cell_num : ret_cell_num;
  498. if (argv_cell_num > 64) {
  499. aot_set_last_error("prepare native arguments failed: "
  500. "maximum 64 parameter cell number supported.");
  501. return false;
  502. }
  503. /* prepare frame_lp */
  504. for (i = 0; i < param_count; i++) {
  505. if (!(elem_idx = I32_CONST(cell_num))
  506. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  507. aot_set_last_error("llvm add const or pointer type failed.");
  508. return false;
  509. }
  510. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  511. if (!(elem_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  512. func_ctx->argv_buf, &elem_idx, 1, buf))
  513. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  514. elem_ptr_type, buf))) {
  515. aot_set_last_error("llvm build bit cast failed.");
  516. return false;
  517. }
  518. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i], elem_ptr))) {
  519. aot_set_last_error("llvm build store failed.");
  520. return false;
  521. }
  522. LLVMSetAlignment(res, 1);
  523. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  524. }
  525. func_param_values[0] = func_ctx->exec_env;
  526. func_param_values[1] = I8_CONST(true);
  527. func_param_values[2] = func_type_idx;
  528. func_param_values[3] = table_elem_idx;
  529. func_param_values[4] = I32_CONST(param_cell_num);
  530. func_param_values[5] = func_ctx->argv_buf;
  531. if (!func_param_values[1] || !func_param_values[4]) {
  532. aot_set_last_error("llvm create const failed.");
  533. return false;
  534. }
  535. /* call aot_call_indirect() function */
  536. if (!(res = LLVMBuildCall(comp_ctx->builder, func,
  537. func_param_values, 6, "res"))) {
  538. aot_set_last_error("llvm build call failed.");
  539. return false;
  540. }
  541. /* get function result values */
  542. cell_num = 0;
  543. for (i = 0; i < result_count; i++) {
  544. ret_type = TO_LLVM_TYPE(wasm_ret_types[i]);
  545. if (!(ret_idx = I32_CONST(cell_num))
  546. || !(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  547. aot_set_last_error("llvm add const or pointer type failed.");
  548. return false;
  549. }
  550. snprintf(buf, sizeof(buf), "argv_ret%d", i);
  551. if (!(ret_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  552. func_ctx->argv_buf, &ret_idx, 1, buf))
  553. || !(ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ret_ptr,
  554. ret_ptr_type, buf))) {
  555. aot_set_last_error("llvm build GEP or bit cast failed.");
  556. return false;
  557. }
  558. snprintf(buf, sizeof(buf), "ret%d", i);
  559. if (!(value_rets[i] = LLVMBuildLoad(comp_ctx->builder, ret_ptr, buf))) {
  560. aot_set_last_error("llvm build load failed.");
  561. return false;
  562. }
  563. cell_num += wasm_value_type_cell_num(wasm_ret_types[i]);
  564. }
  565. *p_res = res;
  566. return true;
  567. }
  568. bool
  569. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  570. uint32 type_idx)
  571. {
  572. AOTFuncType *func_type;
  573. LLVMValueRef elem_idx, ftype_idx;
  574. LLVMValueRef *param_values = NULL, *value_rets = NULL, res = NULL;
  575. LLVMTypeRef *param_types = NULL;
  576. int32 i, param_count, result_count;
  577. uint32 param_cell_num;
  578. uint64 total_size;
  579. uint8 *wasm_ret_types = NULL;
  580. bool ret;
  581. /* Check function type index */
  582. if (type_idx >= comp_ctx->comp_data->func_type_count) {
  583. aot_set_last_error("type index is overflow");
  584. return false;
  585. }
  586. ftype_idx = I32_CONST(type_idx);
  587. CHECK_LLVM_CONST(ftype_idx);
  588. func_type = comp_ctx->comp_data->func_types[type_idx];
  589. param_cell_num = func_type->param_cell_num;
  590. result_count = func_type->result_count;
  591. wasm_ret_types = func_type->types + func_type->param_count;
  592. POP_I32(elem_idx);
  593. /* Initialize parameter types of the LLVM function */
  594. param_count = (int32)func_type->param_count;
  595. total_size = sizeof(LLVMTypeRef) * (uint64)param_count;
  596. if (total_size >= UINT32_MAX
  597. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  598. aot_set_last_error("Allocate memory failed.");
  599. goto fail;
  600. }
  601. for (i = 0; i < param_count; i++)
  602. param_types[i] = TO_LLVM_TYPE(func_type->types[i]);
  603. /* Allocate memory for parameters */
  604. total_size = sizeof(LLVMValueRef) * (uint64)param_count;
  605. if (total_size >= UINT32_MAX
  606. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  607. aot_set_last_error("Allocate memory failed.");
  608. goto fail;
  609. }
  610. /* Pop parameters from stack */
  611. for (i = param_count - 1; i >= 0; i--)
  612. POP(param_values[i], func_type->types[i]);
  613. /* Allocate memory for result values */
  614. total_size = sizeof(LLVMValueRef) * (uint64)result_count;
  615. if (total_size >= UINT32_MAX
  616. || !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
  617. aot_set_last_error("Allocate memory failed.");
  618. goto fail;
  619. }
  620. memset(value_rets, 0, total_size);
  621. if (!call_aot_call_indirect_func(comp_ctx, func_ctx,
  622. func_type, ftype_idx, elem_idx,
  623. param_types, param_values,
  624. param_count, param_cell_num,
  625. result_count, wasm_ret_types,
  626. value_rets, &res))
  627. goto fail;
  628. for (i = 0; i < func_type->result_count; i++)
  629. PUSH(value_rets[i], func_type->types[func_type->param_count + i]);
  630. /* Check whether there was exception thrown when executing the function */
  631. if (!check_call_return(comp_ctx, func_ctx, res))
  632. goto fail;
  633. ret = true;
  634. fail:
  635. if (value_rets)
  636. wasm_runtime_free(value_rets);
  637. if (param_values)
  638. wasm_runtime_free(param_values);
  639. if (param_types)
  640. wasm_runtime_free(param_types);
  641. return ret;
  642. }