aot_emit_function.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  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 (!comp_ctx->enable_bound_check) {
  34. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_ALREADY_THROWN,
  35. false, NULL, NULL)) {
  36. return false;
  37. }
  38. }
  39. else if (!aot_build_zero_function_ret(comp_ctx, func_ctx,
  40. aot_func_type)) {
  41. return false;
  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 = LLVMBuildLoad2(comp_ctx->builder, INT8_TYPE,
  59. func_ctx->cur_exception, "exce_value"))
  60. || !(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, value, I8_ZERO,
  61. "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(
  67. comp_ctx->context, func_ctx->func, "check_exce_succ"))) {
  68. aot_set_last_error("llvm add basic block failed.");
  69. return false;
  70. }
  71. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  72. LLVMMoveBasicBlockAfter(check_exce_succ, block_curr);
  73. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  74. /* Create condition br */
  75. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_exce_succ,
  76. func_ctx->func_return_block)) {
  77. aot_set_last_error("llvm build cond br failed.");
  78. return false;
  79. }
  80. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_exce_succ);
  81. return true;
  82. }
  83. /* Check whether there was exception thrown, if yes, return directly */
  84. static bool
  85. check_call_return(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  86. LLVMValueRef res)
  87. {
  88. LLVMBasicBlockRef block_curr, check_call_succ;
  89. LLVMValueRef cmp;
  90. /* Create function return block if it isn't created */
  91. if (!create_func_return_block(comp_ctx, func_ctx))
  92. return false;
  93. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, res, I8_ZERO,
  94. "cmp"))) {
  95. aot_set_last_error("llvm build icmp failed.");
  96. return false;
  97. }
  98. /* Add check exection success block */
  99. if (!(check_call_succ = LLVMAppendBasicBlockInContext(
  100. comp_ctx->context, func_ctx->func, "check_call_succ"))) {
  101. aot_set_last_error("llvm add basic block failed.");
  102. return false;
  103. }
  104. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  105. LLVMMoveBasicBlockAfter(check_call_succ, block_curr);
  106. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  107. /* Create condition br */
  108. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_call_succ,
  109. func_ctx->func_return_block)) {
  110. aot_set_last_error("llvm build cond br failed.");
  111. return false;
  112. }
  113. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_call_succ);
  114. return true;
  115. }
  116. static bool
  117. call_aot_invoke_native_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  118. LLVMValueRef func_idx, AOTFuncType *aot_func_type,
  119. LLVMTypeRef *param_types,
  120. LLVMValueRef *param_values, uint32 param_count,
  121. uint32 param_cell_num, LLVMTypeRef ret_type,
  122. uint8 wasm_ret_type, LLVMValueRef *p_value_ret,
  123. LLVMValueRef *p_res)
  124. {
  125. LLVMTypeRef func_type, func_ptr_type, func_param_types[4];
  126. LLVMTypeRef ret_ptr_type, elem_ptr_type;
  127. LLVMValueRef func, elem_idx, elem_ptr;
  128. LLVMValueRef func_param_values[4], value_ret = NULL, res;
  129. char buf[32], *func_name = "aot_invoke_native";
  130. uint32 i, cell_num = 0;
  131. /* prepare function type of aot_invoke_native */
  132. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  133. func_param_types[1] = I32_TYPE; /* func_idx */
  134. func_param_types[2] = I32_TYPE; /* argc */
  135. func_param_types[3] = INT32_PTR_TYPE; /* argv */
  136. if (!(func_type =
  137. 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)llvm_jit_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 if (comp_ctx->is_indirect_mode) {
  155. int32 func_index;
  156. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  157. aot_set_last_error("create LLVM function type failed.");
  158. return false;
  159. }
  160. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  161. if (func_index < 0) {
  162. return false;
  163. }
  164. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  165. func_ptr_type, func_index))) {
  166. return false;
  167. }
  168. }
  169. else {
  170. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  171. && !(func =
  172. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  173. aot_set_last_error("add LLVM function failed.");
  174. return false;
  175. }
  176. }
  177. if (param_cell_num > 64) {
  178. aot_set_last_error("prepare native arguments failed: "
  179. "maximum 64 parameter cell number supported.");
  180. return false;
  181. }
  182. /* prepare frame_lp */
  183. for (i = 0; i < param_count; i++) {
  184. if (!(elem_idx = I32_CONST(cell_num))
  185. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  186. aot_set_last_error("llvm add const or pointer type failed.");
  187. return false;
  188. }
  189. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  190. if (!(elem_ptr =
  191. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  192. func_ctx->argv_buf, &elem_idx, 1, buf))
  193. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  194. elem_ptr_type, buf))) {
  195. aot_set_last_error("llvm build bit cast failed.");
  196. return false;
  197. }
  198. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  199. elem_ptr))) {
  200. aot_set_last_error("llvm build store failed.");
  201. return false;
  202. }
  203. LLVMSetAlignment(res, 1);
  204. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  205. }
  206. func_param_values[0] = func_ctx->exec_env;
  207. func_param_values[1] = func_idx;
  208. func_param_values[2] = I32_CONST(param_cell_num);
  209. func_param_values[3] = func_ctx->argv_buf;
  210. if (!func_param_values[2]) {
  211. aot_set_last_error("llvm create const failed.");
  212. return false;
  213. }
  214. /* call aot_invoke_native() function */
  215. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  216. func_param_values, 4, "res"))) {
  217. aot_set_last_error("llvm build call failed.");
  218. return false;
  219. }
  220. /* get function return value */
  221. if (wasm_ret_type != VALUE_TYPE_VOID) {
  222. if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  223. aot_set_last_error("llvm add pointer type failed.");
  224. return false;
  225. }
  226. if (!(value_ret =
  227. LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
  228. ret_ptr_type, "argv_ret"))) {
  229. aot_set_last_error("llvm build bit cast failed.");
  230. return false;
  231. }
  232. if (!(*p_value_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  233. value_ret, "value_ret"))) {
  234. aot_set_last_error("llvm build load failed.");
  235. return false;
  236. }
  237. }
  238. *p_res = res;
  239. return true;
  240. }
  241. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  242. static bool
  243. call_aot_alloc_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  244. LLVMValueRef func_idx)
  245. {
  246. LLVMValueRef param_values[2], ret_value, value, func;
  247. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  248. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  249. LLVMBasicBlockRef frame_alloc_fail, frame_alloc_success;
  250. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  251. param_types[0] = comp_ctx->exec_env_type;
  252. param_types[1] = I32_TYPE;
  253. ret_type = INT8_TYPE;
  254. if (comp_ctx->is_jit_mode)
  255. GET_AOT_FUNCTION(llvm_jit_alloc_frame, 2);
  256. else
  257. GET_AOT_FUNCTION(aot_alloc_frame, 2);
  258. param_values[0] = func_ctx->exec_env;
  259. param_values[1] = func_idx;
  260. if (!(ret_value =
  261. LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  262. 2, "call_aot_alloc_frame"))) {
  263. aot_set_last_error("llvm build call failed.");
  264. return false;
  265. }
  266. if (!(ret_value = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, ret_value,
  267. I8_ZERO, "frame_alloc_ret"))) {
  268. aot_set_last_error("llvm build icmp failed.");
  269. return false;
  270. }
  271. ADD_BASIC_BLOCK(frame_alloc_fail, "frame_alloc_fail");
  272. ADD_BASIC_BLOCK(frame_alloc_success, "frame_alloc_success");
  273. LLVMMoveBasicBlockAfter(frame_alloc_fail, block_curr);
  274. LLVMMoveBasicBlockAfter(frame_alloc_success, block_curr);
  275. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, frame_alloc_success,
  276. frame_alloc_fail)) {
  277. aot_set_last_error("llvm build cond br failed.");
  278. return false;
  279. }
  280. /* If frame alloc failed, return this function
  281. so the runtime can catch the exception */
  282. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_fail);
  283. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  284. return false;
  285. }
  286. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_success);
  287. return true;
  288. fail:
  289. return false;
  290. }
  291. static bool
  292. call_aot_free_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  293. {
  294. LLVMValueRef param_values[1], ret_value, value, func;
  295. LLVMTypeRef param_types[1], ret_type, func_type, func_ptr_type;
  296. param_types[0] = comp_ctx->exec_env_type;
  297. ret_type = INT8_TYPE;
  298. if (comp_ctx->is_jit_mode)
  299. GET_AOT_FUNCTION(llvm_jit_free_frame, 1);
  300. else
  301. GET_AOT_FUNCTION(aot_free_frame, 1);
  302. param_values[0] = func_ctx->exec_env;
  303. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  304. param_values, 1, "call_aot_free_frame"))) {
  305. aot_set_last_error("llvm build call failed.");
  306. return false;
  307. }
  308. return true;
  309. fail:
  310. return false;
  311. }
  312. #endif /* end of (WASM_ENABLE_DUMP_CALL_STACK != 0) \
  313. || (WASM_ENABLE_PERF_PROFILING != 0) */
  314. static bool
  315. record_stack_usage(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  316. uint32 callee_cell_num)
  317. {
  318. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  319. LLVMBasicBlockRef block_update;
  320. LLVMBasicBlockRef block_after_update;
  321. LLVMValueRef callee_local_size, new_sp, cmp;
  322. LLVMValueRef native_stack_top_min;
  323. LLVMTypeRef ptrdiff_type;
  324. if (comp_ctx->pointer_size == sizeof(uint64_t)) {
  325. ptrdiff_type = I64_TYPE;
  326. }
  327. else {
  328. ptrdiff_type = I32_TYPE;
  329. }
  330. /*
  331. * new_sp = last_alloca - callee_local_size;
  332. * if (*native_stack_top_min_addr > new_sp) {
  333. * *native_stack_top_min_addr = new_sp;
  334. * }
  335. */
  336. if (!(callee_local_size = LLVMConstInt(
  337. ptrdiff_type, -(int64_t)callee_cell_num * 4, true))) {
  338. aot_set_last_error("llvm build const failed.");
  339. return false;
  340. }
  341. if (!(new_sp = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  342. func_ctx->last_alloca,
  343. &callee_local_size, 1, "new_sp"))) {
  344. aot_set_last_error("llvm build gep failed");
  345. return false;
  346. }
  347. if (!(native_stack_top_min = LLVMBuildLoad2(
  348. comp_ctx->builder, OPQ_PTR_TYPE,
  349. func_ctx->native_stack_top_min_addr, "native_stack_top_min"))) {
  350. aot_set_last_error("llvm build load failed");
  351. return false;
  352. }
  353. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT, new_sp,
  354. native_stack_top_min, "cmp"))) {
  355. aot_set_last_error("llvm build icmp failed.");
  356. return false;
  357. }
  358. if (!(block_update = LLVMAppendBasicBlockInContext(
  359. comp_ctx->context, func_ctx->func, "block_update"))) {
  360. aot_set_last_error("llvm add basic block failed.");
  361. return false;
  362. }
  363. if (!(block_after_update = LLVMAppendBasicBlockInContext(
  364. comp_ctx->context, func_ctx->func, "block_after_update"))) {
  365. aot_set_last_error("llvm add basic block failed.");
  366. return false;
  367. }
  368. LLVMMoveBasicBlockAfter(block_update, block_curr);
  369. LLVMMoveBasicBlockAfter(block_after_update, block_update);
  370. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, block_update,
  371. block_after_update)) {
  372. aot_set_last_error("llvm build cond br failed.");
  373. return false;
  374. }
  375. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_update);
  376. if (!LLVMBuildStore(comp_ctx->builder, new_sp,
  377. func_ctx->native_stack_top_min_addr)) {
  378. aot_set_last_error("llvm build store failed");
  379. return false;
  380. }
  381. if (!LLVMBuildBr(comp_ctx->builder, block_after_update)) {
  382. aot_set_last_error("llvm build br failed.");
  383. return false;
  384. }
  385. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_after_update);
  386. return true;
  387. }
  388. static bool
  389. check_stack_boundary(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  390. uint32 callee_cell_num)
  391. {
  392. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  393. LLVMBasicBlockRef check_stack;
  394. LLVMValueRef callee_local_size, stack_bound, cmp;
  395. if (!(callee_local_size = I32_CONST(callee_cell_num * 4))) {
  396. aot_set_last_error("llvm build const failed.");
  397. return false;
  398. }
  399. if (!(stack_bound = LLVMBuildInBoundsGEP2(
  400. comp_ctx->builder, INT8_TYPE, func_ctx->native_stack_bound,
  401. &callee_local_size, 1, "stack_bound"))) {
  402. aot_set_last_error("llvm build inbound gep failed.");
  403. return false;
  404. }
  405. if (!(check_stack = LLVMAppendBasicBlockInContext(
  406. comp_ctx->context, func_ctx->func, "check_stack"))) {
  407. aot_set_last_error("llvm add basic block failed.");
  408. return false;
  409. }
  410. LLVMMoveBasicBlockAfter(check_stack, block_curr);
  411. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT,
  412. func_ctx->last_alloca, stack_bound, "cmp"))) {
  413. aot_set_last_error("llvm build icmp failed.");
  414. return false;
  415. }
  416. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NATIVE_STACK_OVERFLOW,
  417. true, cmp, check_stack)) {
  418. return false;
  419. }
  420. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_stack);
  421. return true;
  422. }
  423. static bool
  424. check_stack(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  425. uint32 callee_cell_num)
  426. {
  427. if (comp_ctx->enable_stack_estimation
  428. && !record_stack_usage(comp_ctx, func_ctx, callee_cell_num))
  429. return false;
  430. if (comp_ctx->enable_stack_bound_check
  431. && !check_stack_boundary(comp_ctx, func_ctx, callee_cell_num))
  432. return false;
  433. return true;
  434. }
  435. /**
  436. * Check whether the app address and its buffer are inside the linear memory,
  437. * if no, throw exception
  438. */
  439. static bool
  440. check_app_addr_and_convert(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  441. bool is_str_arg, LLVMValueRef app_addr,
  442. LLVMValueRef buf_size,
  443. LLVMValueRef *p_native_addr_converted)
  444. {
  445. LLVMTypeRef func_type, func_ptr_type, func_param_types[5];
  446. LLVMValueRef func, func_param_values[5], res, native_addr_ptr;
  447. char *func_name = "aot_check_app_addr_and_convert";
  448. /* prepare function type of aot_check_app_addr_and_convert */
  449. func_param_types[0] = comp_ctx->aot_inst_type; /* module_inst */
  450. func_param_types[1] = INT8_TYPE; /* is_str_arg */
  451. func_param_types[2] = I32_TYPE; /* app_offset */
  452. func_param_types[3] = I32_TYPE; /* buf_size */
  453. func_param_types[4] =
  454. comp_ctx->basic_types.int8_pptr_type; /* p_native_addr */
  455. if (!(func_type =
  456. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  457. aot_set_last_error("llvm add function type failed.");
  458. return false;
  459. }
  460. /* prepare function pointer */
  461. if (comp_ctx->is_jit_mode) {
  462. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  463. aot_set_last_error("create LLVM function type failed.");
  464. return false;
  465. }
  466. /* JIT mode, call the function directly */
  467. if (!(func =
  468. I64_CONST((uint64)(uintptr_t)jit_check_app_addr_and_convert))
  469. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  470. aot_set_last_error("create LLVM value failed.");
  471. return false;
  472. }
  473. }
  474. else if (comp_ctx->is_indirect_mode) {
  475. int32 func_index;
  476. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  477. aot_set_last_error("create LLVM function type failed.");
  478. return false;
  479. }
  480. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  481. if (func_index < 0) {
  482. return false;
  483. }
  484. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  485. func_ptr_type, func_index))) {
  486. return false;
  487. }
  488. }
  489. else {
  490. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  491. && !(func =
  492. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  493. aot_set_last_error("add LLVM function failed.");
  494. return false;
  495. }
  496. }
  497. if (!(native_addr_ptr = LLVMBuildBitCast(
  498. comp_ctx->builder, func_ctx->argv_buf,
  499. comp_ctx->basic_types.int8_pptr_type, "p_native_addr"))) {
  500. aot_set_last_error("llvm build bit cast failed.");
  501. return false;
  502. }
  503. func_param_values[0] = func_ctx->aot_inst;
  504. func_param_values[1] = I8_CONST(is_str_arg);
  505. func_param_values[2] = app_addr;
  506. func_param_values[3] = buf_size;
  507. func_param_values[4] = native_addr_ptr;
  508. if (!func_param_values[1]) {
  509. aot_set_last_error("llvm create const failed.");
  510. return false;
  511. }
  512. /* call aot_check_app_addr_and_convert() function */
  513. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  514. func_param_values, 5, "res"))) {
  515. aot_set_last_error("llvm build call failed.");
  516. return false;
  517. }
  518. /* Check whether exception was thrown when executing the function */
  519. if (comp_ctx->enable_bound_check
  520. && !check_call_return(comp_ctx, func_ctx, res)) {
  521. return false;
  522. }
  523. if (!(*p_native_addr_converted =
  524. LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, native_addr_ptr,
  525. "native_addr"))) {
  526. aot_set_last_error("llvm build load failed.");
  527. return false;
  528. }
  529. return true;
  530. }
  531. bool
  532. aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  533. uint32 func_idx, bool tail_call)
  534. {
  535. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  536. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  537. uint32 func_count = comp_ctx->func_ctx_count, param_cell_num = 0;
  538. uint32 ext_ret_cell_num = 0, cell_num = 0;
  539. AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
  540. AOTFuncType *func_type;
  541. AOTFunc *aot_func;
  542. LLVMTypeRef *param_types = NULL, ret_type;
  543. LLVMTypeRef ext_ret_ptr_type;
  544. LLVMValueRef *param_values = NULL, value_ret = NULL, func;
  545. LLVMValueRef import_func_idx, res;
  546. LLVMValueRef ext_ret, ext_ret_ptr, ext_ret_idx;
  547. int32 i, j = 0, param_count, result_count, ext_ret_count;
  548. uint64 total_size;
  549. uint32 callee_cell_num;
  550. uint8 wasm_ret_type;
  551. uint8 *ext_ret_types = NULL;
  552. const char *signature = NULL;
  553. bool ret = false;
  554. char buf[32];
  555. #if WASM_ENABLE_THREAD_MGR != 0
  556. /* Insert suspend check point */
  557. if (comp_ctx->enable_thread_mgr) {
  558. if (!check_suspend_flags(comp_ctx, func_ctx))
  559. return false;
  560. }
  561. #endif
  562. /* Check function index */
  563. if (func_idx >= import_func_count + func_count) {
  564. aot_set_last_error("Function index out of range.");
  565. return false;
  566. }
  567. /* Get function type */
  568. if (func_idx < import_func_count) {
  569. func_type = import_funcs[func_idx].func_type;
  570. signature = import_funcs[func_idx].signature;
  571. }
  572. else {
  573. func_type =
  574. func_ctxes[func_idx - import_func_count]->aot_func->func_type;
  575. }
  576. /* Get param cell number */
  577. param_cell_num = func_type->param_cell_num;
  578. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  579. if (comp_ctx->enable_aux_stack_frame) {
  580. LLVMValueRef func_idx_const;
  581. if (!(func_idx_const = I32_CONST(func_idx))) {
  582. aot_set_last_error("llvm build const failed.");
  583. return false;
  584. }
  585. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx_const))
  586. return false;
  587. }
  588. #endif
  589. /* Allocate memory for parameters.
  590. * Parameters layout:
  591. * - exec env
  592. * - wasm function's parameters
  593. * - extra results'(except the first one) addresses
  594. */
  595. param_count = (int32)func_type->param_count;
  596. result_count = (int32)func_type->result_count;
  597. ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  598. total_size =
  599. sizeof(LLVMValueRef) * (uint64)(param_count + 1 + ext_ret_count);
  600. if (total_size >= UINT32_MAX
  601. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  602. aot_set_last_error("allocate memory failed.");
  603. return false;
  604. }
  605. /* First parameter is exec env */
  606. param_values[j++] = func_ctx->exec_env;
  607. /* Pop parameters from stack */
  608. for (i = param_count - 1; i >= 0; i--)
  609. POP(param_values[i + j], func_type->types[i]);
  610. /* Set parameters for multiple return values, the first return value
  611. is returned by function return value, and the other return values
  612. are returned by function parameters with pointer types */
  613. if (ext_ret_count > 0) {
  614. ext_ret_types = func_type->types + param_count + 1;
  615. ext_ret_cell_num = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  616. if (ext_ret_cell_num > 64) {
  617. aot_set_last_error("prepare extra results's return "
  618. "address arguments failed: "
  619. "maximum 64 parameter cell number supported.");
  620. goto fail;
  621. }
  622. for (i = 0; i < ext_ret_count; i++) {
  623. if (!(ext_ret_idx = I32_CONST(cell_num))
  624. || !(ext_ret_ptr_type =
  625. LLVMPointerType(TO_LLVM_TYPE(ext_ret_types[i]), 0))) {
  626. aot_set_last_error("llvm add const or pointer type failed.");
  627. goto fail;
  628. }
  629. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i);
  630. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(
  631. comp_ctx->builder, I32_TYPE, func_ctx->argv_buf,
  632. &ext_ret_idx, 1, buf))) {
  633. aot_set_last_error("llvm build GEP failed.");
  634. goto fail;
  635. }
  636. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i);
  637. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  638. ext_ret_ptr_type, buf))) {
  639. aot_set_last_error("llvm build bit cast failed.");
  640. goto fail;
  641. }
  642. param_values[param_count + 1 + i] = ext_ret_ptr;
  643. cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
  644. }
  645. }
  646. if (func_idx < import_func_count) {
  647. if (!(import_func_idx = I32_CONST(func_idx))) {
  648. aot_set_last_error("llvm build inbounds gep failed.");
  649. goto fail;
  650. }
  651. /* Initialize parameter types of the LLVM function */
  652. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  653. if (total_size >= UINT32_MAX
  654. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  655. aot_set_last_error("allocate memory failed.");
  656. goto fail;
  657. }
  658. j = 0;
  659. param_types[j++] = comp_ctx->exec_env_type;
  660. for (i = 0; i < param_count; i++, j++) {
  661. param_types[j] = TO_LLVM_TYPE(func_type->types[i]);
  662. /* If the signature can be gotten, e.g. the signature of the builtin
  663. native libraries, just check the app offset and buf size, and
  664. then convert app offset to native addr and call the native func
  665. directly, no need to call aot_invoke_native to call it */
  666. if (signature) {
  667. LLVMValueRef native_addr, native_addr_size;
  668. if (signature[i + 1] == '*' || signature[i + 1] == '$') {
  669. param_types[j] = INT8_PTR_TYPE;
  670. }
  671. if (signature[i + 1] == '*') {
  672. if (signature[i + 2] == '~')
  673. native_addr_size = param_values[i + 2];
  674. else
  675. native_addr_size = I32_ONE;
  676. if (!check_app_addr_and_convert(
  677. comp_ctx, func_ctx, false, param_values[j],
  678. native_addr_size, &native_addr)) {
  679. goto fail;
  680. }
  681. param_values[j] = native_addr;
  682. }
  683. else if (signature[i + 1] == '$') {
  684. native_addr_size = I32_ZERO;
  685. if (!check_app_addr_and_convert(
  686. comp_ctx, func_ctx, true, param_values[j],
  687. native_addr_size, &native_addr)) {
  688. goto fail;
  689. }
  690. param_values[j] = native_addr;
  691. }
  692. }
  693. }
  694. if (func_type->result_count) {
  695. wasm_ret_type = func_type->types[func_type->param_count];
  696. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  697. }
  698. else {
  699. wasm_ret_type = VALUE_TYPE_VOID;
  700. ret_type = VOID_TYPE;
  701. }
  702. if (!signature) {
  703. /* call aot_invoke_native() */
  704. if (!call_aot_invoke_native_func(
  705. comp_ctx, func_ctx, import_func_idx, func_type,
  706. param_types + 1, param_values + 1, param_count,
  707. param_cell_num, ret_type, wasm_ret_type, &value_ret, &res))
  708. goto fail;
  709. /* Check whether there was exception thrown when executing
  710. the function */
  711. if (comp_ctx->enable_bound_check
  712. && !check_call_return(comp_ctx, func_ctx, res))
  713. goto fail;
  714. }
  715. else { /* call native func directly */
  716. LLVMTypeRef native_func_type, func_ptr_type;
  717. LLVMValueRef func_ptr;
  718. if (!(native_func_type = LLVMFunctionType(
  719. ret_type, param_types, param_count + 1, false))) {
  720. aot_set_last_error("llvm add function type failed.");
  721. goto fail;
  722. }
  723. if (!(func_ptr_type = LLVMPointerType(native_func_type, 0))) {
  724. aot_set_last_error("create LLVM function type failed.");
  725. goto fail;
  726. }
  727. /* Load function pointer */
  728. if (!(func_ptr = LLVMBuildInBoundsGEP2(
  729. comp_ctx->builder, OPQ_PTR_TYPE, func_ctx->func_ptrs,
  730. &import_func_idx, 1, "native_func_ptr_tmp"))) {
  731. aot_set_last_error("llvm build inbounds gep failed.");
  732. goto fail;
  733. }
  734. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE,
  735. func_ptr, "native_func_ptr"))) {
  736. aot_set_last_error("llvm build load failed.");
  737. goto fail;
  738. }
  739. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  740. func_ptr_type, "native_func"))) {
  741. aot_set_last_error("llvm bit cast failed.");
  742. goto fail;
  743. }
  744. /* Call the function */
  745. if (!(value_ret = LLVMBuildCall2(
  746. comp_ctx->builder, native_func_type, func, param_values,
  747. (uint32)param_count + 1 + ext_ret_count,
  748. (func_type->result_count > 0 ? "call" : "")))) {
  749. aot_set_last_error("LLVM build call failed.");
  750. goto fail;
  751. }
  752. /* Check whether there was exception thrown when executing
  753. the function */
  754. if (!check_exception_thrown(comp_ctx, func_ctx)) {
  755. goto fail;
  756. }
  757. }
  758. }
  759. else {
  760. #if LLVM_VERSION_MAJOR >= 14
  761. LLVMTypeRef llvm_func_type;
  762. #endif
  763. bool recursive_call =
  764. (func_ctx == func_ctxes[func_idx - import_func_count]) ? true
  765. : false;
  766. if (comp_ctx->is_indirect_mode) {
  767. LLVMTypeRef func_ptr_type;
  768. if (!(func_ptr_type = LLVMPointerType(
  769. func_ctxes[func_idx - import_func_count]->func_type,
  770. 0))) {
  771. aot_set_last_error("construct func ptr type failed.");
  772. goto fail;
  773. }
  774. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->func_ptrs,
  775. func_ptr_type, func_idx))) {
  776. goto fail;
  777. }
  778. }
  779. else {
  780. if (func_ctxes[func_idx - import_func_count] == func_ctx) {
  781. /* recursive call */
  782. func = func_ctx->func;
  783. }
  784. else {
  785. if (!comp_ctx->is_jit_mode) {
  786. func = func_ctxes[func_idx - import_func_count]->func;
  787. }
  788. else {
  789. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  790. func = func_ctxes[func_idx - import_func_count]->func;
  791. #else
  792. /* JIT tier-up, load func ptr from func_ptrs[func_idx] */
  793. LLVMValueRef func_ptr, func_idx_const;
  794. LLVMTypeRef func_ptr_type;
  795. if (!(func_idx_const = I32_CONST(func_idx))) {
  796. aot_set_last_error("llvm build const failed.");
  797. goto fail;
  798. }
  799. if (!(func_ptr = LLVMBuildInBoundsGEP2(
  800. comp_ctx->builder, OPQ_PTR_TYPE,
  801. func_ctx->func_ptrs, &func_idx_const, 1,
  802. "func_ptr_tmp"))) {
  803. aot_set_last_error("llvm build inbounds gep failed.");
  804. goto fail;
  805. }
  806. if (!(func_ptr =
  807. LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE,
  808. func_ptr, "func_ptr"))) {
  809. aot_set_last_error("llvm build load failed.");
  810. goto fail;
  811. }
  812. if (!(func_ptr_type = LLVMPointerType(
  813. func_ctxes[func_idx - import_func_count]
  814. ->func_type,
  815. 0))) {
  816. aot_set_last_error("construct func ptr type failed.");
  817. goto fail;
  818. }
  819. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  820. func_ptr_type,
  821. "indirect_func"))) {
  822. aot_set_last_error("llvm build bit cast failed.");
  823. goto fail;
  824. }
  825. #endif /* end of !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0) */
  826. }
  827. }
  828. }
  829. aot_func = func_ctxes[func_idx - import_func_count]->aot_func;
  830. callee_cell_num =
  831. aot_func->param_cell_num + aot_func->local_cell_num + 1;
  832. if (!check_stack(comp_ctx, func_ctx, callee_cell_num))
  833. goto fail;
  834. #if LLVM_VERSION_MAJOR >= 14
  835. llvm_func_type = func_ctxes[func_idx - import_func_count]->func_type;
  836. #endif
  837. /* Call the function */
  838. if (!(value_ret = LLVMBuildCall2(
  839. comp_ctx->builder, llvm_func_type, func, param_values,
  840. (uint32)param_count + 1 + ext_ret_count,
  841. (func_type->result_count > 0 ? "call" : "")))) {
  842. aot_set_last_error("LLVM build call failed.");
  843. goto fail;
  844. }
  845. /* Set calling convention for the call with the func's calling
  846. convention */
  847. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  848. if (tail_call)
  849. LLVMSetTailCall(value_ret, true);
  850. /* Check whether there was exception thrown when executing
  851. the function */
  852. if (!tail_call && !recursive_call && comp_ctx->enable_bound_check
  853. && !check_exception_thrown(comp_ctx, func_ctx))
  854. goto fail;
  855. }
  856. if (func_type->result_count > 0) {
  857. /* Push the first result to stack */
  858. PUSH(value_ret, func_type->types[func_type->param_count]);
  859. /* Load extra result from its address and push to stack */
  860. for (i = 0; i < ext_ret_count; i++) {
  861. snprintf(buf, sizeof(buf), "func%d_ext_ret%d", func_idx, i);
  862. if (!(ext_ret = LLVMBuildLoad2(
  863. comp_ctx->builder, TO_LLVM_TYPE(ext_ret_types[i]),
  864. param_values[1 + param_count + i], buf))) {
  865. aot_set_last_error("llvm build load failed.");
  866. goto fail;
  867. }
  868. PUSH(ext_ret, ext_ret_types[i]);
  869. }
  870. }
  871. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  872. if (comp_ctx->enable_aux_stack_frame) {
  873. if (!call_aot_free_frame_func(comp_ctx, func_ctx))
  874. goto fail;
  875. }
  876. #endif
  877. #if WASM_ENABLE_THREAD_MGR != 0
  878. /* Insert suspend check point */
  879. if (comp_ctx->enable_thread_mgr) {
  880. if (!check_suspend_flags(comp_ctx, func_ctx))
  881. goto fail;
  882. }
  883. #endif
  884. ret = true;
  885. fail:
  886. if (param_types)
  887. wasm_runtime_free(param_types);
  888. if (param_values)
  889. wasm_runtime_free(param_values);
  890. return ret;
  891. }
  892. static bool
  893. call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  894. AOTFuncType *aot_func_type,
  895. LLVMValueRef func_type_idx, LLVMValueRef table_idx,
  896. LLVMValueRef table_elem_idx,
  897. LLVMTypeRef *param_types,
  898. LLVMValueRef *param_values, uint32 param_count,
  899. uint32 param_cell_num, uint32 result_count,
  900. uint8 *wasm_ret_types, LLVMValueRef *value_rets,
  901. LLVMValueRef *p_res)
  902. {
  903. LLVMTypeRef func_type, func_ptr_type, func_param_types[6];
  904. LLVMTypeRef ret_type, ret_ptr_type, elem_ptr_type;
  905. LLVMValueRef func, ret_idx, ret_ptr, elem_idx, elem_ptr;
  906. LLVMValueRef func_param_values[6], res = NULL;
  907. char buf[32], *func_name = "aot_call_indirect";
  908. uint32 i, cell_num = 0, ret_cell_num, argv_cell_num;
  909. /* prepare function type of aot_call_indirect */
  910. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  911. func_param_types[1] = I32_TYPE; /* table_idx */
  912. func_param_types[2] = I32_TYPE; /* table_elem_idx */
  913. func_param_types[3] = I32_TYPE; /* argc */
  914. func_param_types[4] = INT32_PTR_TYPE; /* argv */
  915. if (!(func_type =
  916. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  917. aot_set_last_error("llvm add function type failed.");
  918. return false;
  919. }
  920. /* prepare function pointer */
  921. if (comp_ctx->is_jit_mode) {
  922. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  923. aot_set_last_error("create LLVM function type failed.");
  924. return false;
  925. }
  926. /* JIT mode, call the function directly */
  927. if (!(func = I64_CONST((uint64)(uintptr_t)llvm_jit_call_indirect))
  928. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  929. aot_set_last_error("create LLVM value failed.");
  930. return false;
  931. }
  932. }
  933. else if (comp_ctx->is_indirect_mode) {
  934. int32 func_index;
  935. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  936. aot_set_last_error("create LLVM function type failed.");
  937. return false;
  938. }
  939. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  940. if (func_index < 0) {
  941. return false;
  942. }
  943. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  944. func_ptr_type, func_index))) {
  945. return false;
  946. }
  947. }
  948. else {
  949. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  950. && !(func =
  951. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  952. aot_set_last_error("add LLVM function failed.");
  953. return false;
  954. }
  955. }
  956. ret_cell_num = wasm_get_cell_num(wasm_ret_types, result_count);
  957. argv_cell_num =
  958. param_cell_num > ret_cell_num ? param_cell_num : ret_cell_num;
  959. if (argv_cell_num > 64) {
  960. aot_set_last_error("prepare native arguments failed: "
  961. "maximum 64 parameter cell number supported.");
  962. return false;
  963. }
  964. /* prepare frame_lp */
  965. for (i = 0; i < param_count; i++) {
  966. if (!(elem_idx = I32_CONST(cell_num))
  967. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  968. aot_set_last_error("llvm add const or pointer type failed.");
  969. return false;
  970. }
  971. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  972. if (!(elem_ptr =
  973. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  974. func_ctx->argv_buf, &elem_idx, 1, buf))
  975. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  976. elem_ptr_type, buf))) {
  977. aot_set_last_error("llvm build bit cast failed.");
  978. return false;
  979. }
  980. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  981. elem_ptr))) {
  982. aot_set_last_error("llvm build store failed.");
  983. return false;
  984. }
  985. LLVMSetAlignment(res, 1);
  986. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  987. }
  988. func_param_values[0] = func_ctx->exec_env;
  989. func_param_values[1] = table_idx;
  990. func_param_values[2] = table_elem_idx;
  991. func_param_values[3] = I32_CONST(param_cell_num);
  992. func_param_values[4] = func_ctx->argv_buf;
  993. if (!func_param_values[3]) {
  994. aot_set_last_error("llvm create const failed.");
  995. return false;
  996. }
  997. /* call aot_call_indirect() function */
  998. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  999. func_param_values, 5, "res"))) {
  1000. aot_set_last_error("llvm build call failed.");
  1001. return false;
  1002. }
  1003. /* get function result values */
  1004. cell_num = 0;
  1005. for (i = 0; i < result_count; i++) {
  1006. ret_type = TO_LLVM_TYPE(wasm_ret_types[i]);
  1007. if (!(ret_idx = I32_CONST(cell_num))
  1008. || !(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  1009. aot_set_last_error("llvm add const or pointer type failed.");
  1010. return false;
  1011. }
  1012. snprintf(buf, sizeof(buf), "argv_ret%d", i);
  1013. if (!(ret_ptr =
  1014. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  1015. func_ctx->argv_buf, &ret_idx, 1, buf))
  1016. || !(ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ret_ptr,
  1017. ret_ptr_type, buf))) {
  1018. aot_set_last_error("llvm build GEP or bit cast failed.");
  1019. return false;
  1020. }
  1021. snprintf(buf, sizeof(buf), "ret%d", i);
  1022. if (!(value_rets[i] =
  1023. LLVMBuildLoad2(comp_ctx->builder, ret_type, ret_ptr, buf))) {
  1024. aot_set_last_error("llvm build load failed.");
  1025. return false;
  1026. }
  1027. cell_num += wasm_value_type_cell_num(wasm_ret_types[i]);
  1028. }
  1029. *p_res = res;
  1030. return true;
  1031. }
  1032. bool
  1033. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1034. uint32 type_idx, uint32 tbl_idx)
  1035. {
  1036. AOTFuncType *func_type;
  1037. LLVMValueRef tbl_idx_value, elem_idx, table_elem, func_idx;
  1038. LLVMValueRef ftype_idx_ptr, ftype_idx, ftype_idx_const;
  1039. LLVMValueRef cmp_elem_idx, cmp_func_idx, cmp_ftype_idx;
  1040. LLVMValueRef func, func_ptr, table_size_const;
  1041. LLVMValueRef ext_ret_offset, ext_ret_ptr, ext_ret, res;
  1042. LLVMValueRef *param_values = NULL, *value_rets = NULL;
  1043. LLVMValueRef *result_phis = NULL, value_ret, import_func_count;
  1044. LLVMTypeRef *param_types = NULL, ret_type;
  1045. LLVMTypeRef llvm_func_type, llvm_func_ptr_type;
  1046. LLVMTypeRef ext_ret_ptr_type;
  1047. LLVMBasicBlockRef check_elem_idx_succ, check_ftype_idx_succ;
  1048. LLVMBasicBlockRef check_func_idx_succ, block_return, block_curr;
  1049. LLVMBasicBlockRef block_call_import, block_call_non_import;
  1050. LLVMValueRef offset;
  1051. uint32 total_param_count, func_param_count, func_result_count;
  1052. uint32 ext_cell_num, param_cell_num, i, j;
  1053. uint8 wasm_ret_type, *wasm_ret_types;
  1054. uint64 total_size;
  1055. char buf[32];
  1056. bool ret = false;
  1057. /* Check function type index */
  1058. if (type_idx >= comp_ctx->comp_data->func_type_count) {
  1059. aot_set_last_error("function type index out of range");
  1060. return false;
  1061. }
  1062. /* Find the equivalent function type whose type index is the smallest:
  1063. the callee function's type index is also converted to the smallest
  1064. one in wasm loader, so we can just check whether the two type indexes
  1065. are equal (the type index of call_indirect opcode and callee func),
  1066. we don't need to check whether the whole function types are equal,
  1067. including param types and result types. */
  1068. type_idx = wasm_get_smallest_type_idx(comp_ctx->comp_data->func_types,
  1069. comp_ctx->comp_data->func_type_count,
  1070. type_idx);
  1071. ftype_idx_const = I32_CONST(type_idx);
  1072. CHECK_LLVM_CONST(ftype_idx_const);
  1073. func_type = comp_ctx->comp_data->func_types[type_idx];
  1074. func_param_count = func_type->param_count;
  1075. func_result_count = func_type->result_count;
  1076. POP_I32(elem_idx);
  1077. /* get the cur size of the table instance */
  1078. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  1079. + offsetof(AOTTableInstance, cur_size)))) {
  1080. HANDLE_FAILURE("LLVMConstInt");
  1081. goto fail;
  1082. }
  1083. if (!(table_size_const = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1084. func_ctx->aot_inst, &offset,
  1085. 1, "cur_size_i8p"))) {
  1086. HANDLE_FAILURE("LLVMBuildGEP");
  1087. goto fail;
  1088. }
  1089. if (!(table_size_const =
  1090. LLVMBuildBitCast(comp_ctx->builder, table_size_const,
  1091. INT32_PTR_TYPE, "cur_siuze_i32p"))) {
  1092. HANDLE_FAILURE("LLVMBuildBitCast");
  1093. goto fail;
  1094. }
  1095. if (!(table_size_const = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE,
  1096. table_size_const, "cur_size"))) {
  1097. HANDLE_FAILURE("LLVMBuildLoad");
  1098. goto fail;
  1099. }
  1100. /* Check if (uint32)elem index >= table size */
  1101. if (!(cmp_elem_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGE, elem_idx,
  1102. table_size_const, "cmp_elem_idx"))) {
  1103. aot_set_last_error("llvm build icmp failed.");
  1104. goto fail;
  1105. }
  1106. /* Throw exception if elem index >= table size */
  1107. if (!(check_elem_idx_succ = LLVMAppendBasicBlockInContext(
  1108. comp_ctx->context, func_ctx->func, "check_elem_idx_succ"))) {
  1109. aot_set_last_error("llvm add basic block failed.");
  1110. goto fail;
  1111. }
  1112. LLVMMoveBasicBlockAfter(check_elem_idx_succ,
  1113. LLVMGetInsertBlock(comp_ctx->builder));
  1114. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNDEFINED_ELEMENT, true,
  1115. cmp_elem_idx, check_elem_idx_succ)))
  1116. goto fail;
  1117. /* load data as i32* */
  1118. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  1119. + offsetof(AOTTableInstance, elems)))) {
  1120. HANDLE_FAILURE("LLVMConstInt");
  1121. goto fail;
  1122. }
  1123. if (!(table_elem = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1124. func_ctx->aot_inst, &offset, 1,
  1125. "table_elem_i8p"))) {
  1126. aot_set_last_error("llvm build add failed.");
  1127. goto fail;
  1128. }
  1129. if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem,
  1130. INT32_PTR_TYPE, "table_elem_i32p"))) {
  1131. HANDLE_FAILURE("LLVMBuildBitCast");
  1132. goto fail;
  1133. }
  1134. /* Load function index */
  1135. if (!(table_elem =
  1136. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE, table_elem,
  1137. &elem_idx, 1, "table_elem"))) {
  1138. HANDLE_FAILURE("LLVMBuildNUWAdd");
  1139. goto fail;
  1140. }
  1141. if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, table_elem,
  1142. "func_idx"))) {
  1143. aot_set_last_error("llvm build load failed.");
  1144. goto fail;
  1145. }
  1146. /* Check if func_idx == -1 */
  1147. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, func_idx,
  1148. I32_NEG_ONE, "cmp_func_idx"))) {
  1149. aot_set_last_error("llvm build icmp failed.");
  1150. goto fail;
  1151. }
  1152. /* Throw exception if func_idx == -1 */
  1153. if (!(check_func_idx_succ = LLVMAppendBasicBlockInContext(
  1154. comp_ctx->context, func_ctx->func, "check_func_idx_succ"))) {
  1155. aot_set_last_error("llvm add basic block failed.");
  1156. goto fail;
  1157. }
  1158. LLVMMoveBasicBlockAfter(check_func_idx_succ,
  1159. LLVMGetInsertBlock(comp_ctx->builder));
  1160. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNINITIALIZED_ELEMENT,
  1161. true, cmp_func_idx, check_func_idx_succ)))
  1162. goto fail;
  1163. /* Load function type index */
  1164. if (!(ftype_idx_ptr = LLVMBuildInBoundsGEP2(
  1165. comp_ctx->builder, I32_TYPE, func_ctx->func_type_indexes,
  1166. &func_idx, 1, "ftype_idx_ptr"))) {
  1167. aot_set_last_error("llvm build inbounds gep failed.");
  1168. goto fail;
  1169. }
  1170. if (!(ftype_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, ftype_idx_ptr,
  1171. "ftype_idx"))) {
  1172. aot_set_last_error("llvm build load failed.");
  1173. goto fail;
  1174. }
  1175. /* Check if function type index not equal */
  1176. if (!(cmp_ftype_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, ftype_idx,
  1177. ftype_idx_const, "cmp_ftype_idx"))) {
  1178. aot_set_last_error("llvm build icmp failed.");
  1179. goto fail;
  1180. }
  1181. /* Throw exception if ftype_idx != ftype_idx_const */
  1182. if (!(check_ftype_idx_succ = LLVMAppendBasicBlockInContext(
  1183. comp_ctx->context, func_ctx->func, "check_ftype_idx_succ"))) {
  1184. aot_set_last_error("llvm add basic block failed.");
  1185. goto fail;
  1186. }
  1187. LLVMMoveBasicBlockAfter(check_ftype_idx_succ,
  1188. LLVMGetInsertBlock(comp_ctx->builder));
  1189. if (!(aot_emit_exception(comp_ctx, func_ctx,
  1190. EXCE_INVALID_FUNCTION_TYPE_INDEX, true,
  1191. cmp_ftype_idx, check_ftype_idx_succ)))
  1192. goto fail;
  1193. /* Initialize parameter types of the LLVM function */
  1194. total_param_count = 1 + func_param_count;
  1195. /* Extra function results' addresses (except the first one) are
  1196. appended to aot function parameters. */
  1197. if (func_result_count > 1)
  1198. total_param_count += func_result_count - 1;
  1199. total_size = sizeof(LLVMTypeRef) * (uint64)total_param_count;
  1200. if (total_size >= UINT32_MAX
  1201. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  1202. aot_set_last_error("allocate memory failed.");
  1203. goto fail;
  1204. }
  1205. /* Prepare param types */
  1206. j = 0;
  1207. param_types[j++] = comp_ctx->exec_env_type;
  1208. for (i = 0; i < func_param_count; i++)
  1209. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  1210. for (i = 1; i < func_result_count; i++, j++) {
  1211. param_types[j] = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1212. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  1213. aot_set_last_error("llvm get pointer type failed.");
  1214. goto fail;
  1215. }
  1216. }
  1217. /* Resolve return type of the LLVM function */
  1218. if (func_result_count) {
  1219. wasm_ret_type = func_type->types[func_param_count];
  1220. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  1221. }
  1222. else {
  1223. wasm_ret_type = VALUE_TYPE_VOID;
  1224. ret_type = VOID_TYPE;
  1225. }
  1226. /* Allocate memory for parameters */
  1227. total_size = sizeof(LLVMValueRef) * (uint64)total_param_count;
  1228. if (total_size >= UINT32_MAX
  1229. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  1230. aot_set_last_error("allocate memory failed.");
  1231. goto fail;
  1232. }
  1233. /* First parameter is exec env */
  1234. j = 0;
  1235. param_values[j++] = func_ctx->exec_env;
  1236. /* Pop parameters from stack */
  1237. for (i = func_param_count - 1; (int32)i >= 0; i--)
  1238. POP(param_values[i + j], func_type->types[i]);
  1239. /* Prepare extra parameters */
  1240. ext_cell_num = 0;
  1241. for (i = 1; i < func_result_count; i++) {
  1242. ext_ret_offset = I32_CONST(ext_cell_num);
  1243. CHECK_LLVM_CONST(ext_ret_offset);
  1244. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i - 1);
  1245. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  1246. func_ctx->argv_buf,
  1247. &ext_ret_offset, 1, buf))) {
  1248. aot_set_last_error("llvm build GEP failed.");
  1249. goto fail;
  1250. }
  1251. ext_ret_ptr_type = param_types[func_param_count + i];
  1252. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i - 1);
  1253. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  1254. ext_ret_ptr_type, buf))) {
  1255. aot_set_last_error("llvm build bit cast failed.");
  1256. goto fail;
  1257. }
  1258. param_values[func_param_count + i] = ext_ret_ptr;
  1259. ext_cell_num +=
  1260. wasm_value_type_cell_num(func_type->types[func_param_count + i]);
  1261. }
  1262. if (ext_cell_num > 64) {
  1263. aot_set_last_error("prepare call-indirect arguments failed: "
  1264. "maximum 64 extra cell number supported.");
  1265. goto fail;
  1266. }
  1267. #if WASM_ENABLE_THREAD_MGR != 0
  1268. /* Insert suspend check point */
  1269. if (comp_ctx->enable_thread_mgr) {
  1270. if (!check_suspend_flags(comp_ctx, func_ctx))
  1271. goto fail;
  1272. }
  1273. #endif
  1274. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1275. if (comp_ctx->enable_aux_stack_frame) {
  1276. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx))
  1277. goto fail;
  1278. }
  1279. #endif
  1280. /* Add basic blocks */
  1281. block_call_import = LLVMAppendBasicBlockInContext(
  1282. comp_ctx->context, func_ctx->func, "call_import");
  1283. block_call_non_import = LLVMAppendBasicBlockInContext(
  1284. comp_ctx->context, func_ctx->func, "call_non_import");
  1285. block_return = LLVMAppendBasicBlockInContext(comp_ctx->context,
  1286. func_ctx->func, "func_return");
  1287. if (!block_call_import || !block_call_non_import || !block_return) {
  1288. aot_set_last_error("llvm add basic block failed.");
  1289. goto fail;
  1290. }
  1291. LLVMMoveBasicBlockAfter(block_call_import,
  1292. LLVMGetInsertBlock(comp_ctx->builder));
  1293. LLVMMoveBasicBlockAfter(block_call_non_import, block_call_import);
  1294. LLVMMoveBasicBlockAfter(block_return, block_call_non_import);
  1295. import_func_count = I32_CONST(comp_ctx->comp_data->import_func_count);
  1296. CHECK_LLVM_CONST(import_func_count);
  1297. /* Check if func_idx < import_func_count */
  1298. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT, func_idx,
  1299. import_func_count, "cmp_func_idx"))) {
  1300. aot_set_last_error("llvm build icmp failed.");
  1301. goto fail;
  1302. }
  1303. /* If func_idx < import_func_count, jump to call import block,
  1304. else jump to call non-import block */
  1305. if (!LLVMBuildCondBr(comp_ctx->builder, cmp_func_idx, block_call_import,
  1306. block_call_non_import)) {
  1307. aot_set_last_error("llvm build cond br failed.");
  1308. goto fail;
  1309. }
  1310. /* Add result phis for return block */
  1311. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  1312. if (func_result_count > 0) {
  1313. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1314. if (total_size >= UINT32_MAX
  1315. || !(result_phis = wasm_runtime_malloc((uint32)total_size))) {
  1316. aot_set_last_error("allocate memory failed.");
  1317. goto fail;
  1318. }
  1319. memset(result_phis, 0, (uint32)total_size);
  1320. for (i = 0; i < func_result_count; i++) {
  1321. LLVMTypeRef tmp_type =
  1322. TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1323. if (!(result_phis[i] =
  1324. LLVMBuildPhi(comp_ctx->builder, tmp_type, "phi"))) {
  1325. aot_set_last_error("llvm build phi failed.");
  1326. goto fail;
  1327. }
  1328. }
  1329. }
  1330. /* Translate call import block */
  1331. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_import);
  1332. /* Allocate memory for result values */
  1333. if (func_result_count > 0) {
  1334. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1335. if (total_size >= UINT32_MAX
  1336. || !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
  1337. aot_set_last_error("allocate memory failed.");
  1338. goto fail;
  1339. }
  1340. memset(value_rets, 0, (uint32)total_size);
  1341. }
  1342. param_cell_num = func_type->param_cell_num;
  1343. wasm_ret_types = func_type->types + func_type->param_count;
  1344. tbl_idx_value = I32_CONST(tbl_idx);
  1345. if (!tbl_idx_value) {
  1346. aot_set_last_error("llvm create const failed.");
  1347. goto fail;
  1348. }
  1349. if (!call_aot_call_indirect_func(
  1350. comp_ctx, func_ctx, func_type, ftype_idx, tbl_idx_value, elem_idx,
  1351. param_types + 1, param_values + 1, func_param_count, param_cell_num,
  1352. func_result_count, wasm_ret_types, value_rets, &res))
  1353. goto fail;
  1354. /* Check whether exception was thrown when executing the function */
  1355. if (comp_ctx->enable_bound_check
  1356. && !check_call_return(comp_ctx, func_ctx, res))
  1357. goto fail;
  1358. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1359. for (i = 0; i < func_result_count; i++) {
  1360. LLVMAddIncoming(result_phis[i], &value_rets[i], &block_curr, 1);
  1361. }
  1362. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1363. aot_set_last_error("llvm build br failed.");
  1364. goto fail;
  1365. }
  1366. /* Translate call non-import block */
  1367. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_non_import);
  1368. if (!check_stack(comp_ctx, func_ctx,
  1369. param_cell_num + ext_cell_num
  1370. + 1
  1371. /* Reserve some local variables */
  1372. + 16))
  1373. goto fail;
  1374. /* Load function pointer */
  1375. if (!(func_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, OPQ_PTR_TYPE,
  1376. func_ctx->func_ptrs, &func_idx, 1,
  1377. "func_ptr_tmp"))) {
  1378. aot_set_last_error("llvm build inbounds gep failed.");
  1379. goto fail;
  1380. }
  1381. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, func_ptr,
  1382. "func_ptr"))) {
  1383. aot_set_last_error("llvm build load failed.");
  1384. goto fail;
  1385. }
  1386. if (!(llvm_func_type =
  1387. LLVMFunctionType(ret_type, param_types, total_param_count, false))
  1388. || !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
  1389. aot_set_last_error("llvm add function type failed.");
  1390. goto fail;
  1391. }
  1392. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  1393. llvm_func_ptr_type, "indirect_func"))) {
  1394. aot_set_last_error("llvm build bit cast failed.");
  1395. goto fail;
  1396. }
  1397. if (!(value_ret = LLVMBuildCall2(comp_ctx->builder, llvm_func_type, func,
  1398. param_values, total_param_count,
  1399. func_result_count > 0 ? "ret" : ""))) {
  1400. aot_set_last_error("llvm build call failed.");
  1401. goto fail;
  1402. }
  1403. /* Check whether exception was thrown when executing the function */
  1404. if (comp_ctx->enable_bound_check
  1405. && !check_exception_thrown(comp_ctx, func_ctx))
  1406. goto fail;
  1407. if (func_result_count > 0) {
  1408. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1409. /* Push the first result to stack */
  1410. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  1411. /* Load extra result from its address and push to stack */
  1412. for (i = 1; i < func_result_count; i++) {
  1413. ret_type = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1414. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  1415. if (!(ext_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  1416. param_values[func_param_count + i],
  1417. buf))) {
  1418. aot_set_last_error("llvm build load failed.");
  1419. goto fail;
  1420. }
  1421. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  1422. }
  1423. }
  1424. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1425. aot_set_last_error("llvm build br failed.");
  1426. goto fail;
  1427. }
  1428. /* Translate function return block */
  1429. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  1430. for (i = 0; i < func_result_count; i++) {
  1431. PUSH(result_phis[i], func_type->types[func_param_count + i]);
  1432. }
  1433. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1434. if (comp_ctx->enable_aux_stack_frame) {
  1435. if (!call_aot_free_frame_func(comp_ctx, func_ctx))
  1436. goto fail;
  1437. }
  1438. #endif
  1439. #if WASM_ENABLE_THREAD_MGR != 0
  1440. /* Insert suspend check point */
  1441. if (comp_ctx->enable_thread_mgr) {
  1442. if (!check_suspend_flags(comp_ctx, func_ctx))
  1443. goto fail;
  1444. }
  1445. #endif
  1446. ret = true;
  1447. fail:
  1448. if (param_values)
  1449. wasm_runtime_free(param_values);
  1450. if (param_types)
  1451. wasm_runtime_free(param_types);
  1452. if (value_rets)
  1453. wasm_runtime_free(value_rets);
  1454. if (result_phis)
  1455. wasm_runtime_free(result_phis);
  1456. return ret;
  1457. }
  1458. bool
  1459. aot_compile_op_ref_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1460. {
  1461. PUSH_I32(REF_NULL);
  1462. return true;
  1463. fail:
  1464. return false;
  1465. }
  1466. bool
  1467. aot_compile_op_ref_is_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1468. {
  1469. LLVMValueRef lhs, res;
  1470. POP_I32(lhs);
  1471. if (!(res = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, lhs, REF_NULL,
  1472. "cmp_w_null"))) {
  1473. HANDLE_FAILURE("LLVMBuildICmp");
  1474. goto fail;
  1475. }
  1476. if (!(res = LLVMBuildZExt(comp_ctx->builder, res, I32_TYPE, "r_i"))) {
  1477. HANDLE_FAILURE("LLVMBuildZExt");
  1478. goto fail;
  1479. }
  1480. PUSH_I32(res);
  1481. return true;
  1482. fail:
  1483. return false;
  1484. }
  1485. bool
  1486. aot_compile_op_ref_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1487. uint32 func_idx)
  1488. {
  1489. LLVMValueRef ref_idx;
  1490. if (!(ref_idx = I32_CONST(func_idx))) {
  1491. HANDLE_FAILURE("LLVMConstInt");
  1492. goto fail;
  1493. }
  1494. PUSH_I32(ref_idx);
  1495. return true;
  1496. fail:
  1497. return false;
  1498. }