aot_emit_function.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  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. #if WASM_ENABLE_GC != 0
  11. #include "aot_emit_gc.h"
  12. #endif
  13. #define ADD_BASIC_BLOCK(block, name) \
  14. do { \
  15. if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
  16. func_ctx->func, name))) { \
  17. aot_set_last_error("llvm add basic block failed."); \
  18. goto fail; \
  19. } \
  20. } while (0)
  21. static bool
  22. is_win_platform(AOTCompContext *comp_ctx)
  23. {
  24. bool ret = false;
  25. char *triple = LLVMGetTargetMachineTriple(comp_ctx->target_machine);
  26. bh_assert(triple);
  27. if (strstr(triple, "win32") || strstr(triple, "win"))
  28. ret = true;
  29. LLVMDisposeMessage(triple);
  30. return ret;
  31. }
  32. static bool
  33. create_func_return_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  34. {
  35. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  36. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  37. /* Create function return block if it isn't created */
  38. if (!func_ctx->func_return_block) {
  39. if (!(func_ctx->func_return_block = LLVMAppendBasicBlockInContext(
  40. comp_ctx->context, func_ctx->func, "func_ret"))) {
  41. aot_set_last_error("llvm add basic block failed.");
  42. return false;
  43. }
  44. /* Create return IR */
  45. LLVMPositionBuilderAtEnd(comp_ctx->builder,
  46. func_ctx->func_return_block);
  47. if (!comp_ctx->enable_bound_check) {
  48. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_ALREADY_THROWN,
  49. false, NULL, NULL)) {
  50. return false;
  51. }
  52. }
  53. else if (!aot_build_zero_function_ret(comp_ctx, func_ctx,
  54. aot_func_type)) {
  55. return false;
  56. }
  57. }
  58. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  59. return true;
  60. }
  61. /* Check whether there was exception thrown, if yes, return directly */
  62. static bool
  63. check_exception_thrown(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  64. {
  65. LLVMBasicBlockRef block_curr, check_exce_succ;
  66. LLVMValueRef value, cmp;
  67. /* Create function return block if it isn't created */
  68. if (!create_func_return_block(comp_ctx, func_ctx))
  69. return false;
  70. /* Load the first byte of aot_module_inst->cur_exception, and check
  71. whether it is '\0'. If yes, no exception was thrown. */
  72. if (!(value = LLVMBuildLoad2(comp_ctx->builder, INT8_TYPE,
  73. func_ctx->cur_exception, "exce_value"))
  74. || !(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, value, I8_ZERO,
  75. "cmp"))) {
  76. aot_set_last_error("llvm build icmp failed.");
  77. return false;
  78. }
  79. /* Add check exection success block */
  80. if (!(check_exce_succ = LLVMAppendBasicBlockInContext(
  81. comp_ctx->context, func_ctx->func, "check_exce_succ"))) {
  82. aot_set_last_error("llvm add basic block failed.");
  83. return false;
  84. }
  85. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  86. LLVMMoveBasicBlockAfter(check_exce_succ, block_curr);
  87. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  88. /* Create condition br */
  89. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_exce_succ,
  90. func_ctx->func_return_block)) {
  91. aot_set_last_error("llvm build cond br failed.");
  92. return false;
  93. }
  94. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_exce_succ);
  95. return true;
  96. }
  97. /* Check whether there was exception thrown, if yes, return directly */
  98. static bool
  99. check_call_return(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  100. LLVMValueRef res)
  101. {
  102. LLVMBasicBlockRef block_curr, check_call_succ;
  103. LLVMValueRef cmp;
  104. /* Create function return block if it isn't created */
  105. if (!create_func_return_block(comp_ctx, func_ctx))
  106. return false;
  107. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, res, I8_ZERO,
  108. "cmp"))) {
  109. aot_set_last_error("llvm build icmp failed.");
  110. return false;
  111. }
  112. /* Add check exection success block */
  113. if (!(check_call_succ = LLVMAppendBasicBlockInContext(
  114. comp_ctx->context, func_ctx->func, "check_call_succ"))) {
  115. aot_set_last_error("llvm add basic block failed.");
  116. return false;
  117. }
  118. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  119. LLVMMoveBasicBlockAfter(check_call_succ, block_curr);
  120. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  121. /* Create condition br */
  122. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_call_succ,
  123. func_ctx->func_return_block)) {
  124. aot_set_last_error("llvm build cond br failed.");
  125. return false;
  126. }
  127. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_call_succ);
  128. return true;
  129. }
  130. static bool
  131. call_aot_invoke_native_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  132. LLVMValueRef func_idx, AOTFuncType *aot_func_type,
  133. LLVMTypeRef *param_types,
  134. LLVMValueRef *param_values, uint32 param_count,
  135. uint32 param_cell_num, LLVMTypeRef ret_type,
  136. uint8 wasm_ret_type, LLVMValueRef *p_value_ret,
  137. LLVMValueRef *p_res)
  138. {
  139. LLVMTypeRef func_type, func_ptr_type, func_param_types[4];
  140. LLVMTypeRef ret_ptr_type, elem_ptr_type;
  141. LLVMValueRef func, elem_idx, elem_ptr;
  142. LLVMValueRef func_param_values[4], value_ret = NULL, res;
  143. char buf[32], *func_name = "aot_invoke_native";
  144. uint32 i, cell_num = 0;
  145. /* prepare function type of aot_invoke_native */
  146. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  147. func_param_types[1] = I32_TYPE; /* func_idx */
  148. func_param_types[2] = I32_TYPE; /* argc */
  149. func_param_types[3] = INT32_PTR_TYPE; /* argv */
  150. if (!(func_type =
  151. LLVMFunctionType(INT8_TYPE, func_param_types, 4, false))) {
  152. aot_set_last_error("llvm add function type failed.");
  153. return false;
  154. }
  155. /* prepare function pointer */
  156. if (comp_ctx->is_jit_mode) {
  157. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  158. aot_set_last_error("create LLVM function type failed.");
  159. return false;
  160. }
  161. /* JIT mode, call the function directly */
  162. if (!(func = I64_CONST((uint64)(uintptr_t)llvm_jit_invoke_native))
  163. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  164. aot_set_last_error("create LLVM value failed.");
  165. return false;
  166. }
  167. }
  168. else if (comp_ctx->is_indirect_mode) {
  169. int32 func_index;
  170. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  171. aot_set_last_error("create LLVM function type failed.");
  172. return false;
  173. }
  174. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  175. if (func_index < 0) {
  176. return false;
  177. }
  178. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  179. func_ptr_type, func_index))) {
  180. return false;
  181. }
  182. }
  183. else {
  184. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  185. && !(func =
  186. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  187. aot_set_last_error("add LLVM function failed.");
  188. return false;
  189. }
  190. }
  191. if (param_cell_num > 64) {
  192. aot_set_last_error("prepare native arguments failed: "
  193. "maximum 64 parameter cell number supported.");
  194. return false;
  195. }
  196. /* prepare frame_lp */
  197. for (i = 0; i < param_count; i++) {
  198. if (!(elem_idx = I32_CONST(cell_num))
  199. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  200. aot_set_last_error("llvm add const or pointer type failed.");
  201. return false;
  202. }
  203. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  204. if (!(elem_ptr =
  205. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  206. func_ctx->argv_buf, &elem_idx, 1, buf))
  207. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  208. elem_ptr_type, buf))) {
  209. aot_set_last_error("llvm build bit cast failed.");
  210. return false;
  211. }
  212. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  213. elem_ptr))) {
  214. aot_set_last_error("llvm build store failed.");
  215. return false;
  216. }
  217. LLVMSetAlignment(res, 1);
  218. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  219. }
  220. func_param_values[0] = func_ctx->exec_env;
  221. func_param_values[1] = func_idx;
  222. func_param_values[2] = I32_CONST(param_cell_num);
  223. func_param_values[3] = func_ctx->argv_buf;
  224. if (!func_param_values[2]) {
  225. aot_set_last_error("llvm create const failed.");
  226. return false;
  227. }
  228. /* call aot_invoke_native() function */
  229. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  230. func_param_values, 4, "res"))) {
  231. aot_set_last_error("llvm build call failed.");
  232. return false;
  233. }
  234. /* get function return value */
  235. if (wasm_ret_type != VALUE_TYPE_VOID) {
  236. if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  237. aot_set_last_error("llvm add pointer type failed.");
  238. return false;
  239. }
  240. if (!(value_ret =
  241. LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
  242. ret_ptr_type, "argv_ret"))) {
  243. aot_set_last_error("llvm build bit cast failed.");
  244. return false;
  245. }
  246. if (!(*p_value_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  247. value_ret, "value_ret"))) {
  248. aot_set_last_error("llvm build load failed.");
  249. return false;
  250. }
  251. }
  252. *p_res = res;
  253. return true;
  254. }
  255. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  256. static bool
  257. call_aot_alloc_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  258. LLVMValueRef func_idx)
  259. {
  260. LLVMValueRef param_values[2], ret_value, value, func;
  261. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  262. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  263. LLVMBasicBlockRef frame_alloc_fail, frame_alloc_success;
  264. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  265. param_types[0] = comp_ctx->exec_env_type;
  266. param_types[1] = I32_TYPE;
  267. ret_type = INT8_TYPE;
  268. if (comp_ctx->is_jit_mode)
  269. GET_AOT_FUNCTION(llvm_jit_alloc_frame, 2);
  270. else
  271. GET_AOT_FUNCTION(aot_alloc_frame, 2);
  272. param_values[0] = func_ctx->exec_env;
  273. param_values[1] = func_idx;
  274. if (!(ret_value =
  275. LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  276. 2, "call_aot_alloc_frame"))) {
  277. aot_set_last_error("llvm build call failed.");
  278. return false;
  279. }
  280. if (!(ret_value = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, ret_value,
  281. I8_ZERO, "frame_alloc_ret"))) {
  282. aot_set_last_error("llvm build icmp failed.");
  283. return false;
  284. }
  285. ADD_BASIC_BLOCK(frame_alloc_fail, "frame_alloc_fail");
  286. ADD_BASIC_BLOCK(frame_alloc_success, "frame_alloc_success");
  287. LLVMMoveBasicBlockAfter(frame_alloc_fail, block_curr);
  288. LLVMMoveBasicBlockAfter(frame_alloc_success, block_curr);
  289. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, frame_alloc_success,
  290. frame_alloc_fail)) {
  291. aot_set_last_error("llvm build cond br failed.");
  292. return false;
  293. }
  294. /* If frame alloc failed, return this function
  295. so the runtime can catch the exception */
  296. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_fail);
  297. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  298. return false;
  299. }
  300. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_success);
  301. return true;
  302. fail:
  303. return false;
  304. }
  305. static bool
  306. call_aot_free_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  307. {
  308. LLVMValueRef param_values[1], ret_value, value, func;
  309. LLVMTypeRef param_types[1], ret_type, func_type, func_ptr_type;
  310. param_types[0] = comp_ctx->exec_env_type;
  311. ret_type = INT8_TYPE;
  312. if (comp_ctx->is_jit_mode)
  313. GET_AOT_FUNCTION(llvm_jit_free_frame, 1);
  314. else
  315. GET_AOT_FUNCTION(aot_free_frame, 1);
  316. param_values[0] = func_ctx->exec_env;
  317. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  318. param_values, 1, "call_aot_free_frame"))) {
  319. aot_set_last_error("llvm build call failed.");
  320. return false;
  321. }
  322. return true;
  323. fail:
  324. return false;
  325. }
  326. #endif /* end of (WASM_ENABLE_DUMP_CALL_STACK != 0) \
  327. || (WASM_ENABLE_PERF_PROFILING != 0) */
  328. /**
  329. * Check whether the app address and its buffer are inside the linear memory,
  330. * if no, throw exception
  331. */
  332. static bool
  333. check_app_addr_and_convert(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  334. bool is_str_arg, LLVMValueRef app_addr,
  335. LLVMValueRef buf_size,
  336. LLVMValueRef *p_native_addr_converted)
  337. {
  338. LLVMTypeRef func_type, func_ptr_type, func_param_types[5];
  339. LLVMValueRef func, func_param_values[5], res, native_addr_ptr;
  340. char *func_name = "aot_check_app_addr_and_convert";
  341. /* prepare function type of aot_check_app_addr_and_convert */
  342. func_param_types[0] = comp_ctx->aot_inst_type; /* module_inst */
  343. func_param_types[1] = INT8_TYPE; /* is_str_arg */
  344. func_param_types[2] = I32_TYPE; /* app_offset */
  345. func_param_types[3] = I32_TYPE; /* buf_size */
  346. func_param_types[4] =
  347. comp_ctx->basic_types.int8_pptr_type; /* p_native_addr */
  348. if (!(func_type =
  349. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  350. aot_set_last_error("llvm add function type failed.");
  351. return false;
  352. }
  353. /* prepare function pointer */
  354. if (comp_ctx->is_jit_mode) {
  355. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  356. aot_set_last_error("create LLVM function type failed.");
  357. return false;
  358. }
  359. /* JIT mode, call the function directly */
  360. if (!(func =
  361. I64_CONST((uint64)(uintptr_t)jit_check_app_addr_and_convert))
  362. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  363. aot_set_last_error("create LLVM value failed.");
  364. return false;
  365. }
  366. }
  367. else if (comp_ctx->is_indirect_mode) {
  368. int32 func_index;
  369. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  370. aot_set_last_error("create LLVM function type failed.");
  371. return false;
  372. }
  373. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  374. if (func_index < 0) {
  375. return false;
  376. }
  377. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  378. func_ptr_type, func_index))) {
  379. return false;
  380. }
  381. }
  382. else {
  383. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  384. && !(func =
  385. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  386. aot_set_last_error("add LLVM function failed.");
  387. return false;
  388. }
  389. }
  390. if (!(native_addr_ptr = LLVMBuildBitCast(
  391. comp_ctx->builder, func_ctx->argv_buf,
  392. comp_ctx->basic_types.int8_pptr_type, "p_native_addr"))) {
  393. aot_set_last_error("llvm build bit cast failed.");
  394. return false;
  395. }
  396. func_param_values[0] = func_ctx->aot_inst;
  397. func_param_values[1] = I8_CONST(is_str_arg);
  398. func_param_values[2] = app_addr;
  399. func_param_values[3] = buf_size;
  400. func_param_values[4] = native_addr_ptr;
  401. if (!func_param_values[1]) {
  402. aot_set_last_error("llvm create const failed.");
  403. return false;
  404. }
  405. /* call aot_check_app_addr_and_convert() function */
  406. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  407. func_param_values, 5, "res"))) {
  408. aot_set_last_error("llvm build call failed.");
  409. return false;
  410. }
  411. /* Check whether exception was thrown when executing the function */
  412. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  413. && !check_call_return(comp_ctx, func_ctx, res)) {
  414. return false;
  415. }
  416. if (!(*p_native_addr_converted =
  417. LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, native_addr_ptr,
  418. "native_addr"))) {
  419. aot_set_last_error("llvm build load failed.");
  420. return false;
  421. }
  422. return true;
  423. }
  424. static void
  425. aot_estimate_and_record_stack_usage_for_function_call(
  426. const AOTCompContext *comp_ctx, AOTFuncContext *caller_func_ctx,
  427. const AOTFuncType *callee_func_type)
  428. {
  429. unsigned int size;
  430. if (!(comp_ctx->enable_stack_bound_check
  431. || comp_ctx->enable_stack_estimation)) {
  432. return;
  433. }
  434. size =
  435. aot_estimate_stack_usage_for_function_call(comp_ctx, callee_func_type);
  436. /*
  437. * only record the max value, assuming that LLVM emits machine code
  438. * which rewinds the stack before making the next call in the
  439. * function.
  440. */
  441. if (caller_func_ctx->stack_consumption_for_func_call < size) {
  442. caller_func_ctx->stack_consumption_for_func_call = size;
  443. }
  444. }
  445. bool
  446. aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  447. uint32 func_idx, bool tail_call, const uint8 *frame_ip)
  448. {
  449. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  450. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  451. uint32 func_count = comp_ctx->func_ctx_count, param_cell_num = 0;
  452. uint32 ext_ret_cell_num = 0, cell_num = 0;
  453. AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
  454. AOTFuncType *func_type;
  455. LLVMTypeRef *param_types = NULL, ret_type;
  456. LLVMTypeRef ext_ret_ptr_type;
  457. LLVMValueRef *param_values = NULL, value_ret = NULL, func;
  458. LLVMValueRef import_func_idx, res;
  459. LLVMValueRef ext_ret, ext_ret_ptr, ext_ret_idx;
  460. int32 i, j = 0, param_count, result_count, ext_ret_count;
  461. uint64 total_size;
  462. uint8 wasm_ret_type;
  463. uint8 *ext_ret_types = NULL;
  464. const char *signature = NULL;
  465. bool ret = false;
  466. char buf[32];
  467. /* Check function index */
  468. if (func_idx >= import_func_count + func_count) {
  469. aot_set_last_error("Function index out of range.");
  470. return false;
  471. }
  472. /* Get function type */
  473. if (func_idx < import_func_count) {
  474. func_type = import_funcs[func_idx].func_type;
  475. signature = import_funcs[func_idx].signature;
  476. }
  477. else {
  478. func_type =
  479. func_ctxes[func_idx - import_func_count]->aot_func->func_type;
  480. }
  481. aot_estimate_and_record_stack_usage_for_function_call(comp_ctx, func_ctx,
  482. func_type);
  483. /* Get param cell number */
  484. param_cell_num = func_type->param_cell_num;
  485. if (comp_ctx->aot_frame) {
  486. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  487. return false;
  488. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame,
  489. comp_ctx->aot_frame->sp - param_cell_num,
  490. frame_ip))
  491. return false;
  492. }
  493. #if WASM_ENABLE_THREAD_MGR != 0
  494. /* Insert suspend check point */
  495. if (comp_ctx->enable_thread_mgr) {
  496. if (!check_suspend_flags(comp_ctx, func_ctx, true))
  497. return false;
  498. }
  499. #endif
  500. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  501. if (comp_ctx->enable_aux_stack_frame) {
  502. LLVMValueRef func_idx_const;
  503. if (!(func_idx_const = I32_CONST(func_idx))) {
  504. aot_set_last_error("llvm build const failed.");
  505. return false;
  506. }
  507. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx_const))
  508. return false;
  509. }
  510. #endif
  511. /* Allocate memory for parameters.
  512. * Parameters layout:
  513. * - exec env
  514. * - wasm function's parameters
  515. * - extra results'(except the first one) addresses
  516. */
  517. param_count = (int32)func_type->param_count;
  518. result_count = (int32)func_type->result_count;
  519. ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  520. total_size =
  521. sizeof(LLVMValueRef) * (uint64)(param_count + 1 + ext_ret_count);
  522. if (total_size >= UINT32_MAX
  523. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  524. aot_set_last_error("allocate memory failed.");
  525. return false;
  526. }
  527. /* First parameter is exec env */
  528. param_values[j++] = func_ctx->exec_env;
  529. /* Pop parameters from stack */
  530. for (i = param_count - 1; i >= 0; i--)
  531. POP(param_values[i + j], func_type->types[i]);
  532. /* Set parameters for multiple return values, the first return value
  533. is returned by function return value, and the other return values
  534. are returned by function parameters with pointer types */
  535. if (ext_ret_count > 0) {
  536. ext_ret_types = func_type->types + param_count + 1;
  537. ext_ret_cell_num = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  538. if (ext_ret_cell_num > 64) {
  539. aot_set_last_error("prepare extra results's return "
  540. "address arguments failed: "
  541. "maximum 64 parameter cell number supported.");
  542. goto fail;
  543. }
  544. for (i = 0; i < ext_ret_count; i++) {
  545. if (!(ext_ret_idx = I32_CONST(cell_num))
  546. || !(ext_ret_ptr_type =
  547. LLVMPointerType(TO_LLVM_TYPE(ext_ret_types[i]), 0))) {
  548. aot_set_last_error("llvm add const or pointer type failed.");
  549. goto fail;
  550. }
  551. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i);
  552. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(
  553. comp_ctx->builder, I32_TYPE, func_ctx->argv_buf,
  554. &ext_ret_idx, 1, buf))) {
  555. aot_set_last_error("llvm build GEP failed.");
  556. goto fail;
  557. }
  558. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i);
  559. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  560. ext_ret_ptr_type, buf))) {
  561. aot_set_last_error("llvm build bit cast failed.");
  562. goto fail;
  563. }
  564. param_values[param_count + 1 + i] = ext_ret_ptr;
  565. cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
  566. }
  567. }
  568. if (func_idx < import_func_count) {
  569. if (!(import_func_idx = I32_CONST(func_idx))) {
  570. aot_set_last_error("llvm build inbounds gep failed.");
  571. goto fail;
  572. }
  573. /* Initialize parameter types of the LLVM function */
  574. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  575. if (total_size >= UINT32_MAX
  576. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  577. aot_set_last_error("allocate memory failed.");
  578. goto fail;
  579. }
  580. j = 0;
  581. param_types[j++] = comp_ctx->exec_env_type;
  582. for (i = 0; i < param_count; i++, j++) {
  583. param_types[j] = TO_LLVM_TYPE(func_type->types[i]);
  584. /* If the signature can be gotten, e.g. the signature of the builtin
  585. native libraries, just check the app offset and buf size, and
  586. then convert app offset to native addr and call the native func
  587. directly, no need to call aot_invoke_native to call it */
  588. if (signature) {
  589. LLVMValueRef native_addr, native_addr_size;
  590. if (signature[i + 1] == '*' || signature[i + 1] == '$') {
  591. param_types[j] = INT8_PTR_TYPE;
  592. }
  593. if (signature[i + 1] == '*') {
  594. if (signature[i + 2] == '~')
  595. native_addr_size = param_values[i + 2];
  596. else
  597. native_addr_size = I32_ONE;
  598. if (!check_app_addr_and_convert(
  599. comp_ctx, func_ctx, false, param_values[j],
  600. native_addr_size, &native_addr)) {
  601. goto fail;
  602. }
  603. param_values[j] = native_addr;
  604. }
  605. else if (signature[i + 1] == '$') {
  606. native_addr_size = I32_ZERO;
  607. if (!check_app_addr_and_convert(
  608. comp_ctx, func_ctx, true, param_values[j],
  609. native_addr_size, &native_addr)) {
  610. goto fail;
  611. }
  612. param_values[j] = native_addr;
  613. }
  614. }
  615. }
  616. if (func_type->result_count) {
  617. wasm_ret_type = func_type->types[func_type->param_count];
  618. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  619. }
  620. else {
  621. wasm_ret_type = VALUE_TYPE_VOID;
  622. ret_type = VOID_TYPE;
  623. }
  624. if (!signature) {
  625. /* call aot_invoke_native() */
  626. if (!call_aot_invoke_native_func(
  627. comp_ctx, func_ctx, import_func_idx, func_type,
  628. param_types + 1, param_values + 1, param_count,
  629. param_cell_num, ret_type, wasm_ret_type, &value_ret, &res))
  630. goto fail;
  631. /* Check whether there was exception thrown when executing
  632. the function */
  633. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  634. && !check_call_return(comp_ctx, func_ctx, res))
  635. goto fail;
  636. }
  637. else { /* call native func directly */
  638. LLVMTypeRef native_func_type, func_ptr_type;
  639. LLVMValueRef func_ptr;
  640. if (!(native_func_type = LLVMFunctionType(
  641. ret_type, param_types, param_count + 1, false))) {
  642. aot_set_last_error("llvm add function type failed.");
  643. goto fail;
  644. }
  645. if (!(func_ptr_type = LLVMPointerType(native_func_type, 0))) {
  646. aot_set_last_error("create LLVM function type failed.");
  647. goto fail;
  648. }
  649. /* Load function pointer */
  650. if (!(func_ptr = LLVMBuildInBoundsGEP2(
  651. comp_ctx->builder, OPQ_PTR_TYPE, func_ctx->func_ptrs,
  652. &import_func_idx, 1, "native_func_ptr_tmp"))) {
  653. aot_set_last_error("llvm build inbounds gep failed.");
  654. goto fail;
  655. }
  656. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE,
  657. func_ptr, "native_func_ptr"))) {
  658. aot_set_last_error("llvm build load failed.");
  659. goto fail;
  660. }
  661. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  662. func_ptr_type, "native_func"))) {
  663. aot_set_last_error("llvm bit cast failed.");
  664. goto fail;
  665. }
  666. /* Call the function */
  667. if (!(value_ret = LLVMBuildCall2(
  668. comp_ctx->builder, native_func_type, func, param_values,
  669. (uint32)param_count + 1 + ext_ret_count,
  670. (func_type->result_count > 0 ? "call" : "")))) {
  671. aot_set_last_error("LLVM build call failed.");
  672. goto fail;
  673. }
  674. /* Check whether there was exception thrown when executing
  675. the function */
  676. if (!check_exception_thrown(comp_ctx, func_ctx)) {
  677. goto fail;
  678. }
  679. }
  680. }
  681. else {
  682. #if LLVM_VERSION_MAJOR >= 14
  683. LLVMTypeRef llvm_func_type;
  684. #endif
  685. if (comp_ctx->is_indirect_mode) {
  686. LLVMTypeRef func_ptr_type;
  687. if (!(func_ptr_type = LLVMPointerType(
  688. func_ctxes[func_idx - import_func_count]->func_type,
  689. 0))) {
  690. aot_set_last_error("construct func ptr type failed.");
  691. goto fail;
  692. }
  693. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->func_ptrs,
  694. func_ptr_type, func_idx))) {
  695. goto fail;
  696. }
  697. }
  698. else {
  699. if (func_ctxes[func_idx - import_func_count] == func_ctx) {
  700. /* recursive call */
  701. func = func_ctx->precheck_func;
  702. }
  703. else {
  704. if (!comp_ctx->is_jit_mode) {
  705. func =
  706. func_ctxes[func_idx - import_func_count]->precheck_func;
  707. }
  708. else {
  709. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  710. func =
  711. func_ctxes[func_idx - import_func_count]->precheck_func;
  712. #else
  713. /* JIT tier-up, load func ptr from func_ptrs[func_idx] */
  714. LLVMValueRef func_ptr, func_idx_const;
  715. LLVMTypeRef func_ptr_type;
  716. if (!(func_idx_const = I32_CONST(func_idx))) {
  717. aot_set_last_error("llvm build const failed.");
  718. goto fail;
  719. }
  720. if (!(func_ptr = LLVMBuildInBoundsGEP2(
  721. comp_ctx->builder, OPQ_PTR_TYPE,
  722. func_ctx->func_ptrs, &func_idx_const, 1,
  723. "func_ptr_tmp"))) {
  724. aot_set_last_error("llvm build inbounds gep failed.");
  725. goto fail;
  726. }
  727. if (!(func_ptr =
  728. LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE,
  729. func_ptr, "func_ptr"))) {
  730. aot_set_last_error("llvm build load failed.");
  731. goto fail;
  732. }
  733. if (!(func_ptr_type = LLVMPointerType(
  734. func_ctxes[func_idx - import_func_count]
  735. ->func_type,
  736. 0))) {
  737. aot_set_last_error("construct func ptr type failed.");
  738. goto fail;
  739. }
  740. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  741. func_ptr_type,
  742. "indirect_func"))) {
  743. aot_set_last_error("llvm build bit cast failed.");
  744. goto fail;
  745. }
  746. #endif /* end of !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0) */
  747. }
  748. }
  749. }
  750. #if LLVM_VERSION_MAJOR >= 14
  751. llvm_func_type = func_ctxes[func_idx - import_func_count]->func_type;
  752. #endif
  753. /* Call the function */
  754. if (!(value_ret = LLVMBuildCall2(
  755. comp_ctx->builder, llvm_func_type, func, param_values,
  756. (uint32)param_count + 1 + ext_ret_count,
  757. (func_type->result_count > 0 ? "call" : "")))) {
  758. aot_set_last_error("LLVM build call failed.");
  759. goto fail;
  760. }
  761. /* Set calling convention for the call with the func's calling
  762. convention */
  763. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  764. if (tail_call)
  765. LLVMSetTailCall(value_ret, true);
  766. /* Check whether there was exception thrown when executing
  767. the function */
  768. if (!tail_call
  769. && (comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  770. && !check_exception_thrown(comp_ctx, func_ctx))
  771. goto fail;
  772. }
  773. if (func_type->result_count > 0) {
  774. /* Push the first result to stack */
  775. PUSH(value_ret, func_type->types[func_type->param_count]);
  776. /* Load extra result from its address and push to stack */
  777. for (i = 0; i < ext_ret_count; i++) {
  778. snprintf(buf, sizeof(buf), "func%d_ext_ret%d", func_idx, i);
  779. if (!(ext_ret = LLVMBuildLoad2(
  780. comp_ctx->builder, TO_LLVM_TYPE(ext_ret_types[i]),
  781. param_values[1 + param_count + i], buf))) {
  782. aot_set_last_error("llvm build load failed.");
  783. goto fail;
  784. }
  785. PUSH(ext_ret, ext_ret_types[i]);
  786. }
  787. }
  788. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  789. if (comp_ctx->enable_aux_stack_frame) {
  790. if (!call_aot_free_frame_func(comp_ctx, func_ctx))
  791. goto fail;
  792. }
  793. #endif
  794. #if WASM_ENABLE_THREAD_MGR != 0
  795. /* Insert suspend check point */
  796. if (comp_ctx->enable_thread_mgr) {
  797. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  798. goto fail;
  799. }
  800. #endif
  801. ret = true;
  802. fail:
  803. if (param_types)
  804. wasm_runtime_free(param_types);
  805. if (param_values)
  806. wasm_runtime_free(param_values);
  807. return ret;
  808. }
  809. static bool
  810. call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  811. AOTFuncType *aot_func_type,
  812. LLVMValueRef func_type_idx, LLVMValueRef table_idx,
  813. LLVMValueRef table_elem_idx,
  814. LLVMTypeRef *param_types,
  815. LLVMValueRef *param_values, uint32 param_count,
  816. uint32 param_cell_num, uint32 result_count,
  817. uint8 *wasm_ret_types, LLVMValueRef *value_rets,
  818. LLVMValueRef *p_res)
  819. {
  820. LLVMTypeRef func_type, func_ptr_type, func_param_types[6];
  821. LLVMTypeRef ret_type, ret_ptr_type, elem_ptr_type;
  822. LLVMValueRef func, ret_idx, ret_ptr, elem_idx, elem_ptr;
  823. LLVMValueRef func_param_values[6], res = NULL;
  824. char buf[32], *func_name = "aot_call_indirect";
  825. uint32 i, cell_num = 0, ret_cell_num, argv_cell_num;
  826. /* prepare function type of aot_call_indirect */
  827. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  828. func_param_types[1] = I32_TYPE; /* table_idx */
  829. func_param_types[2] = I32_TYPE; /* table_elem_idx */
  830. func_param_types[3] = I32_TYPE; /* argc */
  831. func_param_types[4] = INT32_PTR_TYPE; /* argv */
  832. if (!(func_type =
  833. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  834. aot_set_last_error("llvm add function type failed.");
  835. return false;
  836. }
  837. /* prepare function pointer */
  838. if (comp_ctx->is_jit_mode) {
  839. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  840. aot_set_last_error("create LLVM function type failed.");
  841. return false;
  842. }
  843. /* JIT mode, call the function directly */
  844. if (!(func = I64_CONST((uint64)(uintptr_t)llvm_jit_call_indirect))
  845. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  846. aot_set_last_error("create LLVM value failed.");
  847. return false;
  848. }
  849. }
  850. else if (comp_ctx->is_indirect_mode) {
  851. int32 func_index;
  852. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  853. aot_set_last_error("create LLVM function type failed.");
  854. return false;
  855. }
  856. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  857. if (func_index < 0) {
  858. return false;
  859. }
  860. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  861. func_ptr_type, func_index))) {
  862. return false;
  863. }
  864. }
  865. else {
  866. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  867. && !(func =
  868. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  869. aot_set_last_error("add LLVM function failed.");
  870. return false;
  871. }
  872. }
  873. ret_cell_num = wasm_get_cell_num(wasm_ret_types, result_count);
  874. argv_cell_num =
  875. param_cell_num > ret_cell_num ? param_cell_num : ret_cell_num;
  876. if (argv_cell_num > 64) {
  877. aot_set_last_error("prepare native arguments failed: "
  878. "maximum 64 parameter cell number supported.");
  879. return false;
  880. }
  881. /* prepare frame_lp */
  882. for (i = 0; i < param_count; i++) {
  883. if (!(elem_idx = I32_CONST(cell_num))
  884. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  885. aot_set_last_error("llvm add const or pointer type failed.");
  886. return false;
  887. }
  888. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  889. if (!(elem_ptr =
  890. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  891. func_ctx->argv_buf, &elem_idx, 1, buf))
  892. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  893. elem_ptr_type, buf))) {
  894. aot_set_last_error("llvm build bit cast failed.");
  895. return false;
  896. }
  897. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  898. elem_ptr))) {
  899. aot_set_last_error("llvm build store failed.");
  900. return false;
  901. }
  902. LLVMSetAlignment(res, 1);
  903. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  904. }
  905. func_param_values[0] = func_ctx->exec_env;
  906. func_param_values[1] = table_idx;
  907. func_param_values[2] = table_elem_idx;
  908. func_param_values[3] = I32_CONST(param_cell_num);
  909. func_param_values[4] = func_ctx->argv_buf;
  910. if (!func_param_values[3]) {
  911. aot_set_last_error("llvm create const failed.");
  912. return false;
  913. }
  914. /* call aot_call_indirect() function */
  915. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  916. func_param_values, 5, "res"))) {
  917. aot_set_last_error("llvm build call failed.");
  918. return false;
  919. }
  920. /* get function result values */
  921. cell_num = 0;
  922. for (i = 0; i < result_count; i++) {
  923. ret_type = TO_LLVM_TYPE(wasm_ret_types[i]);
  924. if (!(ret_idx = I32_CONST(cell_num))
  925. || !(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  926. aot_set_last_error("llvm add const or pointer type failed.");
  927. return false;
  928. }
  929. snprintf(buf, sizeof(buf), "argv_ret%d", i);
  930. if (!(ret_ptr =
  931. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  932. func_ctx->argv_buf, &ret_idx, 1, buf))
  933. || !(ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ret_ptr,
  934. ret_ptr_type, buf))) {
  935. aot_set_last_error("llvm build GEP or bit cast failed.");
  936. return false;
  937. }
  938. snprintf(buf, sizeof(buf), "ret%d", i);
  939. if (!(value_rets[i] =
  940. LLVMBuildLoad2(comp_ctx->builder, ret_type, ret_ptr, buf))) {
  941. aot_set_last_error("llvm build load failed.");
  942. return false;
  943. }
  944. cell_num += wasm_value_type_cell_num(wasm_ret_types[i]);
  945. }
  946. *p_res = res;
  947. return true;
  948. }
  949. bool
  950. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  951. uint32 type_idx, uint32 tbl_idx,
  952. const uint8 *frame_ip)
  953. {
  954. AOTFuncType *func_type;
  955. LLVMValueRef tbl_idx_value, elem_idx, table_elem, func_idx;
  956. LLVMValueRef ftype_idx_ptr, ftype_idx, ftype_idx_const;
  957. LLVMValueRef cmp_func_obj, cmp_elem_idx, cmp_func_idx, cmp_ftype_idx;
  958. LLVMValueRef func, func_ptr, table_size_const;
  959. LLVMValueRef ext_ret_offset, ext_ret_ptr, ext_ret, res;
  960. LLVMValueRef *param_values = NULL, *value_rets = NULL;
  961. LLVMValueRef *result_phis = NULL, value_ret, import_func_count;
  962. LLVMTypeRef *param_types = NULL, ret_type;
  963. LLVMTypeRef llvm_func_type, llvm_func_ptr_type;
  964. LLVMTypeRef ext_ret_ptr_type;
  965. LLVMBasicBlockRef check_func_obj_succ, check_elem_idx_succ,
  966. check_ftype_idx_succ;
  967. LLVMBasicBlockRef check_func_idx_succ, block_return, block_curr;
  968. LLVMBasicBlockRef block_call_import, block_call_non_import;
  969. LLVMValueRef offset;
  970. uint32 total_param_count, func_param_count, func_result_count;
  971. uint32 ext_cell_num, param_cell_num, i, j;
  972. uint8 wasm_ret_type, *wasm_ret_types;
  973. uint64 total_size;
  974. char buf[32];
  975. bool ret = false;
  976. /* Check function type index */
  977. if (type_idx >= comp_ctx->comp_data->type_count) {
  978. aot_set_last_error("function type index out of range");
  979. return false;
  980. }
  981. /* Find the equivalent function type whose type index is the smallest:
  982. the callee function's type index is also converted to the smallest
  983. one in wasm loader, so we can just check whether the two type indexes
  984. are equal (the type index of call_indirect opcode and callee func),
  985. we don't need to check whether the whole function types are equal,
  986. including param types and result types. */
  987. type_idx =
  988. wasm_get_smallest_type_idx((WASMTypePtr *)comp_ctx->comp_data->types,
  989. comp_ctx->comp_data->type_count, type_idx);
  990. ftype_idx_const = I32_CONST(type_idx);
  991. CHECK_LLVM_CONST(ftype_idx_const);
  992. func_type = (AOTFuncType *)comp_ctx->comp_data->types[type_idx];
  993. aot_estimate_and_record_stack_usage_for_function_call(comp_ctx, func_ctx,
  994. func_type);
  995. func_param_count = func_type->param_count;
  996. func_result_count = func_type->result_count;
  997. if (comp_ctx->aot_frame) {
  998. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  999. return false;
  1000. if (!aot_gen_commit_sp_ip(
  1001. comp_ctx->aot_frame,
  1002. comp_ctx->aot_frame->sp - func_type->param_cell_num, frame_ip))
  1003. return false;
  1004. }
  1005. POP_I32(elem_idx);
  1006. /* get the cur size of the table instance */
  1007. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  1008. + offsetof(AOTTableInstance, cur_size)))) {
  1009. HANDLE_FAILURE("LLVMConstInt");
  1010. goto fail;
  1011. }
  1012. if (!(table_size_const = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1013. func_ctx->aot_inst, &offset,
  1014. 1, "cur_size_i8p"))) {
  1015. HANDLE_FAILURE("LLVMBuildGEP");
  1016. goto fail;
  1017. }
  1018. if (!(table_size_const =
  1019. LLVMBuildBitCast(comp_ctx->builder, table_size_const,
  1020. INT32_PTR_TYPE, "cur_siuze_i32p"))) {
  1021. HANDLE_FAILURE("LLVMBuildBitCast");
  1022. goto fail;
  1023. }
  1024. if (!(table_size_const = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE,
  1025. table_size_const, "cur_size"))) {
  1026. HANDLE_FAILURE("LLVMBuildLoad");
  1027. goto fail;
  1028. }
  1029. /* Check if (uint32)elem index >= table size */
  1030. if (!(cmp_elem_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGE, elem_idx,
  1031. table_size_const, "cmp_elem_idx"))) {
  1032. aot_set_last_error("llvm build icmp failed.");
  1033. goto fail;
  1034. }
  1035. /* Throw exception if elem index >= table size */
  1036. if (!(check_elem_idx_succ = LLVMAppendBasicBlockInContext(
  1037. comp_ctx->context, func_ctx->func, "check_elem_idx_succ"))) {
  1038. aot_set_last_error("llvm add basic block failed.");
  1039. goto fail;
  1040. }
  1041. LLVMMoveBasicBlockAfter(check_elem_idx_succ,
  1042. LLVMGetInsertBlock(comp_ctx->builder));
  1043. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNDEFINED_ELEMENT, true,
  1044. cmp_elem_idx, check_elem_idx_succ)))
  1045. goto fail;
  1046. /* load data as i32* */
  1047. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  1048. + offsetof(AOTTableInstance, elems)))) {
  1049. HANDLE_FAILURE("LLVMConstInt");
  1050. goto fail;
  1051. }
  1052. if (!(table_elem = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1053. func_ctx->aot_inst, &offset, 1,
  1054. "table_elem_i8p"))) {
  1055. aot_set_last_error("llvm build add failed.");
  1056. goto fail;
  1057. }
  1058. /* Load function index */
  1059. if (comp_ctx->enable_gc) {
  1060. /* table elem is func_obj when gc is enabled */
  1061. if (!(table_elem =
  1062. LLVMBuildBitCast(comp_ctx->builder, table_elem,
  1063. GC_REF_PTR_TYPE, "table_elem_ptr"))) {
  1064. HANDLE_FAILURE("LLVMBuildBitCast");
  1065. goto fail;
  1066. }
  1067. if (!(table_elem = LLVMBuildInBoundsGEP2(comp_ctx->builder, GC_REF_TYPE,
  1068. table_elem, &elem_idx, 1,
  1069. "table_elem"))) {
  1070. HANDLE_FAILURE("LLVMBuildNUWAdd");
  1071. goto fail;
  1072. }
  1073. if (!(table_elem = LLVMBuildLoad2(comp_ctx->builder, GC_REF_TYPE,
  1074. table_elem, "func_idx"))) {
  1075. aot_set_last_error("llvm build load failed.");
  1076. goto fail;
  1077. }
  1078. /* Check if func object is NULL */
  1079. if (!(cmp_func_obj = LLVMBuildIsNull(comp_ctx->builder, table_elem,
  1080. "cmp_func_obj"))) {
  1081. aot_set_last_error("llvm build isnull failed.");
  1082. goto fail;
  1083. }
  1084. /* Throw exception if func object is NULL */
  1085. if (!(check_func_obj_succ = LLVMAppendBasicBlockInContext(
  1086. comp_ctx->context, func_ctx->func, "check_func_obj_succ"))) {
  1087. aot_set_last_error("llvm add basic block failed.");
  1088. goto fail;
  1089. }
  1090. LLVMMoveBasicBlockAfter(check_func_obj_succ,
  1091. LLVMGetInsertBlock(comp_ctx->builder));
  1092. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNINITIALIZED_ELEMENT,
  1093. true, cmp_func_obj, check_func_obj_succ)))
  1094. goto fail;
  1095. /* Get the func idx bound of the WASMFuncObject, the offset may be
  1096. * different in 32-bit runtime and 64-bit runtime since WASMObjectHeader
  1097. * is uintptr_t. Use comp_ctx->pointer_size as the
  1098. * offsetof(WASMFuncObject, func_idx_bound)
  1099. */
  1100. if (!(offset = I32_CONST(comp_ctx->pointer_size))) {
  1101. HANDLE_FAILURE("LLVMConstInt");
  1102. goto fail;
  1103. }
  1104. if (!(func_idx = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1105. table_elem, &offset, 1,
  1106. "func_idx_bound_i8p"))) {
  1107. HANDLE_FAILURE("LLVMBuildGEP");
  1108. goto fail;
  1109. }
  1110. if (!(func_idx =
  1111. LLVMBuildBitCast(comp_ctx->builder, func_idx, INT32_PTR_TYPE,
  1112. "func_idx_bound_i32p"))) {
  1113. HANDLE_FAILURE("LLVMBuildBitCast");
  1114. goto fail;
  1115. }
  1116. if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, func_idx,
  1117. "func_idx_bound"))) {
  1118. HANDLE_FAILURE("LLVMBuildLoad");
  1119. goto fail;
  1120. }
  1121. }
  1122. else {
  1123. if (!(table_elem =
  1124. LLVMBuildBitCast(comp_ctx->builder, table_elem,
  1125. INTPTR_PTR_TYPE, "table_elem_ptr"))) {
  1126. HANDLE_FAILURE("LLVMBuildBitCast");
  1127. goto fail;
  1128. }
  1129. if (!(table_elem = LLVMBuildInBoundsGEP2(comp_ctx->builder, INTPTR_TYPE,
  1130. table_elem, &elem_idx, 1,
  1131. "table_elem"))) {
  1132. HANDLE_FAILURE("LLVMBuildNUWAdd");
  1133. goto fail;
  1134. }
  1135. if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, INTPTR_TYPE,
  1136. table_elem, "func_idx"))) {
  1137. aot_set_last_error("llvm build load failed.");
  1138. goto fail;
  1139. }
  1140. if (!(func_idx = LLVMBuildIntCast2(comp_ctx->builder, func_idx,
  1141. I32_TYPE, true, "func_idx_i32"))) {
  1142. aot_set_last_error("llvm build int cast failed.");
  1143. goto fail;
  1144. }
  1145. /* Check if func_idx == -1 */
  1146. if (!(cmp_func_idx =
  1147. 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. }
  1164. /* Load function type index */
  1165. if (!(ftype_idx_ptr = LLVMBuildInBoundsGEP2(
  1166. comp_ctx->builder, I32_TYPE, func_ctx->func_type_indexes,
  1167. &func_idx, 1, "ftype_idx_ptr"))) {
  1168. aot_set_last_error("llvm build inbounds gep failed.");
  1169. goto fail;
  1170. }
  1171. if (!(ftype_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, ftype_idx_ptr,
  1172. "ftype_idx"))) {
  1173. aot_set_last_error("llvm build load failed.");
  1174. goto fail;
  1175. }
  1176. /* Check if function type index not equal */
  1177. if (!(cmp_ftype_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, ftype_idx,
  1178. ftype_idx_const, "cmp_ftype_idx"))) {
  1179. aot_set_last_error("llvm build icmp failed.");
  1180. goto fail;
  1181. }
  1182. /* Throw exception if ftype_idx != ftype_idx_const */
  1183. if (!(check_ftype_idx_succ = LLVMAppendBasicBlockInContext(
  1184. comp_ctx->context, func_ctx->func, "check_ftype_idx_succ"))) {
  1185. aot_set_last_error("llvm add basic block failed.");
  1186. goto fail;
  1187. }
  1188. LLVMMoveBasicBlockAfter(check_ftype_idx_succ,
  1189. LLVMGetInsertBlock(comp_ctx->builder));
  1190. if (!(aot_emit_exception(comp_ctx, func_ctx,
  1191. EXCE_INVALID_FUNCTION_TYPE_INDEX, true,
  1192. cmp_ftype_idx, check_ftype_idx_succ)))
  1193. goto fail;
  1194. /* Initialize parameter types of the LLVM function */
  1195. total_param_count = 1 + func_param_count;
  1196. /* Extra function results' addresses (except the first one) are
  1197. appended to aot function parameters. */
  1198. if (func_result_count > 1)
  1199. total_param_count += func_result_count - 1;
  1200. total_size = sizeof(LLVMTypeRef) * (uint64)total_param_count;
  1201. if (total_size >= UINT32_MAX
  1202. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  1203. aot_set_last_error("allocate memory failed.");
  1204. goto fail;
  1205. }
  1206. /* Prepare param types */
  1207. j = 0;
  1208. param_types[j++] = comp_ctx->exec_env_type;
  1209. for (i = 0; i < func_param_count; i++)
  1210. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  1211. for (i = 1; i < func_result_count; i++, j++) {
  1212. param_types[j] = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1213. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  1214. aot_set_last_error("llvm get pointer type failed.");
  1215. goto fail;
  1216. }
  1217. }
  1218. /* Resolve return type of the LLVM function */
  1219. if (func_result_count) {
  1220. wasm_ret_type = func_type->types[func_param_count];
  1221. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  1222. }
  1223. else {
  1224. wasm_ret_type = VALUE_TYPE_VOID;
  1225. ret_type = VOID_TYPE;
  1226. }
  1227. /* Allocate memory for parameters */
  1228. total_size = sizeof(LLVMValueRef) * (uint64)total_param_count;
  1229. if (total_size >= UINT32_MAX
  1230. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  1231. aot_set_last_error("allocate memory failed.");
  1232. goto fail;
  1233. }
  1234. /* First parameter is exec env */
  1235. j = 0;
  1236. param_values[j++] = func_ctx->exec_env;
  1237. /* Pop parameters from stack */
  1238. for (i = func_param_count - 1; (int32)i >= 0; i--)
  1239. POP(param_values[i + j], func_type->types[i]);
  1240. /* Prepare extra parameters */
  1241. ext_cell_num = 0;
  1242. for (i = 1; i < func_result_count; i++) {
  1243. ext_ret_offset = I32_CONST(ext_cell_num);
  1244. CHECK_LLVM_CONST(ext_ret_offset);
  1245. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i - 1);
  1246. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  1247. func_ctx->argv_buf,
  1248. &ext_ret_offset, 1, buf))) {
  1249. aot_set_last_error("llvm build GEP failed.");
  1250. goto fail;
  1251. }
  1252. ext_ret_ptr_type = param_types[func_param_count + i];
  1253. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i - 1);
  1254. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  1255. ext_ret_ptr_type, buf))) {
  1256. aot_set_last_error("llvm build bit cast failed.");
  1257. goto fail;
  1258. }
  1259. param_values[func_param_count + i] = ext_ret_ptr;
  1260. ext_cell_num +=
  1261. wasm_value_type_cell_num(func_type->types[func_param_count + i]);
  1262. }
  1263. if (ext_cell_num > 64) {
  1264. aot_set_last_error("prepare call-indirect arguments failed: "
  1265. "maximum 64 extra cell number supported.");
  1266. goto fail;
  1267. }
  1268. #if WASM_ENABLE_THREAD_MGR != 0
  1269. /* Insert suspend check point */
  1270. if (comp_ctx->enable_thread_mgr) {
  1271. if (!check_suspend_flags(comp_ctx, func_ctx, true))
  1272. goto fail;
  1273. }
  1274. #endif
  1275. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1276. if (comp_ctx->enable_aux_stack_frame) {
  1277. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx))
  1278. goto fail;
  1279. }
  1280. #endif
  1281. /* Add basic blocks */
  1282. block_call_import = LLVMAppendBasicBlockInContext(
  1283. comp_ctx->context, func_ctx->func, "call_import");
  1284. block_call_non_import = LLVMAppendBasicBlockInContext(
  1285. comp_ctx->context, func_ctx->func, "call_non_import");
  1286. block_return = LLVMAppendBasicBlockInContext(comp_ctx->context,
  1287. func_ctx->func, "func_return");
  1288. if (!block_call_import || !block_call_non_import || !block_return) {
  1289. aot_set_last_error("llvm add basic block failed.");
  1290. goto fail;
  1291. }
  1292. LLVMMoveBasicBlockAfter(block_call_import,
  1293. LLVMGetInsertBlock(comp_ctx->builder));
  1294. LLVMMoveBasicBlockAfter(block_call_non_import, block_call_import);
  1295. LLVMMoveBasicBlockAfter(block_return, block_call_non_import);
  1296. import_func_count = I32_CONST(comp_ctx->comp_data->import_func_count);
  1297. CHECK_LLVM_CONST(import_func_count);
  1298. /* Check if func_idx < import_func_count */
  1299. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT, func_idx,
  1300. import_func_count, "cmp_func_idx"))) {
  1301. aot_set_last_error("llvm build icmp failed.");
  1302. goto fail;
  1303. }
  1304. /* If func_idx < import_func_count, jump to call import block,
  1305. else jump to call non-import block */
  1306. if (!LLVMBuildCondBr(comp_ctx->builder, cmp_func_idx, block_call_import,
  1307. block_call_non_import)) {
  1308. aot_set_last_error("llvm build cond br failed.");
  1309. goto fail;
  1310. }
  1311. /* Add result phis for return block */
  1312. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  1313. if (func_result_count > 0) {
  1314. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1315. if (total_size >= UINT32_MAX
  1316. || !(result_phis = wasm_runtime_malloc((uint32)total_size))) {
  1317. aot_set_last_error("allocate memory failed.");
  1318. goto fail;
  1319. }
  1320. memset(result_phis, 0, (uint32)total_size);
  1321. for (i = 0; i < func_result_count; i++) {
  1322. LLVMTypeRef tmp_type =
  1323. TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1324. if (!(result_phis[i] =
  1325. LLVMBuildPhi(comp_ctx->builder, tmp_type, "phi"))) {
  1326. aot_set_last_error("llvm build phi failed.");
  1327. goto fail;
  1328. }
  1329. }
  1330. }
  1331. /* Translate call import block */
  1332. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_import);
  1333. /* Allocate memory for result values */
  1334. if (func_result_count > 0) {
  1335. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1336. if (total_size >= UINT32_MAX
  1337. || !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
  1338. aot_set_last_error("allocate memory failed.");
  1339. goto fail;
  1340. }
  1341. memset(value_rets, 0, (uint32)total_size);
  1342. }
  1343. param_cell_num = func_type->param_cell_num;
  1344. wasm_ret_types = func_type->types + func_type->param_count;
  1345. tbl_idx_value = I32_CONST(tbl_idx);
  1346. if (!tbl_idx_value) {
  1347. aot_set_last_error("llvm create const failed.");
  1348. goto fail;
  1349. }
  1350. if (!call_aot_call_indirect_func(
  1351. comp_ctx, func_ctx, func_type, ftype_idx, tbl_idx_value, elem_idx,
  1352. param_types + 1, param_values + 1, func_param_count, param_cell_num,
  1353. func_result_count, wasm_ret_types, value_rets, &res))
  1354. goto fail;
  1355. /* Check whether exception was thrown when executing the function */
  1356. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1357. && !check_call_return(comp_ctx, func_ctx, res))
  1358. goto fail;
  1359. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1360. for (i = 0; i < func_result_count; i++) {
  1361. LLVMAddIncoming(result_phis[i], &value_rets[i], &block_curr, 1);
  1362. }
  1363. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1364. aot_set_last_error("llvm build br failed.");
  1365. goto fail;
  1366. }
  1367. /* Translate call non-import block */
  1368. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_non_import);
  1369. /* Load function pointer */
  1370. if (!(func_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, OPQ_PTR_TYPE,
  1371. func_ctx->func_ptrs, &func_idx, 1,
  1372. "func_ptr_tmp"))) {
  1373. aot_set_last_error("llvm build inbounds gep failed.");
  1374. goto fail;
  1375. }
  1376. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, func_ptr,
  1377. "func_ptr"))) {
  1378. aot_set_last_error("llvm build load failed.");
  1379. goto fail;
  1380. }
  1381. if (!(llvm_func_type =
  1382. LLVMFunctionType(ret_type, param_types, total_param_count, false))
  1383. || !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
  1384. aot_set_last_error("llvm add function type failed.");
  1385. goto fail;
  1386. }
  1387. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  1388. llvm_func_ptr_type, "indirect_func"))) {
  1389. aot_set_last_error("llvm build bit cast failed.");
  1390. goto fail;
  1391. }
  1392. if (!(value_ret = LLVMBuildCall2(comp_ctx->builder, llvm_func_type, func,
  1393. param_values, total_param_count,
  1394. func_result_count > 0 ? "ret" : ""))) {
  1395. aot_set_last_error("llvm build call failed.");
  1396. goto fail;
  1397. }
  1398. /* Check whether exception was thrown when executing the function */
  1399. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1400. && !check_exception_thrown(comp_ctx, func_ctx))
  1401. goto fail;
  1402. if (func_result_count > 0) {
  1403. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1404. /* Push the first result to stack */
  1405. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  1406. /* Load extra result from its address and push to stack */
  1407. for (i = 1; i < func_result_count; i++) {
  1408. ret_type = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1409. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  1410. if (!(ext_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  1411. param_values[func_param_count + i],
  1412. buf))) {
  1413. aot_set_last_error("llvm build load failed.");
  1414. goto fail;
  1415. }
  1416. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  1417. }
  1418. }
  1419. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1420. aot_set_last_error("llvm build br failed.");
  1421. goto fail;
  1422. }
  1423. /* Translate function return block */
  1424. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  1425. for (i = 0; i < func_result_count; i++) {
  1426. PUSH(result_phis[i], func_type->types[func_param_count + i]);
  1427. }
  1428. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1429. if (comp_ctx->enable_aux_stack_frame) {
  1430. if (!call_aot_free_frame_func(comp_ctx, func_ctx))
  1431. goto fail;
  1432. }
  1433. #endif
  1434. #if WASM_ENABLE_THREAD_MGR != 0
  1435. /* Insert suspend check point */
  1436. if (comp_ctx->enable_thread_mgr) {
  1437. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  1438. goto fail;
  1439. }
  1440. #endif
  1441. ret = true;
  1442. fail:
  1443. if (param_values)
  1444. wasm_runtime_free(param_values);
  1445. if (param_types)
  1446. wasm_runtime_free(param_types);
  1447. if (value_rets)
  1448. wasm_runtime_free(value_rets);
  1449. if (result_phis)
  1450. wasm_runtime_free(result_phis);
  1451. return ret;
  1452. }
  1453. bool
  1454. aot_compile_op_ref_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1455. {
  1456. if (comp_ctx->enable_gc)
  1457. PUSH_GC_REF(GC_REF_NULL);
  1458. else
  1459. PUSH_I32(REF_NULL);
  1460. return true;
  1461. fail:
  1462. return false;
  1463. }
  1464. bool
  1465. aot_compile_op_ref_is_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1466. {
  1467. LLVMValueRef lhs = NULL, res;
  1468. if (comp_ctx->enable_gc) {
  1469. POP_GC_REF(lhs);
  1470. if (!(res = LLVMBuildIsNull(comp_ctx->builder, lhs, "lhs is null"))) {
  1471. HANDLE_FAILURE("LLVMBuildIsNull");
  1472. goto fail;
  1473. }
  1474. }
  1475. else {
  1476. POP_I32(lhs);
  1477. if (!(res = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, lhs, REF_NULL,
  1478. "cmp_w_null"))) {
  1479. HANDLE_FAILURE("LLVMBuildICmp");
  1480. goto fail;
  1481. }
  1482. }
  1483. if (!(res = LLVMBuildZExt(comp_ctx->builder, res, I32_TYPE, "r_i"))) {
  1484. HANDLE_FAILURE("LLVMBuildZExt");
  1485. goto fail;
  1486. }
  1487. PUSH_I32(res);
  1488. return true;
  1489. fail:
  1490. return false;
  1491. }
  1492. bool
  1493. aot_compile_op_ref_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1494. uint32 func_idx)
  1495. {
  1496. LLVMValueRef ref_idx;
  1497. if (!(ref_idx = I32_CONST(func_idx))) {
  1498. HANDLE_FAILURE("LLVMConstInt");
  1499. goto fail;
  1500. }
  1501. #if WASM_ENABLE_GC != 0
  1502. LLVMValueRef gc_obj;
  1503. if (comp_ctx->enable_gc) {
  1504. if (!aot_call_aot_create_func_obj(comp_ctx, func_ctx, ref_idx,
  1505. &gc_obj)) {
  1506. goto fail;
  1507. }
  1508. PUSH_GC_REF(gc_obj);
  1509. }
  1510. else
  1511. #endif
  1512. {
  1513. PUSH_I32(ref_idx);
  1514. }
  1515. return true;
  1516. fail:
  1517. return false;
  1518. }
  1519. #if WASM_ENABLE_GC != 0
  1520. bool
  1521. aot_compile_op_call_ref(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1522. uint32 type_idx, bool tail_call, const uint8 *frame_ip)
  1523. {
  1524. AOTFuncType *func_type;
  1525. LLVMValueRef func_obj, func_idx;
  1526. LLVMValueRef cmp_func_obj, cmp_func_idx;
  1527. LLVMValueRef func, func_ptr;
  1528. LLVMValueRef ext_ret_offset, ext_ret_ptr, ext_ret, res;
  1529. LLVMValueRef *param_values = NULL;
  1530. LLVMValueRef *result_phis = NULL, value_ret, import_func_count;
  1531. LLVMTypeRef *param_types = NULL, ret_type;
  1532. LLVMTypeRef llvm_func_type, llvm_func_ptr_type;
  1533. LLVMTypeRef ext_ret_ptr_type;
  1534. LLVMBasicBlockRef check_func_obj_succ, block_return, block_curr;
  1535. LLVMBasicBlockRef block_call_import, block_call_non_import;
  1536. LLVMValueRef offset;
  1537. uint32 total_param_count, func_param_count, func_result_count;
  1538. uint32 ext_cell_num, param_cell_num, i, j;
  1539. uint8 wasm_ret_type;
  1540. uint64 total_size;
  1541. char buf[32];
  1542. bool ret = false;
  1543. /* Check function type index */
  1544. bh_assert(type_idx >= comp_ctx->comp_data->type_count);
  1545. func_type = (AOTFuncType *)comp_ctx->comp_data->types[type_idx];
  1546. aot_estimate_and_record_stack_usage_for_function_call(comp_ctx, func_ctx,
  1547. func_type);
  1548. func_param_count = func_type->param_count;
  1549. func_result_count = func_type->result_count;
  1550. param_cell_num = func_type->param_cell_num;
  1551. POP_GC_REF(func_obj);
  1552. /* Check if func object is NULL */
  1553. if (!(cmp_func_obj =
  1554. LLVMBuildIsNull(comp_ctx->builder, func_obj, "cmp_func_obj"))) {
  1555. aot_set_last_error("llvm build isnull failed.");
  1556. goto fail;
  1557. }
  1558. /* Throw exception if func object is NULL */
  1559. if (!(check_func_obj_succ = LLVMAppendBasicBlockInContext(
  1560. comp_ctx->context, func_ctx->func, "check_func_obj_succ"))) {
  1561. aot_set_last_error("llvm add basic block failed.");
  1562. goto fail;
  1563. }
  1564. LLVMMoveBasicBlockAfter(check_func_obj_succ,
  1565. LLVMGetInsertBlock(comp_ctx->builder));
  1566. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_GC_REF, true,
  1567. cmp_func_obj, check_func_obj_succ)))
  1568. goto fail;
  1569. /* Get the func idx bound of the WASMFuncObject, the offset may be
  1570. * different in 32-bit runtime and 64-bit runtime since WASMObjectHeader
  1571. * is uintptr_t. Use comp_ctx->pointer_size as the
  1572. * offsetof(WASMFuncObject, func_idx_bound) */
  1573. if (!(offset = I32_CONST(comp_ctx->pointer_size))) {
  1574. HANDLE_FAILURE("LLVMConstInt");
  1575. goto fail;
  1576. }
  1577. if (!(func_idx =
  1578. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, func_obj,
  1579. &offset, 1, "func_idx_bound_i8p"))) {
  1580. HANDLE_FAILURE("LLVMBuildGEP");
  1581. goto fail;
  1582. }
  1583. if (!(func_idx = LLVMBuildBitCast(comp_ctx->builder, func_idx,
  1584. INT32_PTR_TYPE, "func_idx_bound_i32p"))) {
  1585. HANDLE_FAILURE("LLVMBuildBitCast");
  1586. goto fail;
  1587. }
  1588. if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, func_idx,
  1589. "func_idx_bound"))) {
  1590. HANDLE_FAILURE("LLVMBuildLoad");
  1591. goto fail;
  1592. }
  1593. /* Initialize parameter types of the LLVM function */
  1594. total_param_count = 1 + func_param_count;
  1595. /* Extra function results' addresses (except the first one) are
  1596. appended to aot function parameters. */
  1597. if (func_result_count > 1)
  1598. total_param_count += func_result_count - 1;
  1599. total_size = sizeof(LLVMTypeRef) * (uint64)total_param_count;
  1600. if (total_size >= UINT32_MAX
  1601. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  1602. aot_set_last_error("allocate memory failed.");
  1603. goto fail;
  1604. }
  1605. /* Prepare param types */
  1606. j = 0;
  1607. param_types[j++] = comp_ctx->exec_env_type;
  1608. for (i = 0; i < func_param_count; i++)
  1609. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  1610. for (i = 1; i < func_result_count; i++, j++) {
  1611. param_types[j] = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1612. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  1613. aot_set_last_error("llvm get pointer type failed.");
  1614. goto fail;
  1615. }
  1616. }
  1617. /* Resolve return type of the LLVM function */
  1618. if (func_result_count) {
  1619. wasm_ret_type = func_type->types[func_param_count];
  1620. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  1621. }
  1622. else {
  1623. wasm_ret_type = VALUE_TYPE_VOID;
  1624. ret_type = VOID_TYPE;
  1625. }
  1626. /* Allocate memory for parameters */
  1627. total_size = sizeof(LLVMValueRef) * (uint64)total_param_count;
  1628. if (total_size >= UINT32_MAX
  1629. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  1630. aot_set_last_error("allocate memory failed.");
  1631. goto fail;
  1632. }
  1633. /* First parameter is exec env */
  1634. j = 0;
  1635. param_values[j++] = func_ctx->exec_env;
  1636. /* Pop parameters from stack */
  1637. for (i = func_param_count - 1; (int32)i >= 0; i--)
  1638. POP(param_values[i + j], func_type->types[i]);
  1639. /* Prepare extra parameters */
  1640. ext_cell_num = 0;
  1641. for (i = 1; i < func_result_count; i++) {
  1642. ext_ret_offset = I32_CONST(ext_cell_num);
  1643. CHECK_LLVM_CONST(ext_ret_offset);
  1644. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i - 1);
  1645. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  1646. func_ctx->argv_buf,
  1647. &ext_ret_offset, 1, buf))) {
  1648. aot_set_last_error("llvm build GEP failed.");
  1649. goto fail;
  1650. }
  1651. ext_ret_ptr_type = param_types[func_param_count + i];
  1652. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i - 1);
  1653. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  1654. ext_ret_ptr_type, buf))) {
  1655. aot_set_last_error("llvm build bit cast failed.");
  1656. goto fail;
  1657. }
  1658. param_values[func_param_count + i] = ext_ret_ptr;
  1659. ext_cell_num +=
  1660. wasm_value_type_cell_num(func_type->types[func_param_count + i]);
  1661. }
  1662. if (ext_cell_num > 64) {
  1663. aot_set_last_error("prepare call-indirect arguments failed: "
  1664. "maximum 64 extra cell number supported.");
  1665. goto fail;
  1666. }
  1667. if (comp_ctx->aot_frame) {
  1668. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  1669. goto fail;
  1670. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame,
  1671. comp_ctx->aot_frame->sp - param_cell_num,
  1672. frame_ip))
  1673. goto fail;
  1674. }
  1675. #if WASM_ENABLE_THREAD_MGR != 0
  1676. /* Insert suspend check point */
  1677. if (comp_ctx->enable_thread_mgr) {
  1678. if (!check_suspend_flags(comp_ctx, func_ctx, true))
  1679. goto fail;
  1680. }
  1681. #endif
  1682. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1683. if (comp_ctx->enable_aux_stack_frame) {
  1684. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx))
  1685. goto fail;
  1686. }
  1687. #endif
  1688. /* Add basic blocks */
  1689. block_call_import = LLVMAppendBasicBlockInContext(
  1690. comp_ctx->context, func_ctx->func, "call_import");
  1691. block_call_non_import = LLVMAppendBasicBlockInContext(
  1692. comp_ctx->context, func_ctx->func, "call_non_import");
  1693. block_return = LLVMAppendBasicBlockInContext(comp_ctx->context,
  1694. func_ctx->func, "func_return");
  1695. if (!block_call_import || !block_call_non_import || !block_return) {
  1696. aot_set_last_error("llvm add basic block failed.");
  1697. goto fail;
  1698. }
  1699. LLVMMoveBasicBlockAfter(block_call_import,
  1700. LLVMGetInsertBlock(comp_ctx->builder));
  1701. LLVMMoveBasicBlockAfter(block_call_non_import, block_call_import);
  1702. LLVMMoveBasicBlockAfter(block_return, block_call_non_import);
  1703. import_func_count = I32_CONST(comp_ctx->comp_data->import_func_count);
  1704. CHECK_LLVM_CONST(import_func_count);
  1705. /* Check if func_idx < import_func_count */
  1706. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT, func_idx,
  1707. import_func_count, "cmp_func_idx"))) {
  1708. aot_set_last_error("llvm build icmp failed.");
  1709. goto fail;
  1710. }
  1711. /* If func_idx < import_func_count, jump to call import block,
  1712. else jump to call non-import block */
  1713. if (!LLVMBuildCondBr(comp_ctx->builder, cmp_func_idx, block_call_import,
  1714. block_call_non_import)) {
  1715. aot_set_last_error("llvm build cond br failed.");
  1716. goto fail;
  1717. }
  1718. /* Add result phis for return block */
  1719. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  1720. if (func_result_count > 0) {
  1721. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1722. if (total_size >= UINT32_MAX
  1723. || !(result_phis = wasm_runtime_malloc((uint32)total_size))) {
  1724. aot_set_last_error("allocate memory failed.");
  1725. goto fail;
  1726. }
  1727. memset(result_phis, 0, (uint32)total_size);
  1728. for (i = 0; i < func_result_count; i++) {
  1729. LLVMTypeRef tmp_type =
  1730. TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1731. if (!(result_phis[i] =
  1732. LLVMBuildPhi(comp_ctx->builder, tmp_type, "phi"))) {
  1733. aot_set_last_error("llvm build phi failed.");
  1734. goto fail;
  1735. }
  1736. }
  1737. }
  1738. /* Translate call import block */
  1739. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_import);
  1740. /* Similar to opcode call_indirect, but for opcode ref.func needs to call
  1741. * aot_invoke_native_func instead */
  1742. if (!call_aot_invoke_native_func(comp_ctx, func_ctx, func_idx, func_type,
  1743. param_types + 1, param_values + 1,
  1744. func_param_count, param_cell_num, ret_type,
  1745. wasm_ret_type, &value_ret, &res))
  1746. goto fail;
  1747. /* Check whether exception was thrown when executing the function */
  1748. if (comp_ctx->enable_bound_check
  1749. && !check_call_return(comp_ctx, func_ctx, res))
  1750. goto fail;
  1751. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1752. /* Get function return values, for aot_invoke_native_func, the extra ret
  1753. * values are put into param's array */
  1754. if (func_result_count > 0) {
  1755. /* Push the first result to stack */
  1756. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  1757. /* Load extra result from its address and push to stack */
  1758. for (i = 1; i < func_result_count; i++) {
  1759. ret_type = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1760. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  1761. if (!(ext_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  1762. param_values[func_param_count + i],
  1763. buf))) {
  1764. aot_set_last_error("llvm build load failed.");
  1765. goto fail;
  1766. }
  1767. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  1768. }
  1769. }
  1770. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1771. aot_set_last_error("llvm build br failed.");
  1772. goto fail;
  1773. }
  1774. /* Translate call non-import block */
  1775. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_non_import);
  1776. /* Load function pointer */
  1777. if (!(func_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, OPQ_PTR_TYPE,
  1778. func_ctx->func_ptrs, &func_idx, 1,
  1779. "func_ptr_tmp"))) {
  1780. aot_set_last_error("llvm build inbounds gep failed.");
  1781. goto fail;
  1782. }
  1783. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, func_ptr,
  1784. "func_ptr"))) {
  1785. aot_set_last_error("llvm build load failed.");
  1786. goto fail;
  1787. }
  1788. if (!(llvm_func_type =
  1789. LLVMFunctionType(ret_type, param_types, total_param_count, false))
  1790. || !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
  1791. aot_set_last_error("llvm add function type failed.");
  1792. goto fail;
  1793. }
  1794. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  1795. llvm_func_ptr_type, "indirect_func"))) {
  1796. aot_set_last_error("llvm build bit cast failed.");
  1797. goto fail;
  1798. }
  1799. if (!(value_ret = LLVMBuildCall2(comp_ctx->builder, llvm_func_type, func,
  1800. param_values, total_param_count,
  1801. func_result_count > 0 ? "ret" : ""))) {
  1802. aot_set_last_error("llvm build call failed.");
  1803. goto fail;
  1804. }
  1805. /* Set calling convention for the call with the func's calling
  1806. convention */
  1807. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  1808. if (tail_call)
  1809. LLVMSetTailCall(value_ret, true);
  1810. /* Check whether exception was thrown when executing the function */
  1811. if (!tail_call
  1812. && (comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1813. && !check_exception_thrown(comp_ctx, func_ctx))
  1814. goto fail;
  1815. if (func_result_count > 0) {
  1816. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1817. /* Push the first result to stack */
  1818. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  1819. /* Load extra result from its address and push to stack */
  1820. for (i = 1; i < func_result_count; i++) {
  1821. ret_type = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1822. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  1823. if (!(ext_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  1824. param_values[func_param_count + i],
  1825. buf))) {
  1826. aot_set_last_error("llvm build load failed.");
  1827. goto fail;
  1828. }
  1829. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  1830. }
  1831. }
  1832. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1833. aot_set_last_error("llvm build br failed.");
  1834. goto fail;
  1835. }
  1836. /* Translate function return block */
  1837. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  1838. for (i = 0; i < func_result_count; i++) {
  1839. PUSH(result_phis[i], func_type->types[func_param_count + i]);
  1840. }
  1841. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1842. if (comp_ctx->enable_aux_stack_frame) {
  1843. if (!call_aot_free_frame_func(comp_ctx, func_ctx))
  1844. goto fail;
  1845. }
  1846. #endif
  1847. #if WASM_ENABLE_THREAD_MGR != 0
  1848. /* Insert suspend check point */
  1849. if (comp_ctx->enable_thread_mgr) {
  1850. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  1851. goto fail;
  1852. }
  1853. #endif
  1854. ret = true;
  1855. fail:
  1856. if (param_values)
  1857. wasm_runtime_free(param_values);
  1858. if (param_types)
  1859. wasm_runtime_free(param_types);
  1860. if (result_phis)
  1861. wasm_runtime_free(result_phis);
  1862. return ret;
  1863. }
  1864. #endif /* end of WASM_ENABLE_GC != 0 */