aot_emit_function.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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_emit_table.h"
  9. #include "../aot/aot_runtime.h"
  10. #define ADD_BASIC_BLOCK(block, name) \
  11. do { \
  12. if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
  13. func_ctx->func, name))) { \
  14. aot_set_last_error("llvm add basic block failed."); \
  15. goto fail; \
  16. } \
  17. } while (0)
  18. static bool
  19. create_func_return_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  20. {
  21. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  22. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  23. /* Create function return block if it isn't created */
  24. if (!func_ctx->func_return_block) {
  25. if (!(func_ctx->func_return_block = LLVMAppendBasicBlockInContext(
  26. comp_ctx->context, func_ctx->func, "func_ret"))) {
  27. aot_set_last_error("llvm add basic block failed.");
  28. return false;
  29. }
  30. /* Create return IR */
  31. LLVMPositionBuilderAtEnd(comp_ctx->builder,
  32. func_ctx->func_return_block);
  33. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  34. return false;
  35. }
  36. }
  37. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  38. return true;
  39. }
  40. /* Check whether there was exception thrown, if yes, return directly */
  41. static bool
  42. check_exception_thrown(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  43. {
  44. LLVMBasicBlockRef block_curr, check_exce_succ;
  45. LLVMValueRef value, cmp;
  46. /* Create function return block if it isn't created */
  47. if (!create_func_return_block(comp_ctx, func_ctx))
  48. return false;
  49. /* Load the first byte of aot_module_inst->cur_exception, and check
  50. whether it is '\0'. If yes, no exception was thrown. */
  51. if (!(value = LLVMBuildLoad(comp_ctx->builder, func_ctx->cur_exception,
  52. "exce_value"))
  53. || !(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, value, I8_ZERO,
  54. "cmp"))) {
  55. aot_set_last_error("llvm build icmp failed.");
  56. return false;
  57. }
  58. /* Add check exection success block */
  59. if (!(check_exce_succ = LLVMAppendBasicBlockInContext(
  60. comp_ctx->context, func_ctx->func, "check_exce_succ"))) {
  61. aot_set_last_error("llvm add basic block failed.");
  62. return false;
  63. }
  64. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  65. LLVMMoveBasicBlockAfter(check_exce_succ, block_curr);
  66. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  67. /* Create condition br */
  68. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_exce_succ,
  69. func_ctx->func_return_block)) {
  70. aot_set_last_error("llvm build cond br failed.");
  71. return false;
  72. }
  73. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_exce_succ);
  74. return true;
  75. }
  76. /* Check whether there was exception thrown, if yes, return directly */
  77. static bool
  78. check_call_return(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  79. LLVMValueRef res)
  80. {
  81. LLVMBasicBlockRef block_curr, check_call_succ;
  82. LLVMValueRef cmp;
  83. /* Create function return block if it isn't created */
  84. if (!create_func_return_block(comp_ctx, func_ctx))
  85. return false;
  86. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, res, I8_ZERO,
  87. "cmp"))) {
  88. aot_set_last_error("llvm build icmp failed.");
  89. return false;
  90. }
  91. /* Add check exection success block */
  92. if (!(check_call_succ = LLVMAppendBasicBlockInContext(
  93. comp_ctx->context, func_ctx->func, "check_call_succ"))) {
  94. aot_set_last_error("llvm add basic block failed.");
  95. return false;
  96. }
  97. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  98. LLVMMoveBasicBlockAfter(check_call_succ, block_curr);
  99. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  100. /* Create condition br */
  101. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_call_succ,
  102. func_ctx->func_return_block)) {
  103. aot_set_last_error("llvm build cond br failed.");
  104. return false;
  105. }
  106. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_call_succ);
  107. return true;
  108. }
  109. static bool
  110. call_aot_invoke_native_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  111. LLVMValueRef func_idx, AOTFuncType *aot_func_type,
  112. LLVMTypeRef *param_types,
  113. LLVMValueRef *param_values, uint32 param_count,
  114. uint32 param_cell_num, LLVMTypeRef ret_type,
  115. uint8 wasm_ret_type, LLVMValueRef *p_value_ret,
  116. LLVMValueRef *p_res)
  117. {
  118. LLVMTypeRef func_type, func_ptr_type, func_param_types[4];
  119. LLVMTypeRef ret_ptr_type, elem_ptr_type;
  120. LLVMValueRef func, elem_idx, elem_ptr;
  121. LLVMValueRef func_param_values[4], value_ret = NULL, res;
  122. char buf[32], *func_name = "aot_invoke_native";
  123. uint32 i, cell_num = 0;
  124. /* prepare function type of aot_invoke_native */
  125. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  126. func_param_types[1] = I32_TYPE; /* func_idx */
  127. func_param_types[2] = I32_TYPE; /* argc */
  128. func_param_types[3] = INT32_PTR_TYPE; /* argv */
  129. if (!(func_type =
  130. LLVMFunctionType(INT8_TYPE, func_param_types, 4, false))) {
  131. aot_set_last_error("llvm add function type failed.");
  132. return false;
  133. }
  134. /* prepare function pointer */
  135. if (comp_ctx->is_jit_mode) {
  136. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  137. aot_set_last_error("create LLVM function type failed.");
  138. return false;
  139. }
  140. /* JIT mode, call the function directly */
  141. if (!(func = I64_CONST((uint64)(uintptr_t)aot_invoke_native))
  142. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  143. aot_set_last_error("create LLVM value failed.");
  144. return false;
  145. }
  146. }
  147. else if (comp_ctx->is_indirect_mode) {
  148. int32 func_index;
  149. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  150. aot_set_last_error("create LLVM function type failed.");
  151. return false;
  152. }
  153. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  154. if (func_index < 0) {
  155. return false;
  156. }
  157. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  158. func_ptr_type, func_index))) {
  159. return false;
  160. }
  161. }
  162. else {
  163. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  164. && !(func =
  165. LLVMAddFunction(comp_ctx->module, func_name, func_type))) {
  166. aot_set_last_error("add LLVM function failed.");
  167. return false;
  168. }
  169. }
  170. if (param_cell_num > 64) {
  171. aot_set_last_error("prepare native arguments failed: "
  172. "maximum 64 parameter cell number supported.");
  173. return false;
  174. }
  175. /* prepare frame_lp */
  176. for (i = 0; i < param_count; i++) {
  177. if (!(elem_idx = I32_CONST(cell_num))
  178. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  179. aot_set_last_error("llvm add const or pointer type failed.");
  180. return false;
  181. }
  182. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  183. if (!(elem_ptr = LLVMBuildInBoundsGEP(
  184. comp_ctx->builder, func_ctx->argv_buf, &elem_idx, 1, buf))
  185. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  186. elem_ptr_type, buf))) {
  187. aot_set_last_error("llvm build bit cast failed.");
  188. return false;
  189. }
  190. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  191. elem_ptr))) {
  192. aot_set_last_error("llvm build store failed.");
  193. return false;
  194. }
  195. LLVMSetAlignment(res, 1);
  196. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  197. }
  198. func_param_values[0] = func_ctx->exec_env;
  199. func_param_values[1] = func_idx;
  200. func_param_values[2] = I32_CONST(param_cell_num);
  201. func_param_values[3] = func_ctx->argv_buf;
  202. if (!func_param_values[2]) {
  203. aot_set_last_error("llvm create const failed.");
  204. return false;
  205. }
  206. /* call aot_invoke_native() function */
  207. if (!(res = LLVMBuildCall(comp_ctx->builder, func, func_param_values, 4,
  208. "res"))) {
  209. aot_set_last_error("llvm build call failed.");
  210. return false;
  211. }
  212. /* get function return value */
  213. if (wasm_ret_type != VALUE_TYPE_VOID) {
  214. if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  215. aot_set_last_error("llvm add pointer type failed.");
  216. return false;
  217. }
  218. if (!(value_ret =
  219. LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
  220. ret_ptr_type, "argv_ret"))) {
  221. aot_set_last_error("llvm build bit cast failed.");
  222. return false;
  223. }
  224. if (!(*p_value_ret =
  225. LLVMBuildLoad(comp_ctx->builder, value_ret, "value_ret"))) {
  226. aot_set_last_error("llvm build load failed.");
  227. return false;
  228. }
  229. }
  230. *p_res = res;
  231. return true;
  232. }
  233. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  234. static bool
  235. call_aot_alloc_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  236. LLVMValueRef func_idx)
  237. {
  238. LLVMValueRef param_values[2], ret_value, value, func;
  239. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  240. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  241. LLVMBasicBlockRef frame_alloc_fail, frame_alloc_success;
  242. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  243. param_types[0] = comp_ctx->exec_env_type;
  244. param_types[1] = I32_TYPE;
  245. ret_type = INT8_TYPE;
  246. GET_AOT_FUNCTION(aot_alloc_frame, 2);
  247. param_values[0] = func_ctx->exec_env;
  248. param_values[1] = func_idx;
  249. if (!(ret_value = LLVMBuildCall(comp_ctx->builder, func, param_values, 2,
  250. "call_aot_alloc_frame"))) {
  251. aot_set_last_error("llvm build call failed.");
  252. return false;
  253. }
  254. if (!(ret_value = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, ret_value,
  255. I8_ZERO, "frame_alloc_ret"))) {
  256. aot_set_last_error("llvm build icmp failed.");
  257. return false;
  258. }
  259. ADD_BASIC_BLOCK(frame_alloc_fail, "frame_alloc_fail");
  260. ADD_BASIC_BLOCK(frame_alloc_success, "frame_alloc_success");
  261. LLVMMoveBasicBlockAfter(frame_alloc_fail, block_curr);
  262. LLVMMoveBasicBlockAfter(frame_alloc_success, block_curr);
  263. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, frame_alloc_success,
  264. frame_alloc_fail)) {
  265. aot_set_last_error("llvm build cond br failed.");
  266. return false;
  267. }
  268. /* If frame alloc failed, return this function
  269. so the runtime can catch the exception */
  270. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_fail);
  271. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  272. return false;
  273. }
  274. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_success);
  275. return true;
  276. fail:
  277. return false;
  278. }
  279. static bool
  280. call_aot_free_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  281. {
  282. LLVMValueRef param_values[1], ret_value, value, func;
  283. LLVMTypeRef param_types[1], ret_type, func_type, func_ptr_type;
  284. param_types[0] = comp_ctx->exec_env_type;
  285. ret_type = INT8_TYPE;
  286. GET_AOT_FUNCTION(aot_free_frame, 1);
  287. param_values[0] = func_ctx->exec_env;
  288. if (!(ret_value = LLVMBuildCall(comp_ctx->builder, func, param_values, 1,
  289. "call_aot_free_frame"))) {
  290. aot_set_last_error("llvm build call failed.");
  291. return false;
  292. }
  293. return true;
  294. }
  295. #endif /* end of (WASM_ENABLE_DUMP_CALL_STACK != 0) \
  296. || (WASM_ENABLE_PERF_PROFILING != 0) */
  297. static bool
  298. check_stack_boundary(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  299. uint32 callee_cell_num)
  300. {
  301. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  302. LLVMBasicBlockRef check_stack;
  303. LLVMValueRef callee_local_size, stack_bound, cmp;
  304. if (!(callee_local_size = I32_CONST(callee_cell_num * 4))) {
  305. aot_set_last_error("llvm build const failed.");
  306. return false;
  307. }
  308. if (!(stack_bound = LLVMBuildInBoundsGEP(
  309. comp_ctx->builder, func_ctx->native_stack_bound,
  310. &callee_local_size, 1, "stack_bound"))) {
  311. aot_set_last_error("llvm build inbound gep failed.");
  312. return false;
  313. }
  314. if (!(check_stack = LLVMAppendBasicBlockInContext(
  315. comp_ctx->context, func_ctx->func, "check_stack"))) {
  316. aot_set_last_error("llvm add basic block failed.");
  317. return false;
  318. }
  319. LLVMMoveBasicBlockAfter(check_stack, block_curr);
  320. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT,
  321. func_ctx->last_alloca, stack_bound, "cmp"))) {
  322. aot_set_last_error("llvm build icmp failed.");
  323. return false;
  324. }
  325. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NATIVE_STACK_OVERFLOW,
  326. true, cmp, check_stack)) {
  327. return false;
  328. }
  329. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_stack);
  330. return true;
  331. }
  332. bool
  333. aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  334. uint32 func_idx, bool tail_call)
  335. {
  336. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  337. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  338. uint32 func_count = comp_ctx->func_ctx_count, param_cell_num = 0;
  339. uint32 ext_ret_cell_num = 0, cell_num = 0;
  340. AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
  341. AOTFuncType *func_type;
  342. AOTFunc *aot_func;
  343. LLVMTypeRef *param_types = NULL, ret_type;
  344. LLVMTypeRef ext_ret_ptr_type;
  345. LLVMValueRef *param_values = NULL, value_ret = NULL, func;
  346. LLVMValueRef import_func_idx, res;
  347. LLVMValueRef ext_ret, ext_ret_ptr, ext_ret_idx;
  348. int32 i, j = 0, param_count, result_count, ext_ret_count;
  349. uint64 total_size;
  350. uint32 callee_cell_num;
  351. uint8 wasm_ret_type;
  352. uint8 *ext_ret_types = NULL;
  353. bool ret = false;
  354. char buf[32];
  355. #if WASM_ENABLE_THREAD_MGR != 0
  356. /* Insert suspend check point */
  357. if (comp_ctx->enable_thread_mgr) {
  358. if (!check_suspend_flags(comp_ctx, func_ctx))
  359. return false;
  360. }
  361. #endif
  362. /* Check function index */
  363. if (func_idx >= import_func_count + func_count) {
  364. aot_set_last_error("Function index out of range.");
  365. return false;
  366. }
  367. /* Get function type */
  368. if (func_idx < import_func_count)
  369. func_type = import_funcs[func_idx].func_type;
  370. else
  371. func_type =
  372. func_ctxes[func_idx - import_func_count]->aot_func->func_type;
  373. /* Get param cell number */
  374. param_cell_num = func_type->param_cell_num;
  375. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  376. if (comp_ctx->enable_aux_stack_frame) {
  377. LLVMValueRef func_idx_const;
  378. if (!(func_idx_const = I32_CONST(func_idx))) {
  379. aot_set_last_error("llvm build const failed.");
  380. return false;
  381. }
  382. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx_const))
  383. return false;
  384. }
  385. #endif
  386. /* Allocate memory for parameters.
  387. * Parameters layout:
  388. * - exec env
  389. * - wasm function's parameters
  390. * - extra results'(except the first one) addresses
  391. */
  392. param_count = (int32)func_type->param_count;
  393. result_count = (int32)func_type->result_count;
  394. ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  395. total_size =
  396. sizeof(LLVMValueRef) * (uint64)(param_count + 1 + ext_ret_count);
  397. if (total_size >= UINT32_MAX
  398. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  399. aot_set_last_error("allocate memory failed.");
  400. return false;
  401. }
  402. /* First parameter is exec env */
  403. param_values[j++] = func_ctx->exec_env;
  404. /* Pop parameters from stack */
  405. for (i = param_count - 1; i >= 0; i--)
  406. POP(param_values[i + j], func_type->types[i]);
  407. /* Set parameters for multiple return values, the first return value
  408. is returned by function return value, and the other return values
  409. are returned by function parameters with pointer types */
  410. if (ext_ret_count > 0) {
  411. ext_ret_types = func_type->types + param_count + 1;
  412. ext_ret_cell_num = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  413. if (ext_ret_cell_num > 64) {
  414. aot_set_last_error("prepare extra results's return "
  415. "address arguments failed: "
  416. "maximum 64 parameter cell number supported.");
  417. goto fail;
  418. }
  419. for (i = 0; i < ext_ret_count; i++) {
  420. if (!(ext_ret_idx = I32_CONST(cell_num))
  421. || !(ext_ret_ptr_type =
  422. LLVMPointerType(TO_LLVM_TYPE(ext_ret_types[i]), 0))) {
  423. aot_set_last_error("llvm add const or pointer type failed.");
  424. goto fail;
  425. }
  426. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i);
  427. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  428. func_ctx->argv_buf,
  429. &ext_ret_idx, 1, buf))) {
  430. aot_set_last_error("llvm build GEP failed.");
  431. goto fail;
  432. }
  433. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i);
  434. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  435. ext_ret_ptr_type, buf))) {
  436. aot_set_last_error("llvm build bit cast failed.");
  437. goto fail;
  438. }
  439. param_values[param_count + 1 + i] = ext_ret_ptr;
  440. cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
  441. }
  442. }
  443. if (func_idx < import_func_count) {
  444. if (!(import_func_idx = I32_CONST(func_idx))) {
  445. aot_set_last_error("llvm build inbounds gep failed.");
  446. goto fail;
  447. }
  448. /* Initialize parameter types of the LLVM function */
  449. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  450. if (total_size >= UINT32_MAX
  451. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  452. aot_set_last_error("allocate memory failed.");
  453. goto fail;
  454. }
  455. j = 0;
  456. param_types[j++] = comp_ctx->exec_env_type;
  457. for (i = 0; i < param_count; i++)
  458. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  459. if (func_type->result_count) {
  460. wasm_ret_type = func_type->types[func_type->param_count];
  461. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  462. }
  463. else {
  464. wasm_ret_type = VALUE_TYPE_VOID;
  465. ret_type = VOID_TYPE;
  466. }
  467. /* call aot_invoke_native() */
  468. if (!call_aot_invoke_native_func(
  469. comp_ctx, func_ctx, import_func_idx, func_type, param_types + 1,
  470. param_values + 1, param_count, param_cell_num, ret_type,
  471. wasm_ret_type, &value_ret, &res))
  472. goto fail;
  473. /* Check whether there was exception thrown when executing
  474. the function */
  475. if (!check_call_return(comp_ctx, func_ctx, res))
  476. goto fail;
  477. }
  478. else {
  479. if (comp_ctx->is_indirect_mode) {
  480. LLVMTypeRef func_ptr_type;
  481. if (!(func_ptr_type = LLVMPointerType(
  482. func_ctxes[func_idx - import_func_count]->func_type,
  483. 0))) {
  484. aot_set_last_error("construct func ptr type failed.");
  485. goto fail;
  486. }
  487. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->func_ptrs,
  488. func_ptr_type, func_idx))) {
  489. goto fail;
  490. }
  491. }
  492. else {
  493. func = func_ctxes[func_idx - import_func_count]->func;
  494. }
  495. aot_func = func_ctxes[func_idx - import_func_count]->aot_func;
  496. callee_cell_num =
  497. aot_func->param_cell_num + aot_func->local_cell_num + 1;
  498. if (comp_ctx->enable_bound_check
  499. && !check_stack_boundary(comp_ctx, func_ctx, callee_cell_num))
  500. goto fail;
  501. /* Call the function */
  502. if (!(value_ret =
  503. LLVMBuildCall(comp_ctx->builder, func, param_values,
  504. (uint32)param_count + 1 + ext_ret_count,
  505. (func_type->result_count > 0 ? "call" : "")))) {
  506. aot_set_last_error("LLVM build call failed.");
  507. goto fail;
  508. }
  509. /* Set calling convention for the call with the func's calling
  510. convention */
  511. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  512. if (tail_call)
  513. LLVMSetTailCall(value_ret, true);
  514. /* Check whether there was exception thrown when executing
  515. the function */
  516. if (!tail_call && !check_exception_thrown(comp_ctx, func_ctx))
  517. goto fail;
  518. }
  519. if (func_type->result_count > 0) {
  520. /* Push the first result to stack */
  521. PUSH(value_ret, func_type->types[func_type->param_count]);
  522. /* Load extra result from its address and push to stack */
  523. for (i = 0; i < ext_ret_count; i++) {
  524. snprintf(buf, sizeof(buf), "func%d_ext_ret%d", func_idx, i);
  525. if (!(ext_ret =
  526. LLVMBuildLoad(comp_ctx->builder,
  527. param_values[1 + param_count + i], buf))) {
  528. aot_set_last_error("llvm build load failed.");
  529. goto fail;
  530. }
  531. PUSH(ext_ret, ext_ret_types[i]);
  532. }
  533. }
  534. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  535. if (comp_ctx->enable_aux_stack_frame) {
  536. if (!call_aot_free_frame_func(comp_ctx, func_ctx))
  537. goto fail;
  538. }
  539. #endif
  540. ret = true;
  541. fail:
  542. if (param_types)
  543. wasm_runtime_free(param_types);
  544. if (param_values)
  545. wasm_runtime_free(param_values);
  546. return ret;
  547. }
  548. static bool
  549. call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  550. AOTFuncType *aot_func_type,
  551. LLVMValueRef func_type_idx, LLVMValueRef table_idx,
  552. LLVMValueRef table_elem_idx,
  553. LLVMTypeRef *param_types,
  554. LLVMValueRef *param_values, uint32 param_count,
  555. uint32 param_cell_num, uint32 result_count,
  556. uint8 *wasm_ret_types, LLVMValueRef *value_rets,
  557. LLVMValueRef *p_res)
  558. {
  559. LLVMTypeRef func_type, func_ptr_type, func_param_types[6];
  560. LLVMTypeRef ret_type, ret_ptr_type, elem_ptr_type;
  561. LLVMValueRef func, ret_idx, ret_ptr, elem_idx, elem_ptr;
  562. LLVMValueRef func_param_values[6], res = NULL;
  563. char buf[32], *func_name = "aot_call_indirect";
  564. uint32 i, cell_num = 0, ret_cell_num, argv_cell_num;
  565. /* prepare function type of aot_call_indirect */
  566. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  567. func_param_types[1] = I32_TYPE; /* table_idx */
  568. func_param_types[2] = I32_TYPE; /* table_elem_idx */
  569. func_param_types[3] = I32_TYPE; /* argc */
  570. func_param_types[4] = INT32_PTR_TYPE; /* argv */
  571. if (!(func_type =
  572. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  573. aot_set_last_error("llvm add function type failed.");
  574. return false;
  575. }
  576. /* prepare function pointer */
  577. if (comp_ctx->is_jit_mode) {
  578. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  579. aot_set_last_error("create LLVM function type failed.");
  580. return false;
  581. }
  582. /* JIT mode, call the function directly */
  583. if (!(func = I64_CONST((uint64)(uintptr_t)aot_call_indirect))
  584. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  585. aot_set_last_error("create LLVM value failed.");
  586. return false;
  587. }
  588. }
  589. else if (comp_ctx->is_indirect_mode) {
  590. int32 func_index;
  591. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  592. aot_set_last_error("create LLVM function type failed.");
  593. return false;
  594. }
  595. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  596. if (func_index < 0) {
  597. return false;
  598. }
  599. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  600. func_ptr_type, func_index))) {
  601. return false;
  602. }
  603. }
  604. else {
  605. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  606. && !(func =
  607. LLVMAddFunction(comp_ctx->module, func_name, func_type))) {
  608. aot_set_last_error("add LLVM function failed.");
  609. return false;
  610. }
  611. }
  612. ret_cell_num = wasm_get_cell_num(wasm_ret_types, result_count);
  613. argv_cell_num =
  614. param_cell_num > ret_cell_num ? param_cell_num : ret_cell_num;
  615. if (argv_cell_num > 64) {
  616. aot_set_last_error("prepare native arguments failed: "
  617. "maximum 64 parameter cell number supported.");
  618. return false;
  619. }
  620. /* prepare frame_lp */
  621. for (i = 0; i < param_count; i++) {
  622. if (!(elem_idx = I32_CONST(cell_num))
  623. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  624. aot_set_last_error("llvm add const or pointer type failed.");
  625. return false;
  626. }
  627. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  628. if (!(elem_ptr = LLVMBuildInBoundsGEP(
  629. comp_ctx->builder, func_ctx->argv_buf, &elem_idx, 1, buf))
  630. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  631. elem_ptr_type, buf))) {
  632. aot_set_last_error("llvm build bit cast failed.");
  633. return false;
  634. }
  635. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  636. elem_ptr))) {
  637. aot_set_last_error("llvm build store failed.");
  638. return false;
  639. }
  640. LLVMSetAlignment(res, 1);
  641. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  642. }
  643. func_param_values[0] = func_ctx->exec_env;
  644. func_param_values[1] = table_idx;
  645. func_param_values[2] = table_elem_idx;
  646. func_param_values[3] = I32_CONST(param_cell_num);
  647. func_param_values[4] = func_ctx->argv_buf;
  648. if (!func_param_values[3]) {
  649. aot_set_last_error("llvm create const failed.");
  650. return false;
  651. }
  652. /* call aot_call_indirect() function */
  653. if (!(res = LLVMBuildCall(comp_ctx->builder, func, func_param_values, 5,
  654. "res"))) {
  655. aot_set_last_error("llvm build call failed.");
  656. return false;
  657. }
  658. /* get function result values */
  659. cell_num = 0;
  660. for (i = 0; i < result_count; i++) {
  661. ret_type = TO_LLVM_TYPE(wasm_ret_types[i]);
  662. if (!(ret_idx = I32_CONST(cell_num))
  663. || !(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  664. aot_set_last_error("llvm add const or pointer type failed.");
  665. return false;
  666. }
  667. snprintf(buf, sizeof(buf), "argv_ret%d", i);
  668. if (!(ret_ptr = LLVMBuildInBoundsGEP(
  669. comp_ctx->builder, func_ctx->argv_buf, &ret_idx, 1, buf))
  670. || !(ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ret_ptr,
  671. ret_ptr_type, buf))) {
  672. aot_set_last_error("llvm build GEP or bit cast failed.");
  673. return false;
  674. }
  675. snprintf(buf, sizeof(buf), "ret%d", i);
  676. if (!(value_rets[i] = LLVMBuildLoad(comp_ctx->builder, ret_ptr, buf))) {
  677. aot_set_last_error("llvm build load failed.");
  678. return false;
  679. }
  680. cell_num += wasm_value_type_cell_num(wasm_ret_types[i]);
  681. }
  682. *p_res = res;
  683. return true;
  684. }
  685. bool
  686. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  687. uint32 type_idx, uint32 tbl_idx)
  688. {
  689. AOTFuncType *func_type;
  690. LLVMValueRef tbl_idx_value, elem_idx, table_elem, func_idx;
  691. LLVMValueRef ftype_idx_ptr, ftype_idx, ftype_idx_const;
  692. LLVMValueRef cmp_elem_idx, cmp_func_idx, cmp_ftype_idx;
  693. LLVMValueRef func, func_ptr, table_size_const;
  694. LLVMValueRef ext_ret_offset, ext_ret_ptr, ext_ret, res;
  695. LLVMValueRef *param_values = NULL, *value_rets = NULL;
  696. LLVMValueRef *result_phis = NULL, value_ret, import_func_count;
  697. LLVMTypeRef *param_types = NULL, ret_type;
  698. LLVMTypeRef llvm_func_type, llvm_func_ptr_type;
  699. LLVMTypeRef ext_ret_ptr_type;
  700. LLVMBasicBlockRef check_elem_idx_succ, check_ftype_idx_succ;
  701. LLVMBasicBlockRef check_func_idx_succ, block_return, block_curr;
  702. LLVMBasicBlockRef block_call_import, block_call_non_import;
  703. LLVMValueRef offset;
  704. uint32 total_param_count, func_param_count, func_result_count;
  705. uint32 ext_cell_num, param_cell_num, i, j;
  706. uint8 wasm_ret_type, *wasm_ret_types;
  707. uint64 total_size;
  708. char buf[32];
  709. bool ret = false;
  710. /* Check function type index */
  711. if (type_idx >= comp_ctx->comp_data->func_type_count) {
  712. aot_set_last_error("function type index out of range");
  713. return false;
  714. }
  715. /* Find the equivalent function type whose type index is the smallest:
  716. the callee function's type index is also converted to the smallest
  717. one in wasm loader, so we can just check whether the two type indexes
  718. are equal (the type index of call_indirect opcode and callee func),
  719. we don't need to check whether the whole function types are equal,
  720. including param types and result types. */
  721. type_idx = wasm_get_smallest_type_idx(comp_ctx->comp_data->func_types,
  722. comp_ctx->comp_data->func_type_count,
  723. type_idx);
  724. ftype_idx_const = I32_CONST(type_idx);
  725. CHECK_LLVM_CONST(ftype_idx_const);
  726. func_type = comp_ctx->comp_data->func_types[type_idx];
  727. func_param_count = func_type->param_count;
  728. func_result_count = func_type->result_count;
  729. POP_I32(elem_idx);
  730. /* get the cur size of the table instance */
  731. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  732. + offsetof(AOTTableInstance, cur_size)))) {
  733. HANDLE_FAILURE("LLVMConstInt");
  734. goto fail;
  735. }
  736. if (!(table_size_const = LLVMBuildGEP(comp_ctx->builder, func_ctx->aot_inst,
  737. &offset, 1, "cur_size_i8p"))) {
  738. HANDLE_FAILURE("LLVMBuildGEP");
  739. goto fail;
  740. }
  741. if (!(table_size_const =
  742. LLVMBuildBitCast(comp_ctx->builder, table_size_const,
  743. INT32_PTR_TYPE, "cur_siuze_i32p"))) {
  744. HANDLE_FAILURE("LLVMBuildBitCast");
  745. goto fail;
  746. }
  747. if (!(table_size_const =
  748. LLVMBuildLoad(comp_ctx->builder, table_size_const, "cur_size"))) {
  749. HANDLE_FAILURE("LLVMBuildLoad");
  750. goto fail;
  751. }
  752. /* Check if (uint32)elem index >= table size */
  753. if (!(cmp_elem_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGE, elem_idx,
  754. table_size_const, "cmp_elem_idx"))) {
  755. aot_set_last_error("llvm build icmp failed.");
  756. goto fail;
  757. }
  758. /* Throw exception if elem index >= table size */
  759. if (!(check_elem_idx_succ = LLVMAppendBasicBlockInContext(
  760. comp_ctx->context, func_ctx->func, "check_elem_idx_succ"))) {
  761. aot_set_last_error("llvm add basic block failed.");
  762. goto fail;
  763. }
  764. LLVMMoveBasicBlockAfter(check_elem_idx_succ,
  765. LLVMGetInsertBlock(comp_ctx->builder));
  766. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNDEFINED_ELEMENT, true,
  767. cmp_elem_idx, check_elem_idx_succ)))
  768. goto fail;
  769. /* load data as i32* */
  770. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  771. + offsetof(AOTTableInstance, data)))) {
  772. HANDLE_FAILURE("LLVMConstInt");
  773. goto fail;
  774. }
  775. if (!(table_elem = LLVMBuildGEP(comp_ctx->builder, func_ctx->aot_inst,
  776. &offset, 1, "table_elem_i8p"))) {
  777. aot_set_last_error("llvm build add failed.");
  778. goto fail;
  779. }
  780. if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem,
  781. INT32_PTR_TYPE, "table_elem_i32p"))) {
  782. HANDLE_FAILURE("LLVMBuildBitCast");
  783. goto fail;
  784. }
  785. /* Load function index */
  786. if (!(table_elem = LLVMBuildGEP(comp_ctx->builder, table_elem, &elem_idx, 1,
  787. "table_elem"))) {
  788. HANDLE_FAILURE("LLVMBuildNUWAdd");
  789. goto fail;
  790. }
  791. if (!(func_idx =
  792. LLVMBuildLoad(comp_ctx->builder, table_elem, "func_idx"))) {
  793. aot_set_last_error("llvm build load failed.");
  794. goto fail;
  795. }
  796. /* Check if func_idx == -1 */
  797. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, func_idx,
  798. I32_NEG_ONE, "cmp_func_idx"))) {
  799. aot_set_last_error("llvm build icmp failed.");
  800. goto fail;
  801. }
  802. /* Throw exception if func_idx == -1 */
  803. if (!(check_func_idx_succ = LLVMAppendBasicBlockInContext(
  804. comp_ctx->context, func_ctx->func, "check_func_idx_succ"))) {
  805. aot_set_last_error("llvm add basic block failed.");
  806. goto fail;
  807. }
  808. LLVMMoveBasicBlockAfter(check_func_idx_succ,
  809. LLVMGetInsertBlock(comp_ctx->builder));
  810. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNINITIALIZED_ELEMENT,
  811. true, cmp_func_idx, check_func_idx_succ)))
  812. goto fail;
  813. /* Load function type index */
  814. if (!(ftype_idx_ptr = LLVMBuildInBoundsGEP(
  815. comp_ctx->builder, func_ctx->func_type_indexes, &func_idx, 1,
  816. "ftype_idx_ptr"))) {
  817. aot_set_last_error("llvm build inbounds gep failed.");
  818. goto fail;
  819. }
  820. if (!(ftype_idx =
  821. LLVMBuildLoad(comp_ctx->builder, ftype_idx_ptr, "ftype_idx"))) {
  822. aot_set_last_error("llvm build load failed.");
  823. goto fail;
  824. }
  825. /* Check if function type index not equal */
  826. if (!(cmp_ftype_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, ftype_idx,
  827. ftype_idx_const, "cmp_ftype_idx"))) {
  828. aot_set_last_error("llvm build icmp failed.");
  829. goto fail;
  830. }
  831. /* Throw exception if ftype_idx != ftype_idx_const */
  832. if (!(check_ftype_idx_succ = LLVMAppendBasicBlockInContext(
  833. comp_ctx->context, func_ctx->func, "check_ftype_idx_succ"))) {
  834. aot_set_last_error("llvm add basic block failed.");
  835. goto fail;
  836. }
  837. LLVMMoveBasicBlockAfter(check_ftype_idx_succ,
  838. LLVMGetInsertBlock(comp_ctx->builder));
  839. if (!(aot_emit_exception(comp_ctx, func_ctx,
  840. EXCE_INVALID_FUNCTION_TYPE_INDEX, true,
  841. cmp_ftype_idx, check_ftype_idx_succ)))
  842. goto fail;
  843. /* Initialize parameter types of the LLVM function */
  844. total_param_count = 1 + func_param_count;
  845. /* Extra function results' addresses (except the first one) are
  846. appended to aot function parameters. */
  847. if (func_result_count > 1)
  848. total_param_count += func_result_count - 1;
  849. total_size = sizeof(LLVMTypeRef) * (uint64)total_param_count;
  850. if (total_size >= UINT32_MAX
  851. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  852. aot_set_last_error("allocate memory failed.");
  853. goto fail;
  854. }
  855. /* Prepare param types */
  856. j = 0;
  857. param_types[j++] = comp_ctx->exec_env_type;
  858. for (i = 0; i < func_param_count; i++)
  859. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  860. for (i = 1; i < func_result_count; i++, j++) {
  861. param_types[j] = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  862. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  863. aot_set_last_error("llvm get pointer type failed.");
  864. goto fail;
  865. }
  866. }
  867. /* Resolve return type of the LLVM function */
  868. if (func_result_count) {
  869. wasm_ret_type = func_type->types[func_param_count];
  870. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  871. }
  872. else {
  873. wasm_ret_type = VALUE_TYPE_VOID;
  874. ret_type = VOID_TYPE;
  875. }
  876. /* Allocate memory for parameters */
  877. total_size = sizeof(LLVMValueRef) * (uint64)total_param_count;
  878. if (total_size >= UINT32_MAX
  879. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  880. aot_set_last_error("allocate memory failed.");
  881. goto fail;
  882. }
  883. /* First parameter is exec env */
  884. j = 0;
  885. param_values[j++] = func_ctx->exec_env;
  886. /* Pop parameters from stack */
  887. for (i = func_param_count - 1; (int32)i >= 0; i--)
  888. POP(param_values[i + j], func_type->types[i]);
  889. /* Prepare extra parameters */
  890. ext_cell_num = 0;
  891. for (i = 1; i < func_result_count; i++) {
  892. ext_ret_offset = I32_CONST(ext_cell_num);
  893. CHECK_LLVM_CONST(ext_ret_offset);
  894. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i - 1);
  895. if (!(ext_ret_ptr =
  896. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->argv_buf,
  897. &ext_ret_offset, 1, buf))) {
  898. aot_set_last_error("llvm build GEP failed.");
  899. goto fail;
  900. }
  901. ext_ret_ptr_type = param_types[func_param_count + i];
  902. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i - 1);
  903. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  904. ext_ret_ptr_type, buf))) {
  905. aot_set_last_error("llvm build bit cast failed.");
  906. goto fail;
  907. }
  908. param_values[func_param_count + i] = ext_ret_ptr;
  909. ext_cell_num +=
  910. wasm_value_type_cell_num(func_type->types[func_param_count + i]);
  911. }
  912. if (ext_cell_num > 64) {
  913. aot_set_last_error("prepare call-indirect arguments failed: "
  914. "maximum 64 extra cell number supported.");
  915. goto fail;
  916. }
  917. #if WASM_ENABLE_THREAD_MGR != 0
  918. /* Insert suspend check point */
  919. if (comp_ctx->enable_thread_mgr) {
  920. if (!check_suspend_flags(comp_ctx, func_ctx))
  921. goto fail;
  922. }
  923. #endif
  924. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  925. if (comp_ctx->enable_aux_stack_frame) {
  926. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx))
  927. goto fail;
  928. }
  929. #endif
  930. /* Add basic blocks */
  931. block_call_import = LLVMAppendBasicBlockInContext(
  932. comp_ctx->context, func_ctx->func, "call_import");
  933. block_call_non_import = LLVMAppendBasicBlockInContext(
  934. comp_ctx->context, func_ctx->func, "call_non_import");
  935. block_return = LLVMAppendBasicBlockInContext(comp_ctx->context,
  936. func_ctx->func, "func_return");
  937. if (!block_call_import || !block_call_non_import || !block_return) {
  938. aot_set_last_error("llvm add basic block failed.");
  939. goto fail;
  940. }
  941. LLVMMoveBasicBlockAfter(block_call_import,
  942. LLVMGetInsertBlock(comp_ctx->builder));
  943. LLVMMoveBasicBlockAfter(block_call_non_import, block_call_import);
  944. LLVMMoveBasicBlockAfter(block_return, block_call_non_import);
  945. import_func_count = I32_CONST(comp_ctx->comp_data->import_func_count);
  946. CHECK_LLVM_CONST(import_func_count);
  947. /* Check if func_idx < import_func_count */
  948. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT, func_idx,
  949. import_func_count, "cmp_func_idx"))) {
  950. aot_set_last_error("llvm build icmp failed.");
  951. goto fail;
  952. }
  953. /* If func_idx < import_func_count, jump to call import block,
  954. else jump to call non-import block */
  955. if (!LLVMBuildCondBr(comp_ctx->builder, cmp_func_idx, block_call_import,
  956. block_call_non_import)) {
  957. aot_set_last_error("llvm build cond br failed.");
  958. goto fail;
  959. }
  960. /* Add result phis for return block */
  961. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  962. if (func_result_count > 0) {
  963. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  964. if (total_size >= UINT32_MAX
  965. || !(result_phis = wasm_runtime_malloc((uint32)total_size))) {
  966. aot_set_last_error("allocate memory failed.");
  967. goto fail;
  968. }
  969. memset(result_phis, 0, (uint32)total_size);
  970. for (i = 0; i < func_result_count; i++) {
  971. LLVMTypeRef tmp_type =
  972. TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  973. if (!(result_phis[i] =
  974. LLVMBuildPhi(comp_ctx->builder, tmp_type, "phi"))) {
  975. aot_set_last_error("llvm build phi failed.");
  976. goto fail;
  977. }
  978. }
  979. }
  980. /* Translate call import block */
  981. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_import);
  982. /* Allocate memory for result values */
  983. if (func_result_count > 0) {
  984. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  985. if (total_size >= UINT32_MAX
  986. || !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
  987. aot_set_last_error("allocate memory failed.");
  988. goto fail;
  989. }
  990. memset(value_rets, 0, (uint32)total_size);
  991. }
  992. param_cell_num = func_type->param_cell_num;
  993. wasm_ret_types = func_type->types + func_type->param_count;
  994. tbl_idx_value = I32_CONST(tbl_idx);
  995. if (!tbl_idx_value) {
  996. aot_set_last_error("llvm create const failed.");
  997. goto fail;
  998. }
  999. if (!call_aot_call_indirect_func(
  1000. comp_ctx, func_ctx, func_type, ftype_idx, tbl_idx_value, elem_idx,
  1001. param_types + 1, param_values + 1, func_param_count, param_cell_num,
  1002. func_result_count, wasm_ret_types, value_rets, &res))
  1003. goto fail;
  1004. /* Check whether exception was thrown when executing the function */
  1005. if (!check_call_return(comp_ctx, func_ctx, res))
  1006. goto fail;
  1007. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1008. for (i = 0; i < func_result_count; i++) {
  1009. LLVMAddIncoming(result_phis[i], &value_rets[i], &block_curr, 1);
  1010. }
  1011. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1012. aot_set_last_error("llvm build br failed.");
  1013. goto fail;
  1014. }
  1015. /* Translate call non-import block */
  1016. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_non_import);
  1017. if (comp_ctx->enable_bound_check
  1018. && !check_stack_boundary(comp_ctx, func_ctx,
  1019. param_cell_num + ext_cell_num
  1020. + 1
  1021. /* Reserve some local variables */
  1022. + 16))
  1023. goto fail;
  1024. /* Load function pointer */
  1025. if (!(func_ptr =
  1026. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->func_ptrs,
  1027. &func_idx, 1, "func_ptr_tmp"))) {
  1028. aot_set_last_error("llvm build inbounds gep failed.");
  1029. goto fail;
  1030. }
  1031. if (!(func_ptr = LLVMBuildLoad(comp_ctx->builder, func_ptr, "func_ptr"))) {
  1032. aot_set_last_error("llvm build load failed.");
  1033. goto fail;
  1034. }
  1035. if (!(llvm_func_type =
  1036. LLVMFunctionType(ret_type, param_types, total_param_count, false))
  1037. || !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
  1038. aot_set_last_error("llvm add function type failed.");
  1039. goto fail;
  1040. }
  1041. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  1042. llvm_func_ptr_type, "indirect_func"))) {
  1043. aot_set_last_error("llvm build bit cast failed.");
  1044. goto fail;
  1045. }
  1046. if (!(value_ret = LLVMBuildCall(comp_ctx->builder, func, param_values,
  1047. total_param_count,
  1048. func_result_count > 0 ? "ret" : ""))) {
  1049. aot_set_last_error("llvm build call failed.");
  1050. goto fail;
  1051. }
  1052. /* Check whether exception was thrown when executing the function */
  1053. if (!check_exception_thrown(comp_ctx, func_ctx))
  1054. goto fail;
  1055. if (func_result_count > 0) {
  1056. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1057. /* Push the first result to stack */
  1058. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  1059. /* Load extra result from its address and push to stack */
  1060. for (i = 1; i < func_result_count; i++) {
  1061. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  1062. if (!(ext_ret =
  1063. LLVMBuildLoad(comp_ctx->builder,
  1064. param_values[func_param_count + i], buf))) {
  1065. aot_set_last_error("llvm build load failed.");
  1066. goto fail;
  1067. }
  1068. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  1069. }
  1070. }
  1071. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1072. aot_set_last_error("llvm build br failed.");
  1073. goto fail;
  1074. }
  1075. /* Translate function return block */
  1076. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  1077. for (i = 0; i < func_result_count; i++) {
  1078. PUSH(result_phis[i], func_type->types[func_param_count + i]);
  1079. }
  1080. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1081. if (comp_ctx->enable_aux_stack_frame) {
  1082. if (!call_aot_free_frame_func(comp_ctx, func_ctx))
  1083. goto fail;
  1084. }
  1085. #endif
  1086. ret = true;
  1087. fail:
  1088. if (param_values)
  1089. wasm_runtime_free(param_values);
  1090. if (param_types)
  1091. wasm_runtime_free(param_types);
  1092. if (value_rets)
  1093. wasm_runtime_free(value_rets);
  1094. if (result_phis)
  1095. wasm_runtime_free(result_phis);
  1096. return ret;
  1097. }
  1098. bool
  1099. aot_compile_op_ref_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1100. {
  1101. PUSH_I32(REF_NULL);
  1102. return true;
  1103. fail:
  1104. return false;
  1105. }
  1106. bool
  1107. aot_compile_op_ref_is_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1108. {
  1109. LLVMValueRef lhs, res;
  1110. POP_I32(lhs);
  1111. if (!(res = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, lhs, REF_NULL,
  1112. "cmp_w_null"))) {
  1113. HANDLE_FAILURE("LLVMBuildICmp");
  1114. goto fail;
  1115. }
  1116. if (!(res = LLVMBuildZExt(comp_ctx->builder, res, I32_TYPE, "r_i"))) {
  1117. HANDLE_FAILURE("LLVMBuildZExt");
  1118. goto fail;
  1119. }
  1120. PUSH_I32(res);
  1121. return true;
  1122. fail:
  1123. return false;
  1124. }
  1125. bool
  1126. aot_compile_op_ref_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1127. uint32 func_idx)
  1128. {
  1129. LLVMValueRef ref_idx;
  1130. if (!(ref_idx = I32_CONST(func_idx))) {
  1131. HANDLE_FAILURE("LLVMConstInt");
  1132. goto fail;
  1133. }
  1134. PUSH_I32(ref_idx);
  1135. return true;
  1136. fail:
  1137. return false;
  1138. }