aot_emit_function.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256
  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_stack_frame_comp.h"
  10. #include "../aot/aot_runtime.h"
  11. #if WASM_ENABLE_GC != 0
  12. #include "aot_emit_gc.h"
  13. #endif
  14. #define ADD_BASIC_BLOCK(block, name) \
  15. do { \
  16. if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
  17. func_ctx->func, name))) { \
  18. aot_set_last_error("llvm add basic block failed."); \
  19. goto fail; \
  20. } \
  21. } while (0)
  22. static bool
  23. is_win_platform(AOTCompContext *comp_ctx)
  24. {
  25. char *triple = LLVMGetTargetMachineTriple(comp_ctx->target_machine);
  26. bool ret;
  27. bh_assert(triple);
  28. ret = (strstr(triple, "win32") || strstr(triple, "win")) ? true : false;
  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 exception 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 exception 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_internal(aot_func_type->types[i],
  219. comp_ctx->pointer_size);
  220. }
  221. func_param_values[0] = func_ctx->exec_env;
  222. func_param_values[1] = func_idx;
  223. func_param_values[2] = I32_CONST(param_cell_num);
  224. func_param_values[3] = func_ctx->argv_buf;
  225. if (!func_param_values[2]) {
  226. aot_set_last_error("llvm create const failed.");
  227. return false;
  228. }
  229. /* call aot_invoke_native() function */
  230. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  231. func_param_values, 4, "res"))) {
  232. aot_set_last_error("llvm build call failed.");
  233. return false;
  234. }
  235. /* get function return value */
  236. if (wasm_ret_type != VALUE_TYPE_VOID) {
  237. if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  238. aot_set_last_error("llvm add pointer type failed.");
  239. return false;
  240. }
  241. if (!(value_ret =
  242. LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
  243. ret_ptr_type, "argv_ret"))) {
  244. aot_set_last_error("llvm build bit cast failed.");
  245. return false;
  246. }
  247. if (!(*p_value_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  248. value_ret, "value_ret"))) {
  249. aot_set_last_error("llvm build load failed.");
  250. return false;
  251. }
  252. }
  253. *p_res = res;
  254. return true;
  255. }
  256. static bool
  257. call_aot_invoke_c_api_native(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  258. uint32 import_func_idx, AOTFuncType *aot_func_type,
  259. LLVMValueRef *params)
  260. {
  261. LLVMTypeRef int8_ptr_type, param_types[6], ret_type;
  262. LLVMTypeRef value_ptr_type = NULL, value_type = NULL;
  263. LLVMTypeRef func_type, func_ptr_type;
  264. LLVMValueRef param_values[6], res, func, value = NULL, offset;
  265. LLVMValueRef c_api_func_imports, c_api_func_import;
  266. LLVMValueRef c_api_params, c_api_results, value_ret;
  267. LLVMValueRef c_api_param_kind, c_api_param_value;
  268. LLVMValueRef c_api_result_value;
  269. uint32 offset_c_api_func_imports, i;
  270. uint32 offset_param_kind, offset_param_value;
  271. char buf[16];
  272. /* `int8 **` type */
  273. int8_ptr_type = LLVMPointerType(INT8_PTR_TYPE, 0);
  274. if (!int8_ptr_type) {
  275. aot_set_last_error("create llvm pointer type failed");
  276. return false;
  277. }
  278. param_types[0] = INT8_PTR_TYPE; /* module_inst */
  279. param_types[1] = INT8_PTR_TYPE; /* CApiFuncImport *c_api_import */
  280. param_types[2] = INT8_PTR_TYPE; /* wasm_val_t *params */
  281. param_types[3] = I32_TYPE; /* uint32 param_count */
  282. param_types[4] = INT8_PTR_TYPE; /* wasm_val_t *results */
  283. param_types[5] = I32_TYPE; /* uint32 result_count */
  284. ret_type = INT8_TYPE;
  285. GET_AOT_FUNCTION(wasm_runtime_quick_invoke_c_api_native, 6);
  286. param_values[0] = func_ctx->aot_inst;
  287. /* Get module_inst->c_api_func_imports, jit mode WASMModuleInstance is the
  288. * same layout with AOTModuleInstance */
  289. offset_c_api_func_imports = offsetof(AOTModuleInstance, c_api_func_imports);
  290. offset = I32_CONST(offset_c_api_func_imports);
  291. CHECK_LLVM_CONST(offset);
  292. c_api_func_imports =
  293. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, func_ctx->aot_inst,
  294. &offset, 1, "c_api_func_imports_addr");
  295. c_api_func_imports =
  296. LLVMBuildBitCast(comp_ctx->builder, c_api_func_imports, int8_ptr_type,
  297. "c_api_func_imports_ptr");
  298. c_api_func_imports =
  299. LLVMBuildLoad2(comp_ctx->builder, INT8_PTR_TYPE, c_api_func_imports,
  300. "c_api_func_imports");
  301. /* Get &c_api_func_imports[func_idx], note size of CApiFuncImport
  302. is pointer_size * 3 */
  303. offset = I32_CONST((unsigned long long)comp_ctx->pointer_size * 3
  304. * import_func_idx);
  305. CHECK_LLVM_CONST(offset);
  306. c_api_func_import =
  307. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, c_api_func_imports,
  308. &offset, 1, "c_api_func_import");
  309. param_values[1] = c_api_func_import;
  310. param_values[2] = c_api_params = func_ctx->argv_buf;
  311. param_values[3] = I32_CONST(aot_func_type->param_count);
  312. CHECK_LLVM_CONST(param_values[3]);
  313. /* Ensure sizeof(wasm_val_t) is 16 bytes */
  314. offset = I32_CONST(sizeof(wasm_val_t) * aot_func_type->param_count);
  315. c_api_results =
  316. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, func_ctx->argv_buf,
  317. &offset, 1, "results");
  318. param_values[4] = c_api_results;
  319. param_values[5] = I32_CONST(aot_func_type->result_count);
  320. CHECK_LLVM_CONST(param_values[5]);
  321. /* Set each c api param */
  322. for (i = 0; i < aot_func_type->param_count; i++) {
  323. /* Ensure sizeof(wasm_val_t) is 16 bytes */
  324. offset_param_kind = sizeof(wasm_val_t) * i;
  325. offset = I32_CONST(offset_param_kind);
  326. CHECK_LLVM_CONST(offset);
  327. c_api_param_kind =
  328. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, c_api_params,
  329. &offset, 1, "c_api_param_kind_addr");
  330. c_api_param_kind =
  331. LLVMBuildBitCast(comp_ctx->builder, c_api_param_kind, INT8_PTR_TYPE,
  332. "c_api_param_kind_ptr");
  333. switch (aot_func_type->types[i]) {
  334. case VALUE_TYPE_I32:
  335. value = I8_CONST(WASM_I32);
  336. break;
  337. case VALUE_TYPE_F32:
  338. value = I8_CONST(WASM_F32);
  339. break;
  340. case VALUE_TYPE_I64:
  341. value = I8_CONST(WASM_I64);
  342. break;
  343. case VALUE_TYPE_F64:
  344. value = I8_CONST(WASM_F64);
  345. break;
  346. default:
  347. bh_assert(0);
  348. break;
  349. }
  350. CHECK_LLVM_CONST(value);
  351. LLVMBuildStore(comp_ctx->builder, value, c_api_param_kind);
  352. /* Ensure offsetof(wasm_val_t, of) is 8 bytes */
  353. offset_param_value = offset_param_kind + offsetof(wasm_val_t, of);
  354. offset = I32_CONST(offset_param_value);
  355. CHECK_LLVM_CONST(offset);
  356. c_api_param_value =
  357. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, c_api_params,
  358. &offset, 1, "c_api_param_value_addr");
  359. switch (aot_func_type->types[i]) {
  360. case VALUE_TYPE_I32:
  361. value_ptr_type = INT32_PTR_TYPE;
  362. break;
  363. case VALUE_TYPE_F32:
  364. value_ptr_type = F32_PTR_TYPE;
  365. break;
  366. case VALUE_TYPE_I64:
  367. value_ptr_type = INT64_PTR_TYPE;
  368. break;
  369. case VALUE_TYPE_F64:
  370. value_ptr_type = F64_PTR_TYPE;
  371. break;
  372. default:
  373. bh_assert(0);
  374. break;
  375. }
  376. c_api_param_value =
  377. LLVMBuildBitCast(comp_ctx->builder, c_api_param_value,
  378. value_ptr_type, "c_api_param_value_ptr");
  379. LLVMBuildStore(comp_ctx->builder, params[i], c_api_param_value);
  380. }
  381. /* Call the function */
  382. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  383. 6, "call"))) {
  384. aot_set_last_error("LLVM build call failed.");
  385. goto fail;
  386. }
  387. /* Check whether exception was thrown when executing the function */
  388. if (comp_ctx->enable_bound_check
  389. && !check_call_return(comp_ctx, func_ctx, res)) {
  390. goto fail;
  391. }
  392. for (i = 0; i < aot_func_type->result_count; i++) {
  393. /* Ensure sizeof(wasm_val_t) is 16 bytes and
  394. offsetof(wasm_val_t, of) is 8 bytes */
  395. uint32 offset_result_value =
  396. sizeof(wasm_val_t) * i + offsetof(wasm_val_t, of);
  397. offset = I32_CONST(offset_result_value);
  398. CHECK_LLVM_CONST(offset);
  399. c_api_result_value =
  400. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, c_api_results,
  401. &offset, 1, "c_api_result_value_addr");
  402. switch (aot_func_type->types[aot_func_type->param_count + i]) {
  403. case VALUE_TYPE_I32:
  404. value_type = I32_TYPE;
  405. value_ptr_type = INT32_PTR_TYPE;
  406. break;
  407. case VALUE_TYPE_F32:
  408. value_type = F32_TYPE;
  409. value_ptr_type = F32_PTR_TYPE;
  410. break;
  411. case VALUE_TYPE_I64:
  412. value_type = I64_TYPE;
  413. value_ptr_type = INT64_PTR_TYPE;
  414. break;
  415. case VALUE_TYPE_F64:
  416. value_type = F64_TYPE;
  417. value_ptr_type = F64_PTR_TYPE;
  418. break;
  419. default:
  420. bh_assert(0);
  421. break;
  422. }
  423. c_api_result_value =
  424. LLVMBuildBitCast(comp_ctx->builder, c_api_result_value,
  425. value_ptr_type, "c_api_result_value_ptr");
  426. snprintf(buf, sizeof(buf), "%s%u", "ret", i);
  427. value_ret = LLVMBuildLoad2(comp_ctx->builder, value_type,
  428. c_api_result_value, buf);
  429. PUSH(value_ret, aot_func_type->types[aot_func_type->param_count + i]);
  430. }
  431. return true;
  432. fail:
  433. return false;
  434. }
  435. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  436. static bool
  437. call_aot_alloc_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  438. LLVMValueRef func_idx)
  439. {
  440. LLVMValueRef param_values[2], ret_value, value, func;
  441. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  442. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  443. LLVMBasicBlockRef frame_alloc_fail, frame_alloc_success;
  444. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  445. param_types[0] = comp_ctx->exec_env_type;
  446. param_types[1] = I32_TYPE;
  447. ret_type = INT8_TYPE;
  448. #if WASM_ENABLE_JIT != 0
  449. if (comp_ctx->is_jit_mode)
  450. GET_AOT_FUNCTION(llvm_jit_alloc_frame, 2);
  451. else
  452. #endif
  453. GET_AOT_FUNCTION(aot_alloc_frame, 2);
  454. param_values[0] = func_ctx->exec_env;
  455. param_values[1] = func_idx;
  456. if (!(ret_value =
  457. LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  458. 2, "call_aot_alloc_frame"))) {
  459. aot_set_last_error("llvm build call failed.");
  460. return false;
  461. }
  462. if (!(ret_value = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, ret_value,
  463. I8_ZERO, "frame_alloc_ret"))) {
  464. aot_set_last_error("llvm build icmp failed.");
  465. return false;
  466. }
  467. ADD_BASIC_BLOCK(frame_alloc_fail, "frame_alloc_fail");
  468. ADD_BASIC_BLOCK(frame_alloc_success, "frame_alloc_success");
  469. LLVMMoveBasicBlockAfter(frame_alloc_fail, block_curr);
  470. LLVMMoveBasicBlockAfter(frame_alloc_success, block_curr);
  471. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, frame_alloc_success,
  472. frame_alloc_fail)) {
  473. aot_set_last_error("llvm build cond br failed.");
  474. return false;
  475. }
  476. /* If frame alloc failed, return this function
  477. so the runtime can catch the exception */
  478. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_fail);
  479. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  480. return false;
  481. }
  482. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_success);
  483. return true;
  484. fail:
  485. return false;
  486. }
  487. static bool
  488. alloc_frame_for_aot_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  489. uint32 func_idx)
  490. {
  491. LLVMValueRef wasm_stack_top_bound = func_ctx->wasm_stack_top_bound;
  492. LLVMValueRef wasm_stack_top_ptr = func_ctx->wasm_stack_top_ptr,
  493. wasm_stack_top;
  494. LLVMValueRef wasm_stack_top_max, offset, cmp;
  495. LLVMValueRef cur_frame, new_frame, prev_frame_ptr;
  496. LLVMValueRef cur_frame_ptr = func_ctx->cur_frame_ptr;
  497. LLVMValueRef func_idx_ptr, func_idx_val, func_inst_ptr, func_inst;
  498. LLVMTypeRef int8_ptr_type;
  499. LLVMBasicBlockRef check_wasm_stack_succ;
  500. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  501. uint32 param_cell_num = 0, local_cell_num = 0, i;
  502. uint32 max_local_cell_num, max_stack_cell_num;
  503. uint32 all_cell_num, frame_size, frame_size_with_outs_area;
  504. uint32 aot_frame_ptr_num = offsetof(AOTFrame, lp) / sizeof(uintptr_t);
  505. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  506. AOTFuncType *aot_func_type;
  507. AOTFunc *aot_func = NULL;
  508. /* `int8 **` type */
  509. int8_ptr_type = LLVMPointerType(INT8_PTR_TYPE, 0);
  510. if (!int8_ptr_type) {
  511. aot_set_last_error("create llvm pointer type failed");
  512. return false;
  513. }
  514. /* Get param_cell_num, local_cell_num and max_stack_cell_num */
  515. if (func_idx < import_func_count) {
  516. aot_func_type = import_funcs[func_idx].func_type;
  517. for (i = 0; i < aot_func_type->param_count; i++)
  518. param_cell_num += wasm_value_type_cell_num_internal(
  519. aot_func_type->types[i], comp_ctx->pointer_size);
  520. max_local_cell_num = param_cell_num > 2 ? param_cell_num : 2;
  521. max_stack_cell_num = 0;
  522. }
  523. else {
  524. aot_func = comp_ctx->comp_data->funcs[func_idx - import_func_count];
  525. param_cell_num = aot_func->param_cell_num;
  526. local_cell_num = aot_func->local_cell_num;
  527. max_local_cell_num = param_cell_num + local_cell_num;
  528. max_stack_cell_num = aot_func->max_stack_cell_num;
  529. }
  530. all_cell_num = max_local_cell_num + max_stack_cell_num;
  531. /* Get size of the frame to allocate and get size with outs_area to
  532. check whether wasm operand stack is overflow */
  533. if (!comp_ctx->is_jit_mode) {
  534. /* Refer to aot_alloc_frame */
  535. if (!comp_ctx->enable_gc) {
  536. frame_size = frame_size_with_outs_area =
  537. comp_ctx->pointer_size * aot_frame_ptr_num;
  538. }
  539. else {
  540. frame_size = comp_ctx->pointer_size * aot_frame_ptr_num
  541. + align_uint(all_cell_num * 5, 4);
  542. frame_size_with_outs_area =
  543. frame_size + comp_ctx->pointer_size * aot_frame_ptr_num
  544. + max_stack_cell_num * 4;
  545. }
  546. }
  547. else {
  548. /* Refer to wasm_interp_interp_frame_size */
  549. if (!comp_ctx->enable_gc) {
  550. frame_size = frame_size_with_outs_area =
  551. offsetof(WASMInterpFrame, lp);
  552. }
  553. else {
  554. frame_size =
  555. offsetof(WASMInterpFrame, lp) + align_uint(all_cell_num * 5, 4);
  556. frame_size_with_outs_area = frame_size
  557. + offsetof(WASMInterpFrame, lp)
  558. + max_stack_cell_num * 4;
  559. }
  560. }
  561. cur_frame = func_ctx->cur_frame;
  562. if (!comp_ctx->enable_gc) {
  563. offset = I32_CONST(frame_size);
  564. CHECK_LLVM_CONST(offset);
  565. if (!(wasm_stack_top =
  566. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
  567. &offset, 1, "wasm_stack_top"))) {
  568. aot_set_last_error("llvm build in bounds gep failed");
  569. return false;
  570. }
  571. offset = I32_CONST(frame_size * 2);
  572. CHECK_LLVM_CONST(offset);
  573. if (!(wasm_stack_top_max =
  574. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
  575. &offset, 1, "wasm_stack_top_max"))) {
  576. aot_set_last_error("llvm build in bounds gep failed");
  577. return false;
  578. }
  579. }
  580. else {
  581. /* Get exec_env->wasm_stack.top */
  582. if (!(wasm_stack_top =
  583. LLVMBuildLoad2(comp_ctx->builder, INT8_PTR_TYPE,
  584. wasm_stack_top_ptr, "wasm_stack_top"))) {
  585. aot_set_last_error("load wasm_stack.top failed");
  586. return false;
  587. }
  588. /* Check whether wasm operand stack is overflow */
  589. offset = I32_CONST(frame_size_with_outs_area);
  590. CHECK_LLVM_CONST(offset);
  591. if (!(wasm_stack_top_max = LLVMBuildInBoundsGEP2(
  592. comp_ctx->builder, INT8_TYPE, wasm_stack_top, &offset, 1,
  593. "wasm_stack_top_max"))) {
  594. aot_set_last_error("llvm build in bounds gep failed");
  595. return false;
  596. }
  597. }
  598. new_frame = wasm_stack_top;
  599. if (comp_ctx->call_stack_features.bounds_checks) {
  600. if (!(check_wasm_stack_succ = LLVMAppendBasicBlockInContext(
  601. comp_ctx->context, func_ctx->func,
  602. "check_wasm_stack_succ"))) {
  603. aot_set_last_error("llvm add basic block failed.");
  604. return false;
  605. }
  606. LLVMMoveBasicBlockAfter(check_wasm_stack_succ,
  607. LLVMGetInsertBlock(comp_ctx->builder));
  608. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT,
  609. wasm_stack_top_max, wasm_stack_top_bound,
  610. "cmp"))) {
  611. aot_set_last_error("llvm build icmp failed");
  612. return false;
  613. }
  614. if (!(aot_emit_exception(comp_ctx, func_ctx,
  615. EXCE_OPERAND_STACK_OVERFLOW, true, cmp,
  616. check_wasm_stack_succ))) {
  617. return false;
  618. }
  619. }
  620. #if WASM_ENABLE_GC != 0
  621. if (comp_ctx->enable_gc) {
  622. LLVMValueRef wasm_stack_top_new, frame_ref, frame_ref_ptr;
  623. uint32 j, k;
  624. /* exec_env->wasm_stack.top += frame_size */
  625. offset = I32_CONST(frame_size);
  626. CHECK_LLVM_CONST(offset);
  627. if (!(wasm_stack_top_new = LLVMBuildInBoundsGEP2(
  628. comp_ctx->builder, INT8_TYPE, wasm_stack_top, &offset, 1,
  629. "wasm_stack_top_new"))) {
  630. aot_set_last_error("llvm build in bounds gep failed");
  631. return false;
  632. }
  633. if (!LLVMBuildStore(comp_ctx->builder, wasm_stack_top_new,
  634. wasm_stack_top_ptr)) {
  635. aot_set_last_error("llvm build store failed");
  636. return false;
  637. }
  638. if (func_idx < import_func_count) {
  639. LLVMValueRef frame_sp, frame_sp_ptr;
  640. /* Only need to initialize new_frame->sp when it's import function
  641. otherwise they will be committed in AOT code if needed */
  642. /* new_frame->sp = new_frame->lp + max_local_cell_num */
  643. if (!comp_ctx->is_jit_mode)
  644. offset = I32_CONST(comp_ctx->pointer_size * 5);
  645. else
  646. offset = I32_CONST(offsetof(WASMInterpFrame, sp));
  647. CHECK_LLVM_CONST(offset);
  648. if (!(frame_sp_ptr = LLVMBuildInBoundsGEP2(
  649. comp_ctx->builder, INT8_TYPE, new_frame, &offset, 1,
  650. "frame_sp_addr"))
  651. || !(frame_sp_ptr =
  652. LLVMBuildBitCast(comp_ctx->builder, frame_sp_ptr,
  653. int8_ptr_type, "frame_sp_ptr"))) {
  654. aot_set_last_error("llvm get frame_sp_ptr failed");
  655. return false;
  656. }
  657. if (!comp_ctx->is_jit_mode)
  658. offset = I32_CONST(comp_ctx->pointer_size * aot_frame_ptr_num
  659. + max_local_cell_num * sizeof(uint32));
  660. else
  661. offset = I32_CONST(offsetof(WASMInterpFrame, lp)
  662. + max_local_cell_num * sizeof(uint32));
  663. CHECK_LLVM_CONST(offset);
  664. if (!(frame_sp = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  665. new_frame, &offset, 1,
  666. "frame_sp"))) {
  667. aot_set_last_error("llvm build in bounds gep failed");
  668. return false;
  669. }
  670. if (!LLVMBuildStore(comp_ctx->builder, frame_sp, frame_sp_ptr)) {
  671. aot_set_last_error("llvm build store failed");
  672. return false;
  673. }
  674. }
  675. if (!comp_ctx->is_jit_mode) {
  676. /* new_frame->frame_ref = new_frame->lp + max_local_cell_num
  677. + max_stack_cell_num */
  678. offset = I32_CONST(comp_ctx->pointer_size * 6);
  679. CHECK_LLVM_CONST(offset);
  680. if (!(frame_ref_ptr = LLVMBuildInBoundsGEP2(
  681. comp_ctx->builder, INT8_TYPE, new_frame, &offset, 1,
  682. "frame_ref_addr"))
  683. || !(frame_ref_ptr =
  684. LLVMBuildBitCast(comp_ctx->builder, frame_ref_ptr,
  685. int8_ptr_type, "frame_ref_ptr"))) {
  686. aot_set_last_error("llvm get frame_ref_ptr failed");
  687. return false;
  688. }
  689. offset = I32_CONST(comp_ctx->pointer_size * aot_frame_ptr_num
  690. + (max_local_cell_num + max_stack_cell_num)
  691. * sizeof(uint32));
  692. CHECK_LLVM_CONST(offset);
  693. if (!(frame_ref = LLVMBuildInBoundsGEP2(comp_ctx->builder,
  694. INT8_TYPE, new_frame,
  695. &offset, 1, "frame_ref"))) {
  696. aot_set_last_error("llvm build in bounds gep failed");
  697. return false;
  698. }
  699. if (!LLVMBuildStore(comp_ctx->builder, frame_ref, frame_ref_ptr)) {
  700. aot_set_last_error("llvm build store failed");
  701. return false;
  702. }
  703. }
  704. else {
  705. /* Get frame_ref in WASMInterpFrame */
  706. offset = I32_CONST(offsetof(WASMInterpFrame, lp)
  707. + (max_local_cell_num + max_stack_cell_num)
  708. * sizeof(uint32));
  709. CHECK_LLVM_CONST(offset);
  710. if (!(frame_ref = LLVMBuildInBoundsGEP2(comp_ctx->builder,
  711. INT8_TYPE, new_frame,
  712. &offset, 1, "frame_ref"))) {
  713. aot_set_last_error("llvm build in bounds gep failed");
  714. return false;
  715. }
  716. }
  717. /* Initialize frame ref flags for import function only in JIT mode */
  718. if (func_idx < import_func_count && comp_ctx->is_jit_mode) {
  719. aot_func_type = import_funcs[func_idx].func_type;
  720. for (i = 0, j = 0; i < aot_func_type->param_count; i++) {
  721. if (aot_is_type_gc_reftype(aot_func_type->types[i])
  722. && !wasm_is_reftype_i31ref(aot_func_type->types[i])) {
  723. for (k = 0; k < comp_ctx->pointer_size / sizeof(uint32);
  724. k++) {
  725. /* frame_ref[j++] = 1 */
  726. offset = I32_CONST(j);
  727. CHECK_LLVM_CONST(offset);
  728. frame_ref_ptr = LLVMBuildInBoundsGEP2(
  729. comp_ctx->builder, INT8_TYPE, frame_ref, &offset, 1,
  730. "frame_ref_ptr");
  731. if (!LLVMBuildStore(comp_ctx->builder, I8_ONE,
  732. frame_ref_ptr)) {
  733. aot_set_last_error("llvm build store failed");
  734. return false;
  735. }
  736. j++;
  737. }
  738. }
  739. else {
  740. uint32 value_type_cell_num =
  741. wasm_value_type_cell_num_internal(
  742. aot_func_type->types[i], comp_ctx->pointer_size);
  743. for (k = 0; k < value_type_cell_num; k++) {
  744. /* frame_ref[j++] = 0 */
  745. offset = I32_CONST(j);
  746. CHECK_LLVM_CONST(offset);
  747. frame_ref_ptr = LLVMBuildInBoundsGEP2(
  748. comp_ctx->builder, INT8_TYPE, frame_ref, &offset, 1,
  749. "frame_ref_ptr");
  750. if (!LLVMBuildStore(comp_ctx->builder, I8_ZERO,
  751. frame_ref_ptr)) {
  752. aot_set_last_error("llvm build store failed");
  753. return false;
  754. }
  755. j++;
  756. }
  757. }
  758. }
  759. for (; j < 2; j++) {
  760. /* frame_ref[j++] = 0 */
  761. offset = I32_CONST(j);
  762. CHECK_LLVM_CONST(offset);
  763. frame_ref_ptr = LLVMBuildInBoundsGEP2(
  764. comp_ctx->builder, INT8_TYPE, frame_ref, &offset, 1,
  765. "frame_ref_ptr");
  766. if (!LLVMBuildStore(comp_ctx->builder, I8_ZERO,
  767. frame_ref_ptr)) {
  768. aot_set_last_error("llvm build store failed");
  769. return false;
  770. }
  771. }
  772. }
  773. }
  774. #endif /* end of WASM_ENABLE_GC != 0 */
  775. /* new_frame->prev_frame = cur_frame */
  776. if (!(prev_frame_ptr = LLVMBuildBitCast(comp_ctx->builder, new_frame,
  777. int8_ptr_type, "prev_frame_ptr"))) {
  778. aot_set_last_error("llvm build bitcast failed");
  779. return false;
  780. }
  781. if (!LLVMBuildStore(comp_ctx->builder, cur_frame, prev_frame_ptr)) {
  782. aot_set_last_error("llvm build store failed");
  783. return false;
  784. }
  785. if (!comp_ctx->is_jit_mode) {
  786. if (comp_ctx->call_stack_features.func_idx) {
  787. /* aot mode: new_frame->func_idx = func_idx */
  788. func_idx_val = comp_ctx->pointer_size == sizeof(uint64)
  789. ? I64_CONST(func_idx)
  790. : I32_CONST(func_idx);
  791. offset = I32_CONST(comp_ctx->pointer_size);
  792. CHECK_LLVM_CONST(func_idx_val);
  793. CHECK_LLVM_CONST(offset);
  794. if (!(func_idx_ptr = LLVMBuildInBoundsGEP2(
  795. comp_ctx->builder, INT8_TYPE, new_frame, &offset, 1,
  796. "func_idx_addr"))
  797. || !(func_idx_ptr =
  798. LLVMBuildBitCast(comp_ctx->builder, func_idx_ptr,
  799. INTPTR_T_PTR_TYPE, "func_idx_ptr"))) {
  800. aot_set_last_error("llvm get func_idx_ptr failed");
  801. return false;
  802. }
  803. if (!LLVMBuildStore(comp_ctx->builder, func_idx_val,
  804. func_idx_ptr)) {
  805. aot_set_last_error("llvm build store failed");
  806. return false;
  807. }
  808. }
  809. }
  810. else {
  811. /* jit mode: frame->function = module_inst->e->functions + func_index */
  812. LLVMValueRef functions;
  813. uint32 offset_functions =
  814. get_module_inst_extra_offset(comp_ctx)
  815. + offsetof(WASMModuleInstanceExtra, functions);
  816. offset = I32_CONST(offset_functions);
  817. CHECK_LLVM_CONST(offset);
  818. if (!(functions = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  819. func_ctx->aot_inst, &offset, 1,
  820. "functions_addr"))) {
  821. aot_set_last_error("llvm build inbounds gep failed");
  822. return false;
  823. }
  824. if (!(functions = LLVMBuildBitCast(comp_ctx->builder, functions,
  825. int8_ptr_type, "functions_ptr"))) {
  826. aot_set_last_error("llvm build bitcast failed");
  827. return false;
  828. }
  829. if (!(functions = LLVMBuildLoad2(comp_ctx->builder, INT8_PTR_TYPE,
  830. functions, "functions"))) {
  831. aot_set_last_error("llvm build load failed");
  832. return false;
  833. }
  834. offset = I32_CONST(sizeof(WASMFunctionInstance) * func_idx);
  835. CHECK_LLVM_CONST(offset);
  836. if (!(func_inst =
  837. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, functions,
  838. &offset, 1, "func_inst"))) {
  839. aot_set_last_error("llvm build inbounds gep failed");
  840. return false;
  841. }
  842. offset = I32_CONST(offsetof(WASMInterpFrame, function));
  843. CHECK_LLVM_CONST(offset);
  844. if (!(func_inst_ptr =
  845. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, new_frame,
  846. &offset, 1, "func_inst_addr"))
  847. || !(func_inst_ptr =
  848. LLVMBuildBitCast(comp_ctx->builder, func_inst_ptr,
  849. int8_ptr_type, "func_inst_ptr"))) {
  850. aot_set_last_error("llvm get func_inst_ptr failed");
  851. return false;
  852. }
  853. if (!LLVMBuildStore(comp_ctx->builder, func_inst, func_inst_ptr)) {
  854. aot_set_last_error("llvm build store failed");
  855. return false;
  856. }
  857. }
  858. /* No need to initialize new_frame->sp and new_frame->ip_offset
  859. since they will be committed in AOT/JIT code if needed */
  860. /* exec_env->cur_frame = new_frame */
  861. if (!LLVMBuildStore(comp_ctx->builder, new_frame, cur_frame_ptr)) {
  862. aot_set_last_error("llvm build store failed");
  863. return false;
  864. }
  865. if (comp_ctx->enable_perf_profiling || comp_ctx->enable_memory_profiling) {
  866. LLVMTypeRef param_types[2], func_type, func_ptr_type;
  867. LLVMValueRef param_values[2], func = NULL, res;
  868. char *func_name = "aot_frame_update_profile_info";
  869. /* Call aot_frame_update_profile_info for AOT or
  870. llvm_jit_frame_update_profile_info for JIT */
  871. param_types[0] = comp_ctx->exec_env_type;
  872. param_types[1] = INT8_TYPE;
  873. if (!(func_type = LLVMFunctionType(VOID_TYPE, param_types, 2, false))) {
  874. aot_set_last_error("llvm add function type failed.");
  875. return false;
  876. }
  877. if (comp_ctx->is_jit_mode) {
  878. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  879. aot_set_last_error("create LLVM function type failed.");
  880. return false;
  881. }
  882. #if WASM_ENABLE_JIT != 0 \
  883. && (WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_MEMORY_PROFILING != 0)
  884. /* JIT mode, call the function directly */
  885. if (!(func = I64_CONST(
  886. (uint64)(uintptr_t)llvm_jit_frame_update_profile_info))
  887. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  888. aot_set_last_error("create LLVM value failed.");
  889. return false;
  890. }
  891. #endif
  892. }
  893. else if (comp_ctx->is_indirect_mode) {
  894. int32 func_index;
  895. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  896. aot_set_last_error("create LLVM function type failed.");
  897. return false;
  898. }
  899. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  900. if (func_index < 0) {
  901. return false;
  902. }
  903. if (!(func =
  904. aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  905. func_ptr_type, func_index))) {
  906. return false;
  907. }
  908. }
  909. else {
  910. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  911. && !(func = LLVMAddFunction(func_ctx->module, func_name,
  912. func_type))) {
  913. aot_set_last_error("add LLVM function failed.");
  914. return false;
  915. }
  916. }
  917. param_values[0] = func_ctx->exec_env;
  918. param_values[1] = I8_ONE;
  919. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  920. param_values, 2, ""))) {
  921. aot_set_last_error("llvm build call failed.");
  922. return false;
  923. }
  924. }
  925. return true;
  926. fail:
  927. return false;
  928. }
  929. static bool
  930. free_frame_for_aot_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  931. {
  932. LLVMValueRef cur_frame_ptr = func_ctx->cur_frame_ptr, cur_frame;
  933. LLVMValueRef wasm_stack_top_ptr = func_ctx->wasm_stack_top_ptr;
  934. LLVMTypeRef int8_ptr_type;
  935. /* `int8 **` type */
  936. int8_ptr_type = LLVMPointerType(INT8_PTR_TYPE, 0);
  937. if (!int8_ptr_type) {
  938. aot_set_last_error("create llvm pointer type failed");
  939. return false;
  940. }
  941. if (comp_ctx->enable_perf_profiling) {
  942. LLVMTypeRef param_types[2], func_type, func_ptr_type;
  943. LLVMValueRef param_values[2], func = NULL, res;
  944. char *func_name = "aot_frame_update_profile_info";
  945. /* call aot_frame_update_profile_info for AOT or
  946. llvm_jit_frame_update_profile_info for JIT */
  947. param_types[0] = comp_ctx->exec_env_type;
  948. param_types[1] = INT8_TYPE;
  949. if (!(func_type = LLVMFunctionType(VOID_TYPE, param_types, 2, false))) {
  950. aot_set_last_error("llvm add function type failed.");
  951. return false;
  952. }
  953. if (comp_ctx->is_jit_mode) {
  954. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  955. aot_set_last_error("create LLVM function type failed.");
  956. return false;
  957. }
  958. #if WASM_ENABLE_JIT != 0 \
  959. && (WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_MEMORY_PROFILING != 0)
  960. /* JIT mode, call the function directly */
  961. if (!(func = I64_CONST(
  962. (uint64)(uintptr_t)llvm_jit_frame_update_profile_info))
  963. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  964. aot_set_last_error("create LLVM value failed.");
  965. return false;
  966. }
  967. #endif
  968. }
  969. else if (comp_ctx->is_indirect_mode) {
  970. int32 func_index;
  971. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  972. aot_set_last_error("create LLVM function type failed.");
  973. return false;
  974. }
  975. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  976. if (func_index < 0) {
  977. return false;
  978. }
  979. if (!(func =
  980. aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  981. func_ptr_type, func_index))) {
  982. return false;
  983. }
  984. }
  985. else {
  986. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  987. && !(func = LLVMAddFunction(func_ctx->module, func_name,
  988. func_type))) {
  989. aot_set_last_error("add LLVM function failed.");
  990. return false;
  991. }
  992. }
  993. param_values[0] = func_ctx->exec_env;
  994. param_values[1] = I8_ZERO;
  995. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  996. param_values, 2, ""))) {
  997. aot_set_last_error("llvm build call failed.");
  998. return false;
  999. }
  1000. }
  1001. if (comp_ctx->enable_gc) {
  1002. /* cur_frame = exec_env->cur_frame */
  1003. if (!(cur_frame = LLVMBuildLoad2(comp_ctx->builder, INT8_PTR_TYPE,
  1004. cur_frame_ptr, "cur_frame"))) {
  1005. aot_set_last_error("llvm build load failed");
  1006. return false;
  1007. }
  1008. /* exec_env->wasm_stack.top = cur_frame */
  1009. if (!LLVMBuildStore(comp_ctx->builder, cur_frame, wasm_stack_top_ptr)) {
  1010. aot_set_last_error("llvm build store failed");
  1011. return false;
  1012. }
  1013. }
  1014. /* exec_env->cur_frame = prev_frame */
  1015. if (!LLVMBuildStore(comp_ctx->builder, func_ctx->cur_frame,
  1016. cur_frame_ptr)) {
  1017. aot_set_last_error("llvm build store failed");
  1018. return false;
  1019. }
  1020. return true;
  1021. }
  1022. #endif /* end of WASM_ENABLE_AOT_STACK_FRAME != 0 */
  1023. /**
  1024. * Check whether the app address and its buffer are inside the linear memory,
  1025. * if no, throw exception
  1026. */
  1027. static bool
  1028. check_app_addr_and_convert(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1029. bool is_str_arg, LLVMValueRef app_addr,
  1030. LLVMValueRef buf_size,
  1031. LLVMValueRef *p_native_addr_converted)
  1032. {
  1033. LLVMTypeRef func_type, func_ptr_type, func_param_types[5];
  1034. LLVMValueRef func, func_param_values[5], res, native_addr_ptr;
  1035. char *func_name = "aot_check_app_addr_and_convert";
  1036. /* prepare function type of aot_check_app_addr_and_convert */
  1037. func_param_types[0] = comp_ctx->aot_inst_type; /* module_inst */
  1038. func_param_types[1] = INT8_TYPE; /* is_str_arg */
  1039. func_param_types[2] = I64_TYPE; /* app_offset */
  1040. func_param_types[3] = I64_TYPE; /* buf_size */
  1041. func_param_types[4] =
  1042. comp_ctx->basic_types.int8_pptr_type; /* p_native_addr */
  1043. if (!(func_type =
  1044. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  1045. aot_set_last_error("llvm add function type failed.");
  1046. return false;
  1047. }
  1048. /* prepare function pointer */
  1049. if (comp_ctx->is_jit_mode) {
  1050. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1051. aot_set_last_error("create LLVM function type failed.");
  1052. return false;
  1053. }
  1054. /* JIT mode, call the function directly */
  1055. if (!(func =
  1056. I64_CONST((uint64)(uintptr_t)jit_check_app_addr_and_convert))
  1057. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  1058. aot_set_last_error("create LLVM value failed.");
  1059. return false;
  1060. }
  1061. }
  1062. else if (comp_ctx->is_indirect_mode) {
  1063. int32 func_index;
  1064. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1065. aot_set_last_error("create LLVM function type failed.");
  1066. return false;
  1067. }
  1068. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  1069. if (func_index < 0) {
  1070. return false;
  1071. }
  1072. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1073. func_ptr_type, func_index))) {
  1074. return false;
  1075. }
  1076. }
  1077. else {
  1078. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  1079. && !(func =
  1080. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  1081. aot_set_last_error("add LLVM function failed.");
  1082. return false;
  1083. }
  1084. }
  1085. if (!(native_addr_ptr = LLVMBuildBitCast(
  1086. comp_ctx->builder, func_ctx->argv_buf,
  1087. comp_ctx->basic_types.int8_pptr_type, "p_native_addr"))) {
  1088. aot_set_last_error("llvm build bit cast failed.");
  1089. return false;
  1090. }
  1091. func_param_values[0] = func_ctx->aot_inst;
  1092. func_param_values[1] = I8_CONST(is_str_arg);
  1093. func_param_values[2] = app_addr;
  1094. func_param_values[3] = buf_size;
  1095. func_param_values[4] = native_addr_ptr;
  1096. if (!func_param_values[1]) {
  1097. aot_set_last_error("llvm create const failed.");
  1098. return false;
  1099. }
  1100. /* call aot_check_app_addr_and_convert() function */
  1101. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1102. func_param_values, 5, "res"))) {
  1103. aot_set_last_error("llvm build call failed.");
  1104. return false;
  1105. }
  1106. /* Check whether exception was thrown when executing the function */
  1107. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1108. && !check_call_return(comp_ctx, func_ctx, res)) {
  1109. return false;
  1110. }
  1111. if (!(*p_native_addr_converted =
  1112. LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, native_addr_ptr,
  1113. "native_addr"))) {
  1114. aot_set_last_error("llvm build load failed.");
  1115. return false;
  1116. }
  1117. return true;
  1118. }
  1119. static void
  1120. aot_estimate_and_record_stack_usage_for_function_call(
  1121. const AOTCompContext *comp_ctx, AOTFuncContext *caller_func_ctx,
  1122. const AOTFuncType *callee_func_type)
  1123. {
  1124. unsigned int size;
  1125. if (!(comp_ctx->enable_stack_bound_check
  1126. || comp_ctx->enable_stack_estimation)) {
  1127. return;
  1128. }
  1129. size =
  1130. aot_estimate_stack_usage_for_function_call(comp_ctx, callee_func_type);
  1131. /*
  1132. * only record the max value, assuming that LLVM emits machine code
  1133. * which rewinds the stack before making the next call in the
  1134. * function.
  1135. */
  1136. if (caller_func_ctx->stack_consumption_for_func_call < size) {
  1137. caller_func_ctx->stack_consumption_for_func_call = size;
  1138. }
  1139. }
  1140. static bool
  1141. commit_params_to_frame_of_import_func(AOTCompContext *comp_ctx,
  1142. AOTFuncContext *func_ctx,
  1143. AOTFuncType *func_type,
  1144. const LLVMValueRef *param_values)
  1145. {
  1146. uint32 i, n;
  1147. if (!comp_ctx->call_stack_features.values) {
  1148. return true;
  1149. }
  1150. for (i = 0, n = 0; i < func_type->param_count; i++, n++) {
  1151. switch (func_type->types[i]) {
  1152. case VALUE_TYPE_I32:
  1153. if (!aot_frame_store_value(
  1154. comp_ctx, param_values[i], VALUE_TYPE_I32,
  1155. func_ctx->cur_frame,
  1156. offset_of_local_in_outs_area(comp_ctx, n)))
  1157. return false;
  1158. break;
  1159. case VALUE_TYPE_I64:
  1160. if (!aot_frame_store_value(
  1161. comp_ctx, param_values[i], VALUE_TYPE_I64,
  1162. func_ctx->cur_frame,
  1163. offset_of_local_in_outs_area(comp_ctx, n)))
  1164. return false;
  1165. n++;
  1166. break;
  1167. case VALUE_TYPE_F32:
  1168. if (!aot_frame_store_value(
  1169. comp_ctx, param_values[i], VALUE_TYPE_F32,
  1170. func_ctx->cur_frame,
  1171. offset_of_local_in_outs_area(comp_ctx, n)))
  1172. return false;
  1173. break;
  1174. case VALUE_TYPE_F64:
  1175. if (!aot_frame_store_value(
  1176. comp_ctx, param_values[i], VALUE_TYPE_F64,
  1177. func_ctx->cur_frame,
  1178. offset_of_local_in_outs_area(comp_ctx, n)))
  1179. return false;
  1180. n++;
  1181. break;
  1182. case VALUE_TYPE_FUNCREF:
  1183. case VALUE_TYPE_EXTERNREF:
  1184. if (comp_ctx->enable_ref_types) {
  1185. if (!aot_frame_store_value(
  1186. comp_ctx, param_values[i], VALUE_TYPE_I32,
  1187. func_ctx->cur_frame,
  1188. offset_of_local_in_outs_area(comp_ctx, n)))
  1189. return false;
  1190. }
  1191. #if WASM_ENABLE_GC != 0
  1192. else if (comp_ctx->enable_gc) {
  1193. if (!aot_frame_store_value(
  1194. comp_ctx, param_values[i], VALUE_TYPE_GC_REF,
  1195. func_ctx->cur_frame,
  1196. offset_of_local_in_outs_area(comp_ctx, n)))
  1197. return false;
  1198. if (comp_ctx->pointer_size == sizeof(uint64))
  1199. n++;
  1200. }
  1201. #endif
  1202. else {
  1203. bh_assert(0);
  1204. }
  1205. break;
  1206. #if WASM_ENABLE_GC != 0
  1207. case REF_TYPE_NULLFUNCREF:
  1208. case REF_TYPE_NULLEXTERNREF:
  1209. case REF_TYPE_NULLREF:
  1210. /* case REF_TYPE_FUNCREF: */
  1211. /* case REF_TYPE_EXTERNREF: */
  1212. case REF_TYPE_ANYREF:
  1213. case REF_TYPE_EQREF:
  1214. case REF_TYPE_HT_NULLABLE:
  1215. case REF_TYPE_HT_NON_NULLABLE:
  1216. case REF_TYPE_I31REF:
  1217. case REF_TYPE_STRUCTREF:
  1218. case REF_TYPE_ARRAYREF:
  1219. case VALUE_TYPE_GC_REF:
  1220. #if WASM_ENABLE_STRINGREF != 0
  1221. case REF_TYPE_STRINGREF:
  1222. case REF_TYPE_STRINGVIEWWTF8:
  1223. case REF_TYPE_STRINGVIEWWTF16:
  1224. case REF_TYPE_STRINGVIEWITER:
  1225. #endif
  1226. if (!aot_frame_store_value(
  1227. comp_ctx, param_values[i], VALUE_TYPE_GC_REF,
  1228. func_ctx->cur_frame,
  1229. offset_of_local_in_outs_area(comp_ctx, n)))
  1230. return false;
  1231. if (comp_ctx->pointer_size == sizeof(uint64))
  1232. n++;
  1233. break;
  1234. #endif
  1235. default:
  1236. bh_assert(0);
  1237. break;
  1238. }
  1239. }
  1240. return true;
  1241. }
  1242. bool
  1243. aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1244. uint32 func_idx, bool tail_call)
  1245. {
  1246. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  1247. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  1248. uint32 func_count = comp_ctx->func_ctx_count, param_cell_num = 0;
  1249. uint32 ext_ret_cell_num = 0, cell_num = 0;
  1250. AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
  1251. AOTFuncType *func_type;
  1252. LLVMTypeRef *param_types = NULL, ret_type;
  1253. LLVMTypeRef ext_ret_ptr_type;
  1254. LLVMValueRef *param_values = NULL, value_ret = NULL, func;
  1255. LLVMValueRef import_func_idx, res;
  1256. LLVMValueRef ext_ret, ext_ret_ptr, ext_ret_idx;
  1257. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  1258. LLVMValueRef func_idx_ref;
  1259. #endif
  1260. int32 i, j = 0, param_count, result_count, ext_ret_count;
  1261. uint64 total_size;
  1262. uint8 wasm_ret_type;
  1263. uint8 *ext_ret_types = NULL;
  1264. const char *signature = NULL;
  1265. bool ret = false;
  1266. char buf[32];
  1267. bool quick_invoke_c_api_import = false;
  1268. /* Check function index */
  1269. if (func_idx >= import_func_count + func_count) {
  1270. aot_set_last_error("Function index out of range.");
  1271. return false;
  1272. }
  1273. /* Get function type */
  1274. if (func_idx < import_func_count) {
  1275. func_type = import_funcs[func_idx].func_type;
  1276. signature = import_funcs[func_idx].signature;
  1277. }
  1278. else {
  1279. func_type =
  1280. func_ctxes[func_idx - import_func_count]->aot_func->func_type;
  1281. }
  1282. aot_estimate_and_record_stack_usage_for_function_call(comp_ctx, func_ctx,
  1283. func_type);
  1284. /* Commit stack operands, sp and ip */
  1285. if (comp_ctx->aot_frame) {
  1286. if (comp_ctx->enable_gc && !aot_gen_commit_values(comp_ctx->aot_frame))
  1287. return false;
  1288. /* Commit sp if gc is enabled and commit ip for func call */
  1289. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, comp_ctx->enable_gc,
  1290. true))
  1291. return false;
  1292. }
  1293. /* Insert suspend check point */
  1294. if (comp_ctx->enable_thread_mgr) {
  1295. if (!check_suspend_flags(comp_ctx, func_ctx, true))
  1296. return false;
  1297. }
  1298. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  1299. if (comp_ctx->aux_stack_frame_type) {
  1300. if (func_idx < import_func_count
  1301. && comp_ctx->call_stack_features.frame_per_function) {
  1302. INT_CONST(func_idx_ref, func_idx, I32_TYPE, true);
  1303. if (!aot_alloc_frame_per_function_frame_for_aot_func(
  1304. comp_ctx, func_ctx, func_idx_ref)) {
  1305. return false;
  1306. }
  1307. }
  1308. else if (!comp_ctx->call_stack_features.frame_per_function) {
  1309. if (comp_ctx->aux_stack_frame_type
  1310. != AOT_STACK_FRAME_TYPE_STANDARD) {
  1311. aot_set_last_error("unsupported mode");
  1312. return false;
  1313. }
  1314. if (!alloc_frame_for_aot_func(comp_ctx, func_ctx, func_idx)) {
  1315. return false;
  1316. }
  1317. }
  1318. }
  1319. #endif
  1320. /* Get param cell number */
  1321. param_cell_num = func_type->param_cell_num;
  1322. /* Allocate memory for parameters.
  1323. * Parameters layout:
  1324. * - exec env
  1325. * - wasm function's parameters
  1326. * - extra results'(except the first one) addresses
  1327. */
  1328. param_count = (int32)func_type->param_count;
  1329. result_count = (int32)func_type->result_count;
  1330. ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  1331. total_size =
  1332. sizeof(LLVMValueRef) * (uint64)(param_count + 1 + ext_ret_count);
  1333. if (total_size >= UINT32_MAX
  1334. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  1335. aot_set_last_error("allocate memory failed.");
  1336. return false;
  1337. }
  1338. /* First parameter is exec env */
  1339. param_values[j++] = func_ctx->exec_env;
  1340. /* Pop parameters from stack */
  1341. for (i = param_count - 1; i >= 0; i--)
  1342. POP(param_values[i + j], func_type->types[i]);
  1343. /* Set parameters for multiple return values, the first return value
  1344. is returned by function return value, and the other return values
  1345. are returned by function parameters with pointer types */
  1346. if (ext_ret_count > 0) {
  1347. ext_ret_types = func_type->types + param_count + 1;
  1348. ext_ret_cell_num = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  1349. if (ext_ret_cell_num > 64) {
  1350. aot_set_last_error("prepare extra results's return "
  1351. "address arguments failed: "
  1352. "maximum 64 parameter cell number supported.");
  1353. goto fail;
  1354. }
  1355. for (i = 0; i < ext_ret_count; i++) {
  1356. if (!(ext_ret_idx = I32_CONST(cell_num))
  1357. || !(ext_ret_ptr_type =
  1358. LLVMPointerType(TO_LLVM_TYPE(ext_ret_types[i]), 0))) {
  1359. aot_set_last_error("llvm add const or pointer type failed.");
  1360. goto fail;
  1361. }
  1362. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i);
  1363. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(
  1364. comp_ctx->builder, I32_TYPE, func_ctx->argv_buf,
  1365. &ext_ret_idx, 1, buf))) {
  1366. aot_set_last_error("llvm build GEP failed.");
  1367. goto fail;
  1368. }
  1369. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i);
  1370. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  1371. ext_ret_ptr_type, buf))) {
  1372. aot_set_last_error("llvm build bit cast failed.");
  1373. goto fail;
  1374. }
  1375. param_values[param_count + 1 + i] = ext_ret_ptr;
  1376. cell_num += wasm_value_type_cell_num_internal(
  1377. ext_ret_types[i], comp_ctx->pointer_size);
  1378. }
  1379. }
  1380. if (func_idx < import_func_count) {
  1381. if (comp_ctx->aux_stack_frame_type == AOT_STACK_FRAME_TYPE_STANDARD
  1382. && !commit_params_to_frame_of_import_func(
  1383. comp_ctx, func_ctx, func_type, param_values + 1)) {
  1384. goto fail;
  1385. }
  1386. if (!(import_func_idx = I32_CONST(func_idx))) {
  1387. aot_set_last_error("llvm build inbounds gep failed.");
  1388. goto fail;
  1389. }
  1390. /* Initialize parameter types of the LLVM function */
  1391. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  1392. if (total_size >= UINT32_MAX
  1393. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  1394. aot_set_last_error("allocate memory failed.");
  1395. goto fail;
  1396. }
  1397. j = 0;
  1398. param_types[j++] = comp_ctx->exec_env_type;
  1399. for (i = 0; i < param_count; i++, j++) {
  1400. param_types[j] = TO_LLVM_TYPE(func_type->types[i]);
  1401. /* If the signature can be gotten, e.g. the signature of the builtin
  1402. native libraries, just check the app offset and buf size, and
  1403. then convert app offset to native addr and call the native func
  1404. directly, no need to call aot_invoke_native to call it */
  1405. if (signature) {
  1406. LLVMValueRef native_addr, native_addr_size;
  1407. if (signature[i + 1] == '*' || signature[i + 1] == '$') {
  1408. param_types[j] = INT8_PTR_TYPE;
  1409. }
  1410. if (signature[i + 1] == '*') {
  1411. if (signature[i + 2] == '~')
  1412. native_addr_size = param_values[i + 2];
  1413. else
  1414. native_addr_size = I64_CONST(1);
  1415. if (!(native_addr_size = LLVMBuildZExtOrBitCast(
  1416. comp_ctx->builder, native_addr_size, I64_TYPE,
  1417. "native_addr_size_i64"))) {
  1418. aot_set_last_error("llvm build zextOrBitCast failed.");
  1419. goto fail;
  1420. }
  1421. if (!(param_values[j] = LLVMBuildZExtOrBitCast(
  1422. comp_ctx->builder, param_values[j], I64_TYPE,
  1423. "native_addr_i64"))) {
  1424. aot_set_last_error("llvm build zextOrBitCast failed.");
  1425. goto fail;
  1426. }
  1427. if (!check_app_addr_and_convert(
  1428. comp_ctx, func_ctx, false, param_values[j],
  1429. native_addr_size, &native_addr)) {
  1430. goto fail;
  1431. }
  1432. param_values[j] = native_addr;
  1433. }
  1434. else if (signature[i + 1] == '$') {
  1435. native_addr_size = I64_ZERO;
  1436. if (!(param_values[j] = LLVMBuildZExtOrBitCast(
  1437. comp_ctx->builder, param_values[j], I64_TYPE,
  1438. "native_addr_i64"))) {
  1439. aot_set_last_error("llvm build zextOrBitCast failed.");
  1440. goto fail;
  1441. }
  1442. if (!check_app_addr_and_convert(
  1443. comp_ctx, func_ctx, true, param_values[j],
  1444. native_addr_size, &native_addr)) {
  1445. goto fail;
  1446. }
  1447. param_values[j] = native_addr;
  1448. }
  1449. }
  1450. }
  1451. if (func_type->result_count) {
  1452. wasm_ret_type = func_type->types[func_type->param_count];
  1453. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  1454. }
  1455. else {
  1456. wasm_ret_type = VALUE_TYPE_VOID;
  1457. ret_type = VOID_TYPE;
  1458. }
  1459. if (!signature) {
  1460. if (comp_ctx->quick_invoke_c_api_import) {
  1461. uint32 buf_size_needed =
  1462. sizeof(wasm_val_t) * (param_count + result_count);
  1463. /* length of exec_env->argv_buf is 64 */
  1464. if (buf_size_needed < sizeof(uint32) * 64) {
  1465. for (i = 0; i < param_count + result_count; i++) {
  1466. /* Only support i32/i64/f32/f64 now */
  1467. if (!(func_type->types[i] == VALUE_TYPE_I32
  1468. || func_type->types[i] == VALUE_TYPE_I64
  1469. || func_type->types[i] == VALUE_TYPE_F32
  1470. || func_type->types[i] == VALUE_TYPE_F64))
  1471. break;
  1472. }
  1473. if (i == param_count + result_count)
  1474. quick_invoke_c_api_import = true;
  1475. }
  1476. }
  1477. if (quick_invoke_c_api_import) {
  1478. if (!call_aot_invoke_c_api_native(comp_ctx, func_ctx, func_idx,
  1479. func_type, param_values + 1))
  1480. goto fail;
  1481. }
  1482. else {
  1483. /* call aot_invoke_native() */
  1484. if (!call_aot_invoke_native_func(
  1485. comp_ctx, func_ctx, import_func_idx, func_type,
  1486. param_types + 1, param_values + 1, param_count,
  1487. param_cell_num, ret_type, wasm_ret_type, &value_ret,
  1488. &res))
  1489. goto fail;
  1490. /* Check whether there was exception thrown when executing
  1491. the function */
  1492. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1493. && !check_call_return(comp_ctx, func_ctx, res))
  1494. goto fail;
  1495. }
  1496. }
  1497. else { /* call native func directly */
  1498. LLVMTypeRef native_func_type, func_ptr_type;
  1499. LLVMValueRef func_ptr;
  1500. if (!(native_func_type = LLVMFunctionType(
  1501. ret_type, param_types, param_count + 1, false))) {
  1502. aot_set_last_error("llvm add function type failed.");
  1503. goto fail;
  1504. }
  1505. if (!(func_ptr_type = LLVMPointerType(native_func_type, 0))) {
  1506. aot_set_last_error("create LLVM function type failed.");
  1507. goto fail;
  1508. }
  1509. /* Load function pointer */
  1510. if (!(func_ptr = LLVMBuildInBoundsGEP2(
  1511. comp_ctx->builder, OPQ_PTR_TYPE, func_ctx->func_ptrs,
  1512. &import_func_idx, 1, "native_func_ptr_tmp"))) {
  1513. aot_set_last_error("llvm build inbounds gep failed.");
  1514. goto fail;
  1515. }
  1516. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE,
  1517. func_ptr, "native_func_ptr"))) {
  1518. aot_set_last_error("llvm build load failed.");
  1519. goto fail;
  1520. }
  1521. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  1522. func_ptr_type, "native_func"))) {
  1523. aot_set_last_error("llvm bit cast failed.");
  1524. goto fail;
  1525. }
  1526. /* Call the function */
  1527. if (!(value_ret = LLVMBuildCall2(
  1528. comp_ctx->builder, native_func_type, func, param_values,
  1529. (uint32)param_count + 1 + ext_ret_count,
  1530. (func_type->result_count > 0 ? "call" : "")))) {
  1531. aot_set_last_error("LLVM build call failed.");
  1532. goto fail;
  1533. }
  1534. /* Check whether there was exception thrown when executing
  1535. the function */
  1536. if (!check_exception_thrown(comp_ctx, func_ctx)) {
  1537. goto fail;
  1538. }
  1539. }
  1540. }
  1541. else {
  1542. #if LLVM_VERSION_MAJOR >= 14
  1543. LLVMTypeRef llvm_func_type;
  1544. #endif
  1545. if (comp_ctx->is_indirect_mode) {
  1546. LLVMTypeRef func_ptr_type;
  1547. if (!(func_ptr_type = LLVMPointerType(
  1548. func_ctxes[func_idx - import_func_count]->func_type,
  1549. 0))) {
  1550. aot_set_last_error("construct func ptr type failed.");
  1551. goto fail;
  1552. }
  1553. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->func_ptrs,
  1554. func_ptr_type, func_idx))) {
  1555. goto fail;
  1556. }
  1557. }
  1558. else {
  1559. if (func_ctxes[func_idx - import_func_count] == func_ctx) {
  1560. /* recursive call */
  1561. func = func_ctx->precheck_func;
  1562. }
  1563. else {
  1564. if (!comp_ctx->is_jit_mode) {
  1565. func =
  1566. func_ctxes[func_idx - import_func_count]->precheck_func;
  1567. }
  1568. else {
  1569. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  1570. func =
  1571. func_ctxes[func_idx - import_func_count]->precheck_func;
  1572. #else
  1573. /* JIT tier-up, load func ptr from func_ptrs[func_idx] */
  1574. LLVMValueRef func_ptr, func_idx_const;
  1575. LLVMTypeRef func_ptr_type;
  1576. if (!(func_idx_const = I32_CONST(func_idx))) {
  1577. aot_set_last_error("llvm build const failed.");
  1578. goto fail;
  1579. }
  1580. if (!(func_ptr = LLVMBuildInBoundsGEP2(
  1581. comp_ctx->builder, OPQ_PTR_TYPE,
  1582. func_ctx->func_ptrs, &func_idx_const, 1,
  1583. "func_ptr_tmp"))) {
  1584. aot_set_last_error("llvm build inbounds gep failed.");
  1585. goto fail;
  1586. }
  1587. if (!(func_ptr =
  1588. LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE,
  1589. func_ptr, "func_ptr"))) {
  1590. aot_set_last_error("llvm build load failed.");
  1591. goto fail;
  1592. }
  1593. if (!(func_ptr_type = LLVMPointerType(
  1594. func_ctxes[func_idx - import_func_count]
  1595. ->func_type,
  1596. 0))) {
  1597. aot_set_last_error("construct func ptr type failed.");
  1598. goto fail;
  1599. }
  1600. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  1601. func_ptr_type,
  1602. "indirect_func"))) {
  1603. aot_set_last_error("llvm build bit cast failed.");
  1604. goto fail;
  1605. }
  1606. #endif /* end of !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0) */
  1607. }
  1608. }
  1609. }
  1610. #if LLVM_VERSION_MAJOR >= 14
  1611. llvm_func_type = func_ctxes[func_idx - import_func_count]->func_type;
  1612. #endif
  1613. /* Call the function */
  1614. if (!(value_ret = LLVMBuildCall2(
  1615. comp_ctx->builder, llvm_func_type, func, param_values,
  1616. (uint32)param_count + 1 + ext_ret_count,
  1617. (func_type->result_count > 0 ? "call" : "")))) {
  1618. aot_set_last_error("LLVM build call failed.");
  1619. goto fail;
  1620. }
  1621. if (tail_call)
  1622. LLVMSetTailCall(value_ret, true);
  1623. /* Check whether there was exception thrown when executing
  1624. the function */
  1625. if (!tail_call
  1626. && (comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1627. && !check_exception_thrown(comp_ctx, func_ctx))
  1628. goto fail;
  1629. }
  1630. if (func_type->result_count > 0 && !quick_invoke_c_api_import) {
  1631. /* Push the first result to stack */
  1632. PUSH(value_ret, func_type->types[func_type->param_count]);
  1633. /* Load extra result from its address and push to stack */
  1634. for (i = 0; i < ext_ret_count; i++) {
  1635. snprintf(buf, sizeof(buf), "func%d_ext_ret%d", func_idx, i);
  1636. if (!(ext_ret = LLVMBuildLoad2(
  1637. comp_ctx->builder, TO_LLVM_TYPE(ext_ret_types[i]),
  1638. param_values[1 + param_count + i], buf))) {
  1639. aot_set_last_error("llvm build load failed.");
  1640. goto fail;
  1641. }
  1642. LLVMSetAlignment(ext_ret, 4);
  1643. PUSH(ext_ret, ext_ret_types[i]);
  1644. }
  1645. }
  1646. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  1647. if (comp_ctx->aux_stack_frame_type) {
  1648. if (func_idx < import_func_count
  1649. && comp_ctx->call_stack_features.frame_per_function) {
  1650. if (!aot_free_frame_per_function_frame_for_aot_func(comp_ctx,
  1651. func_ctx)) {
  1652. goto fail;
  1653. }
  1654. }
  1655. else if (!comp_ctx->call_stack_features.frame_per_function) {
  1656. if (comp_ctx->aux_stack_frame_type
  1657. != AOT_STACK_FRAME_TYPE_STANDARD) {
  1658. aot_set_last_error("unsupported mode");
  1659. }
  1660. if (!free_frame_for_aot_func(comp_ctx, func_ctx)) {
  1661. goto fail;
  1662. }
  1663. }
  1664. }
  1665. #endif
  1666. /* Insert suspend check point */
  1667. if (comp_ctx->enable_thread_mgr) {
  1668. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  1669. goto fail;
  1670. }
  1671. ret = true;
  1672. fail:
  1673. if (param_types)
  1674. wasm_runtime_free(param_types);
  1675. if (param_values)
  1676. wasm_runtime_free(param_values);
  1677. return ret;
  1678. }
  1679. #if WASM_ENABLE_GC != 0
  1680. static LLVMValueRef
  1681. call_aot_func_type_is_super_of_func(AOTCompContext *comp_ctx,
  1682. AOTFuncContext *func_ctx,
  1683. LLVMValueRef type_idx1,
  1684. LLVMValueRef type_idx2)
  1685. {
  1686. LLVMValueRef param_values[3], ret_value, value, func;
  1687. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1688. param_types[0] = comp_ctx->aot_inst_type;
  1689. param_types[1] = I32_TYPE;
  1690. param_types[2] = I32_TYPE;
  1691. ret_type = INT8_TYPE;
  1692. #if WASM_ENABLE_JIT != 0
  1693. if (comp_ctx->is_jit_mode)
  1694. GET_AOT_FUNCTION(llvm_jit_func_type_is_super_of, 3);
  1695. else
  1696. #endif
  1697. GET_AOT_FUNCTION(aot_func_type_is_super_of, 3);
  1698. param_values[0] = func_ctx->aot_inst;
  1699. param_values[1] = type_idx1;
  1700. param_values[2] = type_idx2;
  1701. if (!(ret_value =
  1702. LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  1703. 3, "call_aot_func_type_is_super_of"))) {
  1704. aot_set_last_error("llvm build call failed.");
  1705. return NULL;
  1706. }
  1707. if (!(ret_value = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, ret_value,
  1708. I8_ZERO, "check_fail"))) {
  1709. aot_set_last_error("llvm build icmp failed.");
  1710. return NULL;
  1711. }
  1712. return ret_value;
  1713. fail:
  1714. return NULL;
  1715. }
  1716. #endif
  1717. static bool
  1718. call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1719. AOTFuncType *aot_func_type,
  1720. LLVMValueRef func_type_idx, LLVMValueRef table_idx,
  1721. LLVMValueRef table_elem_idx,
  1722. LLVMTypeRef *param_types,
  1723. LLVMValueRef *param_values, uint32 param_count,
  1724. uint32 param_cell_num, uint32 result_count,
  1725. uint8 *wasm_ret_types, LLVMValueRef *value_rets,
  1726. LLVMValueRef *p_res)
  1727. {
  1728. LLVMTypeRef func_type, func_ptr_type, func_param_types[6];
  1729. LLVMTypeRef ret_type, ret_ptr_type, elem_ptr_type;
  1730. LLVMValueRef func, ret_idx, ret_ptr, elem_idx, elem_ptr;
  1731. LLVMValueRef func_param_values[6], res = NULL;
  1732. char buf[32], *func_name = "aot_call_indirect";
  1733. uint32 i, cell_num = 0, ret_cell_num, argv_cell_num;
  1734. /* prepare function type of aot_call_indirect */
  1735. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  1736. func_param_types[1] = I32_TYPE; /* table_idx */
  1737. func_param_types[2] = I32_TYPE; /* table_elem_idx */
  1738. func_param_types[3] = I32_TYPE; /* argc */
  1739. func_param_types[4] = INT32_PTR_TYPE; /* argv */
  1740. if (!(func_type =
  1741. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  1742. aot_set_last_error("llvm add function type failed.");
  1743. return false;
  1744. }
  1745. /* prepare function pointer */
  1746. if (comp_ctx->is_jit_mode) {
  1747. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1748. aot_set_last_error("create LLVM function type failed.");
  1749. return false;
  1750. }
  1751. /* JIT mode, call the function directly */
  1752. if (!(func = I64_CONST((uint64)(uintptr_t)llvm_jit_call_indirect))
  1753. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  1754. aot_set_last_error("create LLVM value failed.");
  1755. return false;
  1756. }
  1757. }
  1758. else if (comp_ctx->is_indirect_mode) {
  1759. int32 func_index;
  1760. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1761. aot_set_last_error("create LLVM function type failed.");
  1762. return false;
  1763. }
  1764. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  1765. if (func_index < 0) {
  1766. return false;
  1767. }
  1768. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1769. func_ptr_type, func_index))) {
  1770. return false;
  1771. }
  1772. }
  1773. else {
  1774. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  1775. && !(func =
  1776. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  1777. aot_set_last_error("add LLVM function failed.");
  1778. return false;
  1779. }
  1780. }
  1781. ret_cell_num = wasm_get_cell_num(wasm_ret_types, result_count);
  1782. argv_cell_num =
  1783. param_cell_num > ret_cell_num ? param_cell_num : ret_cell_num;
  1784. if (argv_cell_num > 64) {
  1785. aot_set_last_error("prepare native arguments failed: "
  1786. "maximum 64 parameter cell number supported.");
  1787. return false;
  1788. }
  1789. /* prepare frame_lp */
  1790. for (i = 0; i < param_count; i++) {
  1791. if (!(elem_idx = I32_CONST(cell_num))
  1792. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  1793. aot_set_last_error("llvm add const or pointer type failed.");
  1794. return false;
  1795. }
  1796. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  1797. if (!(elem_ptr =
  1798. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  1799. func_ctx->argv_buf, &elem_idx, 1, buf))
  1800. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  1801. elem_ptr_type, buf))) {
  1802. aot_set_last_error("llvm build bit cast failed.");
  1803. return false;
  1804. }
  1805. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  1806. elem_ptr))) {
  1807. aot_set_last_error("llvm build store failed.");
  1808. return false;
  1809. }
  1810. LLVMSetAlignment(res, 1);
  1811. cell_num += wasm_value_type_cell_num_internal(aot_func_type->types[i],
  1812. comp_ctx->pointer_size);
  1813. }
  1814. func_param_values[0] = func_ctx->exec_env;
  1815. func_param_values[1] = table_idx;
  1816. func_param_values[2] = table_elem_idx;
  1817. func_param_values[3] = I32_CONST(param_cell_num);
  1818. func_param_values[4] = func_ctx->argv_buf;
  1819. if (!func_param_values[3]) {
  1820. aot_set_last_error("llvm create const failed.");
  1821. return false;
  1822. }
  1823. /* call aot_call_indirect() function */
  1824. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1825. func_param_values, 5, "res"))) {
  1826. aot_set_last_error("llvm build call failed.");
  1827. return false;
  1828. }
  1829. /* get function result values */
  1830. cell_num = 0;
  1831. for (i = 0; i < result_count; i++) {
  1832. ret_type = TO_LLVM_TYPE(wasm_ret_types[i]);
  1833. if (!(ret_idx = I32_CONST(cell_num))
  1834. || !(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  1835. aot_set_last_error("llvm add const or pointer type failed.");
  1836. return false;
  1837. }
  1838. snprintf(buf, sizeof(buf), "argv_ret%d", i);
  1839. if (!(ret_ptr =
  1840. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  1841. func_ctx->argv_buf, &ret_idx, 1, buf))
  1842. || !(ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ret_ptr,
  1843. ret_ptr_type, buf))) {
  1844. aot_set_last_error("llvm build GEP or bit cast failed.");
  1845. return false;
  1846. }
  1847. snprintf(buf, sizeof(buf), "ret%d", i);
  1848. if (!(value_rets[i] =
  1849. LLVMBuildLoad2(comp_ctx->builder, ret_type, ret_ptr, buf))) {
  1850. aot_set_last_error("llvm build load failed.");
  1851. return false;
  1852. }
  1853. LLVMSetAlignment(value_rets[i], 4);
  1854. cell_num += wasm_value_type_cell_num_internal(wasm_ret_types[i],
  1855. comp_ctx->pointer_size);
  1856. }
  1857. *p_res = res;
  1858. return true;
  1859. }
  1860. bool
  1861. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1862. uint32 type_idx, uint32 tbl_idx)
  1863. {
  1864. AOTFuncType *func_type;
  1865. LLVMValueRef tbl_idx_value, elem_idx, func_idx;
  1866. LLVMValueRef table_elem_base, table_elem_addr, table_elem;
  1867. LLVMValueRef ftype_idx_ptr, ftype_idx, ftype_idx_const;
  1868. LLVMValueRef cmp_func_obj, cmp_elem_idx, cmp_func_idx, cmp_ftype_idx;
  1869. LLVMValueRef func, func_ptr, table_size_const;
  1870. LLVMValueRef ext_ret_offset, ext_ret_ptr, ext_ret, res;
  1871. LLVMValueRef *param_values = NULL, *value_rets = NULL;
  1872. LLVMValueRef *result_phis = NULL, value_ret, import_func_count;
  1873. #if WASM_ENABLE_MEMORY64 != 0
  1874. LLVMValueRef u32_max, u32_cmp_result = NULL;
  1875. #endif
  1876. LLVMTypeRef *param_types = NULL, ret_type;
  1877. LLVMTypeRef llvm_func_type, llvm_func_ptr_type;
  1878. LLVMTypeRef ext_ret_ptr_type;
  1879. LLVMBasicBlockRef check_func_obj_succ, check_elem_idx_succ,
  1880. check_ftype_idx_succ;
  1881. LLVMBasicBlockRef check_func_idx_succ, block_return, block_curr;
  1882. LLVMBasicBlockRef block_call_import, block_call_non_import;
  1883. LLVMValueRef offset;
  1884. uint32 total_param_count, func_param_count, func_result_count;
  1885. uint32 ext_cell_num, param_cell_num, i, j;
  1886. uint8 wasm_ret_type, *wasm_ret_types;
  1887. uint64 total_size;
  1888. char buf[32];
  1889. bool ret = false;
  1890. /* Check function type index */
  1891. if (type_idx >= comp_ctx->comp_data->type_count) {
  1892. aot_set_last_error("function type index out of range");
  1893. return false;
  1894. }
  1895. if (!comp_ctx->enable_gc) {
  1896. /* Find the equivalent function type whose type index is the smallest:
  1897. the callee function's type index is also converted to the smallest
  1898. one in wasm loader, so we can just check whether the two type indexes
  1899. are equal (the type index of call_indirect opcode and callee func),
  1900. we don't need to check whether the whole function types are equal,
  1901. including param types and result types. */
  1902. type_idx = wasm_get_smallest_type_idx(
  1903. (WASMTypePtr *)comp_ctx->comp_data->types,
  1904. comp_ctx->comp_data->type_count, type_idx);
  1905. }
  1906. else {
  1907. /* Call aot_func_type_is_super_of to check whether the func type
  1908. provided in the bytecode is a super type of the func type of
  1909. the function to call */
  1910. }
  1911. ftype_idx_const = I32_CONST(type_idx);
  1912. CHECK_LLVM_CONST(ftype_idx_const);
  1913. func_type = (AOTFuncType *)comp_ctx->comp_data->types[type_idx];
  1914. aot_estimate_and_record_stack_usage_for_function_call(comp_ctx, func_ctx,
  1915. func_type);
  1916. /* Commit stack operands, sp and ip */
  1917. if (comp_ctx->aot_frame) {
  1918. if (comp_ctx->enable_gc && !aot_gen_commit_values(comp_ctx->aot_frame))
  1919. return false;
  1920. /* Commit sp if gc is enabled and always commit ip for call_indirect */
  1921. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, comp_ctx->enable_gc,
  1922. true))
  1923. return false;
  1924. }
  1925. /* Insert suspend check point */
  1926. if (comp_ctx->enable_thread_mgr) {
  1927. if (!check_suspend_flags(comp_ctx, func_ctx, true))
  1928. return false;
  1929. }
  1930. func_param_count = func_type->param_count;
  1931. func_result_count = func_type->result_count;
  1932. POP_TBL_ELEM_IDX(elem_idx);
  1933. /* get the cur size of the table instance */
  1934. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  1935. + offsetof(AOTTableInstance, cur_size)))) {
  1936. HANDLE_FAILURE("LLVMConstInt");
  1937. goto fail;
  1938. }
  1939. if (!(table_size_const = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1940. func_ctx->aot_inst, &offset,
  1941. 1, "cur_size_i8p"))) {
  1942. HANDLE_FAILURE("LLVMBuildGEP");
  1943. goto fail;
  1944. }
  1945. if (!(table_size_const =
  1946. LLVMBuildBitCast(comp_ctx->builder, table_size_const,
  1947. INT32_PTR_TYPE, "cur_size_i32p"))) {
  1948. HANDLE_FAILURE("LLVMBuildBitCast");
  1949. goto fail;
  1950. }
  1951. if (!(table_size_const = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE,
  1952. table_size_const, "cur_size"))) {
  1953. HANDLE_FAILURE("LLVMBuildLoad");
  1954. goto fail;
  1955. }
  1956. #if WASM_ENABLE_MEMORY64 != 0
  1957. /* Check if elem index >= UINT32_MAX */
  1958. if (IS_TABLE64(tbl_idx)) {
  1959. if (!(u32_max = I64_CONST(UINT32_MAX))) {
  1960. aot_set_last_error("llvm build const failed");
  1961. goto fail;
  1962. }
  1963. if (!(u32_cmp_result =
  1964. LLVMBuildICmp(comp_ctx->builder, LLVMIntUGE, elem_idx,
  1965. u32_max, "cmp_elem_idx_u32_max"))) {
  1966. aot_set_last_error("llvm build icmp failed.");
  1967. goto fail;
  1968. }
  1969. if (!(elem_idx = LLVMBuildTrunc(comp_ctx->builder, elem_idx, I32_TYPE,
  1970. "elem_idx_i32"))) {
  1971. aot_set_last_error("llvm build trunc failed.");
  1972. goto fail;
  1973. }
  1974. }
  1975. #endif
  1976. /* Check if (uint32)elem index >= table size */
  1977. if (!(cmp_elem_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGE, elem_idx,
  1978. table_size_const, "cmp_elem_idx"))) {
  1979. aot_set_last_error("llvm build icmp failed.");
  1980. goto fail;
  1981. }
  1982. #if WASM_ENABLE_MEMORY64 != 0
  1983. if (IS_TABLE64(tbl_idx)) {
  1984. if (!(cmp_elem_idx =
  1985. LLVMBuildOr(comp_ctx->builder, cmp_elem_idx, u32_cmp_result,
  1986. "larger_than_u32_max_or_cur_size"))) {
  1987. aot_set_last_error("llvm build or failed.");
  1988. goto fail;
  1989. }
  1990. }
  1991. #endif
  1992. /* Throw exception if elem index >= table size or elem index >= UINT32_MAX
  1993. */
  1994. if (!(check_elem_idx_succ = LLVMAppendBasicBlockInContext(
  1995. comp_ctx->context, func_ctx->func, "check_elem_idx_succ"))) {
  1996. aot_set_last_error("llvm add basic block failed.");
  1997. goto fail;
  1998. }
  1999. LLVMMoveBasicBlockAfter(check_elem_idx_succ,
  2000. LLVMGetInsertBlock(comp_ctx->builder));
  2001. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNDEFINED_ELEMENT, true,
  2002. cmp_elem_idx, check_elem_idx_succ)))
  2003. goto fail;
  2004. /* load data as i32* */
  2005. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  2006. + offsetof(AOTTableInstance, elems)))) {
  2007. HANDLE_FAILURE("LLVMConstInt");
  2008. goto fail;
  2009. }
  2010. if (!(table_elem_base = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  2011. func_ctx->aot_inst, &offset,
  2012. 1, "table_elem_base_i8p"))) {
  2013. aot_set_last_error("llvm build add failed.");
  2014. goto fail;
  2015. }
  2016. /* Load function index */
  2017. if (comp_ctx->enable_gc) {
  2018. /* table elem is func_obj when gc is enabled */
  2019. if (!(table_elem_base =
  2020. LLVMBuildBitCast(comp_ctx->builder, table_elem_base,
  2021. GC_REF_PTR_TYPE, "table_elem_base"))) {
  2022. HANDLE_FAILURE("LLVMBuildBitCast");
  2023. goto fail;
  2024. }
  2025. if (!(table_elem_addr = LLVMBuildInBoundsGEP2(
  2026. comp_ctx->builder, GC_REF_TYPE, table_elem_base, &elem_idx, 1,
  2027. "table_elem_addr"))) {
  2028. HANDLE_FAILURE("LLVMBuildNUWAdd");
  2029. goto fail;
  2030. }
  2031. if (!(table_elem = LLVMBuildLoad2(comp_ctx->builder, GC_REF_TYPE,
  2032. table_elem_addr, "table_elem"))) {
  2033. aot_set_last_error("llvm build load failed.");
  2034. goto fail;
  2035. }
  2036. /* Check if func object is NULL */
  2037. if (!(cmp_func_obj = LLVMBuildIsNull(comp_ctx->builder, table_elem,
  2038. "cmp_func_obj"))) {
  2039. aot_set_last_error("llvm build isnull failed.");
  2040. goto fail;
  2041. }
  2042. /* Throw exception if func object is NULL */
  2043. if (!(check_func_obj_succ = LLVMAppendBasicBlockInContext(
  2044. comp_ctx->context, func_ctx->func, "check_func_obj_succ"))) {
  2045. aot_set_last_error("llvm add basic block failed.");
  2046. goto fail;
  2047. }
  2048. LLVMMoveBasicBlockAfter(check_func_obj_succ,
  2049. LLVMGetInsertBlock(comp_ctx->builder));
  2050. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNINITIALIZED_ELEMENT,
  2051. true, cmp_func_obj, check_func_obj_succ)))
  2052. goto fail;
  2053. /* Get the func idx bound of the WASMFuncObject, the offset may be
  2054. * different in 32-bit runtime and 64-bit runtime since WASMObjectHeader
  2055. * is uintptr_t. Use comp_ctx->pointer_size as the
  2056. * offsetof(WASMFuncObject, func_idx_bound)
  2057. */
  2058. if (!(offset = I32_CONST(comp_ctx->pointer_size))) {
  2059. HANDLE_FAILURE("LLVMConstInt");
  2060. goto fail;
  2061. }
  2062. if (!(func_idx = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  2063. table_elem, &offset, 1,
  2064. "func_idx_bound_i8p"))) {
  2065. HANDLE_FAILURE("LLVMBuildGEP");
  2066. goto fail;
  2067. }
  2068. if (!(func_idx =
  2069. LLVMBuildBitCast(comp_ctx->builder, func_idx, INT32_PTR_TYPE,
  2070. "func_idx_bound_i32p"))) {
  2071. HANDLE_FAILURE("LLVMBuildBitCast");
  2072. goto fail;
  2073. }
  2074. if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, func_idx,
  2075. "func_idx_bound"))) {
  2076. HANDLE_FAILURE("LLVMBuildLoad");
  2077. goto fail;
  2078. }
  2079. }
  2080. else {
  2081. if (!(table_elem_base =
  2082. LLVMBuildBitCast(comp_ctx->builder, table_elem_base,
  2083. INTPTR_T_PTR_TYPE, "table_elem_base"))) {
  2084. HANDLE_FAILURE("LLVMBuildBitCast");
  2085. goto fail;
  2086. }
  2087. if (!(table_elem_addr = LLVMBuildInBoundsGEP2(
  2088. comp_ctx->builder, INTPTR_T_TYPE, table_elem_base, &elem_idx,
  2089. 1, "table_elem_addr"))) {
  2090. HANDLE_FAILURE("LLVMBuildNUWAdd");
  2091. goto fail;
  2092. }
  2093. if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, INTPTR_T_TYPE,
  2094. table_elem_addr, "func_idx"))) {
  2095. aot_set_last_error("llvm build load failed.");
  2096. goto fail;
  2097. }
  2098. if (!(func_idx = LLVMBuildIntCast2(comp_ctx->builder, func_idx,
  2099. I32_TYPE, true, "func_idx_i32"))) {
  2100. aot_set_last_error("llvm build int cast failed.");
  2101. goto fail;
  2102. }
  2103. /* Check if func_idx == -1 */
  2104. if (!(cmp_func_idx =
  2105. LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, func_idx,
  2106. I32_NEG_ONE, "cmp_func_idx"))) {
  2107. aot_set_last_error("llvm build icmp failed.");
  2108. goto fail;
  2109. }
  2110. /* Throw exception if func_idx == -1 */
  2111. if (!(check_func_idx_succ = LLVMAppendBasicBlockInContext(
  2112. comp_ctx->context, func_ctx->func, "check_func_idx_succ"))) {
  2113. aot_set_last_error("llvm add basic block failed.");
  2114. goto fail;
  2115. }
  2116. LLVMMoveBasicBlockAfter(check_func_idx_succ,
  2117. LLVMGetInsertBlock(comp_ctx->builder));
  2118. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNINITIALIZED_ELEMENT,
  2119. true, cmp_func_idx, check_func_idx_succ)))
  2120. goto fail;
  2121. }
  2122. /* Load function type index */
  2123. if (!(ftype_idx_ptr = LLVMBuildInBoundsGEP2(
  2124. comp_ctx->builder, I32_TYPE, func_ctx->func_type_indexes,
  2125. &func_idx, 1, "ftype_idx_ptr"))) {
  2126. aot_set_last_error("llvm build inbounds gep failed.");
  2127. goto fail;
  2128. }
  2129. if (!(ftype_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, ftype_idx_ptr,
  2130. "ftype_idx"))) {
  2131. aot_set_last_error("llvm build load failed.");
  2132. goto fail;
  2133. }
  2134. #if WASM_ENABLE_GC != 0
  2135. if (comp_ctx->enable_gc) {
  2136. if (!(cmp_ftype_idx = call_aot_func_type_is_super_of_func(
  2137. comp_ctx, func_ctx, ftype_idx_const, ftype_idx))) {
  2138. goto fail;
  2139. }
  2140. }
  2141. else
  2142. #endif
  2143. {
  2144. /* Check if function type index not equal */
  2145. if (!(cmp_ftype_idx =
  2146. LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, ftype_idx,
  2147. ftype_idx_const, "cmp_ftype_idx"))) {
  2148. aot_set_last_error("llvm build icmp failed.");
  2149. goto fail;
  2150. }
  2151. }
  2152. /* Throw exception if ftype_idx != ftype_idx_const */
  2153. if (!(check_ftype_idx_succ = LLVMAppendBasicBlockInContext(
  2154. comp_ctx->context, func_ctx->func, "check_ftype_idx_succ"))) {
  2155. aot_set_last_error("llvm add basic block failed.");
  2156. goto fail;
  2157. }
  2158. LLVMMoveBasicBlockAfter(check_ftype_idx_succ,
  2159. LLVMGetInsertBlock(comp_ctx->builder));
  2160. if (!(aot_emit_exception(comp_ctx, func_ctx,
  2161. EXCE_INVALID_FUNCTION_TYPE_INDEX, true,
  2162. cmp_ftype_idx, check_ftype_idx_succ)))
  2163. goto fail;
  2164. /* Initialize parameter types of the LLVM function */
  2165. total_param_count = 1 + func_param_count;
  2166. /* Extra function results' addresses (except the first one) are
  2167. appended to aot function parameters. */
  2168. if (func_result_count > 1)
  2169. total_param_count += func_result_count - 1;
  2170. total_size = sizeof(LLVMTypeRef) * (uint64)total_param_count;
  2171. if (total_size >= UINT32_MAX
  2172. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  2173. aot_set_last_error("allocate memory failed.");
  2174. goto fail;
  2175. }
  2176. /* Prepare param types */
  2177. j = 0;
  2178. param_types[j++] = comp_ctx->exec_env_type;
  2179. for (i = 0; i < func_param_count; i++)
  2180. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  2181. for (i = 1; i < func_result_count; i++, j++) {
  2182. param_types[j] = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  2183. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  2184. aot_set_last_error("llvm get pointer type failed.");
  2185. goto fail;
  2186. }
  2187. }
  2188. /* Resolve return type of the LLVM function */
  2189. if (func_result_count) {
  2190. wasm_ret_type = func_type->types[func_param_count];
  2191. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  2192. }
  2193. else {
  2194. wasm_ret_type = VALUE_TYPE_VOID;
  2195. ret_type = VOID_TYPE;
  2196. }
  2197. /* Allocate memory for parameters */
  2198. total_size = sizeof(LLVMValueRef) * (uint64)total_param_count;
  2199. if (total_size >= UINT32_MAX
  2200. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  2201. aot_set_last_error("allocate memory failed.");
  2202. goto fail;
  2203. }
  2204. /* First parameter is exec env */
  2205. j = 0;
  2206. param_values[j++] = func_ctx->exec_env;
  2207. /* Pop parameters from stack */
  2208. for (i = func_param_count - 1; (int32)i >= 0; i--)
  2209. POP(param_values[i + j], func_type->types[i]);
  2210. /* Prepare extra parameters */
  2211. ext_cell_num = 0;
  2212. for (i = 1; i < func_result_count; i++) {
  2213. ext_ret_offset = I32_CONST(ext_cell_num);
  2214. CHECK_LLVM_CONST(ext_ret_offset);
  2215. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i - 1);
  2216. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  2217. func_ctx->argv_buf,
  2218. &ext_ret_offset, 1, buf))) {
  2219. aot_set_last_error("llvm build GEP failed.");
  2220. goto fail;
  2221. }
  2222. ext_ret_ptr_type = param_types[func_param_count + i];
  2223. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i - 1);
  2224. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  2225. ext_ret_ptr_type, buf))) {
  2226. aot_set_last_error("llvm build bit cast failed.");
  2227. goto fail;
  2228. }
  2229. param_values[func_param_count + i] = ext_ret_ptr;
  2230. ext_cell_num += wasm_value_type_cell_num_internal(
  2231. func_type->types[func_param_count + i], comp_ctx->pointer_size);
  2232. }
  2233. if (ext_cell_num > 64) {
  2234. aot_set_last_error("prepare call-indirect arguments failed: "
  2235. "maximum 64 extra cell number supported.");
  2236. goto fail;
  2237. }
  2238. if (comp_ctx->aux_stack_frame_type
  2239. && !comp_ctx->call_stack_features.frame_per_function) {
  2240. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  2241. /* TODO: use current frame instead of allocating new frame
  2242. for WASM_OP_RETURN_CALL_INDIRECT */
  2243. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx))
  2244. goto fail;
  2245. #endif
  2246. }
  2247. /* Add basic blocks */
  2248. block_call_import = LLVMAppendBasicBlockInContext(
  2249. comp_ctx->context, func_ctx->func, "call_import");
  2250. block_call_non_import = LLVMAppendBasicBlockInContext(
  2251. comp_ctx->context, func_ctx->func, "call_non_import");
  2252. block_return = LLVMAppendBasicBlockInContext(comp_ctx->context,
  2253. func_ctx->func, "func_return");
  2254. if (!block_call_import || !block_call_non_import || !block_return) {
  2255. aot_set_last_error("llvm add basic block failed.");
  2256. goto fail;
  2257. }
  2258. LLVMMoveBasicBlockAfter(block_call_import,
  2259. LLVMGetInsertBlock(comp_ctx->builder));
  2260. LLVMMoveBasicBlockAfter(block_call_non_import, block_call_import);
  2261. LLVMMoveBasicBlockAfter(block_return, block_call_non_import);
  2262. import_func_count = I32_CONST(comp_ctx->comp_data->import_func_count);
  2263. CHECK_LLVM_CONST(import_func_count);
  2264. /* Check if func_idx < import_func_count */
  2265. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT, func_idx,
  2266. import_func_count, "cmp_func_idx"))) {
  2267. aot_set_last_error("llvm build icmp failed.");
  2268. goto fail;
  2269. }
  2270. /* If func_idx < import_func_count, jump to call import block,
  2271. else jump to call non-import block */
  2272. if (!LLVMBuildCondBr(comp_ctx->builder, cmp_func_idx, block_call_import,
  2273. block_call_non_import)) {
  2274. aot_set_last_error("llvm build cond br failed.");
  2275. goto fail;
  2276. }
  2277. /* Add result phis for return block */
  2278. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  2279. if (func_result_count > 0) {
  2280. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  2281. if (total_size >= UINT32_MAX
  2282. || !(result_phis = wasm_runtime_malloc((uint32)total_size))) {
  2283. aot_set_last_error("allocate memory failed.");
  2284. goto fail;
  2285. }
  2286. memset(result_phis, 0, (uint32)total_size);
  2287. for (i = 0; i < func_result_count; i++) {
  2288. LLVMTypeRef tmp_type =
  2289. TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  2290. if (!(result_phis[i] =
  2291. LLVMBuildPhi(comp_ctx->builder, tmp_type, "phi"))) {
  2292. aot_set_last_error("llvm build phi failed.");
  2293. goto fail;
  2294. }
  2295. }
  2296. }
  2297. /* Translate call import block */
  2298. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_import);
  2299. if (comp_ctx->aot_frame && comp_ctx->call_stack_features.frame_per_function
  2300. && !aot_alloc_frame_per_function_frame_for_aot_func(comp_ctx, func_ctx,
  2301. func_idx)) {
  2302. goto fail;
  2303. }
  2304. if (comp_ctx->aux_stack_frame_type == AOT_STACK_FRAME_TYPE_STANDARD
  2305. && !commit_params_to_frame_of_import_func(comp_ctx, func_ctx, func_type,
  2306. param_values + 1)) {
  2307. goto fail;
  2308. }
  2309. /* Allocate memory for result values */
  2310. if (func_result_count > 0) {
  2311. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  2312. if (total_size >= UINT32_MAX
  2313. || !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
  2314. aot_set_last_error("allocate memory failed.");
  2315. goto fail;
  2316. }
  2317. memset(value_rets, 0, (uint32)total_size);
  2318. }
  2319. param_cell_num = func_type->param_cell_num;
  2320. wasm_ret_types = func_type->types + func_type->param_count;
  2321. tbl_idx_value = I32_CONST(tbl_idx);
  2322. if (!tbl_idx_value) {
  2323. aot_set_last_error("llvm create const failed.");
  2324. goto fail;
  2325. }
  2326. if (!call_aot_call_indirect_func(
  2327. comp_ctx, func_ctx, func_type, ftype_idx, tbl_idx_value, elem_idx,
  2328. param_types + 1, param_values + 1, func_param_count, param_cell_num,
  2329. func_result_count, wasm_ret_types, value_rets, &res))
  2330. goto fail;
  2331. /* Check whether exception was thrown when executing the function */
  2332. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  2333. && !check_call_return(comp_ctx, func_ctx, res))
  2334. goto fail;
  2335. if (comp_ctx->aot_frame && comp_ctx->call_stack_features.frame_per_function
  2336. && !aot_free_frame_per_function_frame_for_aot_func(comp_ctx,
  2337. func_ctx)) {
  2338. goto fail;
  2339. }
  2340. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  2341. for (i = 0; i < func_result_count; i++) {
  2342. LLVMAddIncoming(result_phis[i], &value_rets[i], &block_curr, 1);
  2343. }
  2344. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  2345. aot_set_last_error("llvm build br failed.");
  2346. goto fail;
  2347. }
  2348. /* Translate call non-import block */
  2349. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_non_import);
  2350. /* Load function pointer */
  2351. if (!(func_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, OPQ_PTR_TYPE,
  2352. func_ctx->func_ptrs, &func_idx, 1,
  2353. "func_ptr_tmp"))) {
  2354. aot_set_last_error("llvm build inbounds gep failed.");
  2355. goto fail;
  2356. }
  2357. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, func_ptr,
  2358. "func_ptr"))) {
  2359. aot_set_last_error("llvm build load failed.");
  2360. goto fail;
  2361. }
  2362. if (!(llvm_func_type =
  2363. LLVMFunctionType(ret_type, param_types, total_param_count, false))
  2364. || !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
  2365. aot_set_last_error("llvm add function type failed.");
  2366. goto fail;
  2367. }
  2368. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  2369. llvm_func_ptr_type, "indirect_func"))) {
  2370. aot_set_last_error("llvm build bit cast failed.");
  2371. goto fail;
  2372. }
  2373. if (!(value_ret = LLVMBuildCall2(comp_ctx->builder, llvm_func_type, func,
  2374. param_values, total_param_count,
  2375. func_result_count > 0 ? "ret" : ""))) {
  2376. aot_set_last_error("llvm build call failed.");
  2377. goto fail;
  2378. }
  2379. /* Check whether exception was thrown when executing the function */
  2380. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  2381. && !check_exception_thrown(comp_ctx, func_ctx))
  2382. goto fail;
  2383. if (func_result_count > 0) {
  2384. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  2385. /* Push the first result to stack */
  2386. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  2387. /* Load extra result from its address and push to stack */
  2388. for (i = 1; i < func_result_count; i++) {
  2389. ret_type = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  2390. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  2391. if (!(ext_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  2392. param_values[func_param_count + i],
  2393. buf))) {
  2394. aot_set_last_error("llvm build load failed.");
  2395. goto fail;
  2396. }
  2397. LLVMSetAlignment(ext_ret, 4);
  2398. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  2399. }
  2400. }
  2401. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  2402. aot_set_last_error("llvm build br failed.");
  2403. goto fail;
  2404. }
  2405. /* Translate function return block */
  2406. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  2407. for (i = 0; i < func_result_count; i++) {
  2408. PUSH(result_phis[i], func_type->types[func_param_count + i]);
  2409. }
  2410. if (comp_ctx->aux_stack_frame_type
  2411. && !comp_ctx->call_stack_features.frame_per_function) {
  2412. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  2413. if (!free_frame_for_aot_func(comp_ctx, func_ctx))
  2414. goto fail;
  2415. #endif
  2416. }
  2417. /* Insert suspend check point */
  2418. if (comp_ctx->enable_thread_mgr) {
  2419. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  2420. goto fail;
  2421. }
  2422. ret = true;
  2423. fail:
  2424. if (param_values)
  2425. wasm_runtime_free(param_values);
  2426. if (param_types)
  2427. wasm_runtime_free(param_types);
  2428. if (value_rets)
  2429. wasm_runtime_free(value_rets);
  2430. if (result_phis)
  2431. wasm_runtime_free(result_phis);
  2432. return ret;
  2433. }
  2434. bool
  2435. aot_compile_op_ref_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  2436. {
  2437. if (comp_ctx->enable_gc)
  2438. PUSH_GC_REF(GC_REF_NULL);
  2439. else
  2440. PUSH_I32(REF_NULL);
  2441. return true;
  2442. fail:
  2443. return false;
  2444. }
  2445. bool
  2446. aot_compile_op_ref_is_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  2447. {
  2448. LLVMValueRef lhs = NULL, res;
  2449. if (comp_ctx->enable_gc) {
  2450. POP_GC_REF(lhs);
  2451. if (!(res = LLVMBuildIsNull(comp_ctx->builder, lhs, "lhs is null"))) {
  2452. HANDLE_FAILURE("LLVMBuildIsNull");
  2453. goto fail;
  2454. }
  2455. }
  2456. else {
  2457. POP_I32(lhs);
  2458. if (!(res = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, lhs, REF_NULL,
  2459. "cmp_w_null"))) {
  2460. HANDLE_FAILURE("LLVMBuildICmp");
  2461. goto fail;
  2462. }
  2463. }
  2464. if (!(res = LLVMBuildZExt(comp_ctx->builder, res, I32_TYPE, "r_i"))) {
  2465. HANDLE_FAILURE("LLVMBuildZExt");
  2466. goto fail;
  2467. }
  2468. PUSH_I32(res);
  2469. return true;
  2470. fail:
  2471. return false;
  2472. }
  2473. bool
  2474. aot_compile_op_ref_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  2475. uint32 func_idx)
  2476. {
  2477. LLVMValueRef ref_idx;
  2478. #if WASM_ENABLE_GC != 0
  2479. LLVMValueRef gc_obj;
  2480. #endif
  2481. if (!(ref_idx = I32_CONST(func_idx))) {
  2482. HANDLE_FAILURE("LLVMConstInt");
  2483. goto fail;
  2484. }
  2485. #if WASM_ENABLE_GC != 0
  2486. if (comp_ctx->enable_gc) {
  2487. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  2488. return false;
  2489. /* Commit sp and ip if gc is enabled */
  2490. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
  2491. return false;
  2492. if (!aot_call_aot_create_func_obj(comp_ctx, func_ctx, ref_idx,
  2493. &gc_obj)) {
  2494. goto fail;
  2495. }
  2496. PUSH_GC_REF(gc_obj);
  2497. }
  2498. else
  2499. #endif
  2500. {
  2501. PUSH_I32(ref_idx);
  2502. }
  2503. return true;
  2504. fail:
  2505. return false;
  2506. }
  2507. #if WASM_ENABLE_GC != 0
  2508. bool
  2509. aot_compile_op_call_ref(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  2510. uint32 type_idx, bool tail_call)
  2511. {
  2512. AOTFuncType *func_type;
  2513. LLVMValueRef func_obj, func_idx;
  2514. LLVMValueRef cmp_func_obj, cmp_func_idx;
  2515. LLVMValueRef func, func_ptr;
  2516. LLVMValueRef ext_ret_offset, ext_ret_ptr, ext_ret, res;
  2517. LLVMValueRef *param_values = NULL;
  2518. LLVMValueRef *result_phis = NULL, value_ret, import_func_count;
  2519. LLVMTypeRef *param_types = NULL, ret_type;
  2520. LLVMTypeRef llvm_func_type, llvm_func_ptr_type;
  2521. LLVMTypeRef ext_ret_ptr_type;
  2522. LLVMBasicBlockRef check_func_obj_succ, block_return, block_curr;
  2523. LLVMBasicBlockRef block_call_import, block_call_non_import;
  2524. LLVMValueRef offset;
  2525. uint32 total_param_count, func_param_count, func_result_count;
  2526. uint32 ext_cell_num, param_cell_num, i, j;
  2527. uint8 wasm_ret_type;
  2528. uint64 total_size;
  2529. char buf[32];
  2530. bool ret = false;
  2531. /* Check function type index */
  2532. bh_assert(type_idx < comp_ctx->comp_data->type_count);
  2533. func_type = (AOTFuncType *)comp_ctx->comp_data->types[type_idx];
  2534. aot_estimate_and_record_stack_usage_for_function_call(comp_ctx, func_ctx,
  2535. func_type);
  2536. func_param_count = func_type->param_count;
  2537. func_result_count = func_type->result_count;
  2538. param_cell_num = func_type->param_cell_num;
  2539. /* Commit stack operands, sp and ip to aot frame */
  2540. if (comp_ctx->aot_frame) {
  2541. /* Note that GC is enabled, no need to check it again */
  2542. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  2543. return false;
  2544. /* Commit sp if gc is enabled and always commit ip for call_ref */
  2545. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
  2546. return false;
  2547. }
  2548. /* Insert suspend check point */
  2549. if (comp_ctx->enable_thread_mgr) {
  2550. if (!check_suspend_flags(comp_ctx, func_ctx, true))
  2551. return false;
  2552. }
  2553. POP_GC_REF(func_obj);
  2554. /* Check if func object is NULL */
  2555. if (!(cmp_func_obj =
  2556. LLVMBuildIsNull(comp_ctx->builder, func_obj, "cmp_func_obj"))) {
  2557. aot_set_last_error("llvm build isnull failed.");
  2558. goto fail;
  2559. }
  2560. /* Throw exception if func object is NULL */
  2561. if (!(check_func_obj_succ = LLVMAppendBasicBlockInContext(
  2562. comp_ctx->context, func_ctx->func, "check_func_obj_succ"))) {
  2563. aot_set_last_error("llvm add basic block failed.");
  2564. goto fail;
  2565. }
  2566. LLVMMoveBasicBlockAfter(check_func_obj_succ,
  2567. LLVMGetInsertBlock(comp_ctx->builder));
  2568. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_FUNC_OBJ, true,
  2569. cmp_func_obj, check_func_obj_succ)))
  2570. goto fail;
  2571. /* Get the func idx bound of the WASMFuncObject, the offset may be
  2572. * different in 32-bit runtime and 64-bit runtime since WASMObjectHeader
  2573. * is uintptr_t. Use comp_ctx->pointer_size as the
  2574. * offsetof(WASMFuncObject, func_idx_bound) */
  2575. if (!(offset = I32_CONST(comp_ctx->pointer_size))) {
  2576. HANDLE_FAILURE("LLVMConstInt");
  2577. goto fail;
  2578. }
  2579. if (!(func_idx =
  2580. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, func_obj,
  2581. &offset, 1, "func_idx_bound_i8p"))) {
  2582. HANDLE_FAILURE("LLVMBuildGEP");
  2583. goto fail;
  2584. }
  2585. if (!(func_idx = LLVMBuildBitCast(comp_ctx->builder, func_idx,
  2586. INT32_PTR_TYPE, "func_idx_bound_i32p"))) {
  2587. HANDLE_FAILURE("LLVMBuildBitCast");
  2588. goto fail;
  2589. }
  2590. if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, func_idx,
  2591. "func_idx_bound"))) {
  2592. HANDLE_FAILURE("LLVMBuildLoad");
  2593. goto fail;
  2594. }
  2595. /* Initialize parameter types of the LLVM function */
  2596. total_param_count = 1 + func_param_count;
  2597. /* Extra function results' addresses (except the first one) are
  2598. appended to aot function parameters. */
  2599. if (func_result_count > 1)
  2600. total_param_count += func_result_count - 1;
  2601. total_size = sizeof(LLVMTypeRef) * (uint64)total_param_count;
  2602. if (total_size >= UINT32_MAX
  2603. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  2604. aot_set_last_error("allocate memory failed.");
  2605. goto fail;
  2606. }
  2607. /* Prepare param types */
  2608. j = 0;
  2609. param_types[j++] = comp_ctx->exec_env_type;
  2610. for (i = 0; i < func_param_count; i++)
  2611. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  2612. for (i = 1; i < func_result_count; i++, j++) {
  2613. param_types[j] = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  2614. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  2615. aot_set_last_error("llvm get pointer type failed.");
  2616. goto fail;
  2617. }
  2618. }
  2619. /* Resolve return type of the LLVM function */
  2620. if (func_result_count) {
  2621. wasm_ret_type = func_type->types[func_param_count];
  2622. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  2623. }
  2624. else {
  2625. wasm_ret_type = VALUE_TYPE_VOID;
  2626. ret_type = VOID_TYPE;
  2627. }
  2628. /* Allocate memory for parameters */
  2629. total_size = sizeof(LLVMValueRef) * (uint64)total_param_count;
  2630. if (total_size >= UINT32_MAX
  2631. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  2632. aot_set_last_error("allocate memory failed.");
  2633. goto fail;
  2634. }
  2635. /* First parameter is exec env */
  2636. j = 0;
  2637. param_values[j++] = func_ctx->exec_env;
  2638. /* Pop parameters from stack */
  2639. for (i = func_param_count - 1; (int32)i >= 0; i--)
  2640. POP(param_values[i + j], func_type->types[i]);
  2641. /* Prepare extra parameters */
  2642. ext_cell_num = 0;
  2643. for (i = 1; i < func_result_count; i++) {
  2644. ext_ret_offset = I32_CONST(ext_cell_num);
  2645. CHECK_LLVM_CONST(ext_ret_offset);
  2646. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i - 1);
  2647. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  2648. func_ctx->argv_buf,
  2649. &ext_ret_offset, 1, buf))) {
  2650. aot_set_last_error("llvm build GEP failed.");
  2651. goto fail;
  2652. }
  2653. ext_ret_ptr_type = param_types[func_param_count + i];
  2654. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i - 1);
  2655. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  2656. ext_ret_ptr_type, buf))) {
  2657. aot_set_last_error("llvm build bit cast failed.");
  2658. goto fail;
  2659. }
  2660. param_values[func_param_count + i] = ext_ret_ptr;
  2661. ext_cell_num += wasm_value_type_cell_num_internal(
  2662. func_type->types[func_param_count + i], comp_ctx->pointer_size);
  2663. }
  2664. if (ext_cell_num > 64) {
  2665. aot_set_last_error("prepare call-indirect arguments failed: "
  2666. "maximum 64 extra cell number supported.");
  2667. goto fail;
  2668. }
  2669. if (comp_ctx->aux_stack_frame_type
  2670. && !comp_ctx->call_stack_features.frame_per_function) {
  2671. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  2672. /* TODO: use current frame instead of allocating new frame
  2673. for WASM_OP_RETURN_CALL_REF */
  2674. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx))
  2675. goto fail;
  2676. #endif
  2677. }
  2678. /* Add basic blocks */
  2679. block_call_import = LLVMAppendBasicBlockInContext(
  2680. comp_ctx->context, func_ctx->func, "call_import");
  2681. block_call_non_import = LLVMAppendBasicBlockInContext(
  2682. comp_ctx->context, func_ctx->func, "call_non_import");
  2683. block_return = LLVMAppendBasicBlockInContext(comp_ctx->context,
  2684. func_ctx->func, "func_return");
  2685. if (!block_call_import || !block_call_non_import || !block_return) {
  2686. aot_set_last_error("llvm add basic block failed.");
  2687. goto fail;
  2688. }
  2689. LLVMMoveBasicBlockAfter(block_call_import,
  2690. LLVMGetInsertBlock(comp_ctx->builder));
  2691. LLVMMoveBasicBlockAfter(block_call_non_import, block_call_import);
  2692. LLVMMoveBasicBlockAfter(block_return, block_call_non_import);
  2693. import_func_count = I32_CONST(comp_ctx->comp_data->import_func_count);
  2694. CHECK_LLVM_CONST(import_func_count);
  2695. /* Check if func_idx < import_func_count */
  2696. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT, func_idx,
  2697. import_func_count, "cmp_func_idx"))) {
  2698. aot_set_last_error("llvm build icmp failed.");
  2699. goto fail;
  2700. }
  2701. /* If func_idx < import_func_count, jump to call import block,
  2702. else jump to call non-import block */
  2703. if (!LLVMBuildCondBr(comp_ctx->builder, cmp_func_idx, block_call_import,
  2704. block_call_non_import)) {
  2705. aot_set_last_error("llvm build cond br failed.");
  2706. goto fail;
  2707. }
  2708. /* Add result phis for return block */
  2709. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  2710. if (func_result_count > 0) {
  2711. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  2712. if (total_size >= UINT32_MAX
  2713. || !(result_phis = wasm_runtime_malloc((uint32)total_size))) {
  2714. aot_set_last_error("allocate memory failed.");
  2715. goto fail;
  2716. }
  2717. memset(result_phis, 0, (uint32)total_size);
  2718. for (i = 0; i < func_result_count; i++) {
  2719. LLVMTypeRef tmp_type =
  2720. TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  2721. if (!(result_phis[i] =
  2722. LLVMBuildPhi(comp_ctx->builder, tmp_type, "phi"))) {
  2723. aot_set_last_error("llvm build phi failed.");
  2724. goto fail;
  2725. }
  2726. }
  2727. }
  2728. /* Translate call import block */
  2729. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_import);
  2730. if (comp_ctx->aux_stack_frame_type == AOT_STACK_FRAME_TYPE_STANDARD
  2731. && !commit_params_to_frame_of_import_func(comp_ctx, func_ctx, func_type,
  2732. param_values + 1)) {
  2733. goto fail;
  2734. }
  2735. /* Similar to opcode call_indirect, but for opcode ref.func needs to call
  2736. * aot_invoke_native_func instead */
  2737. if (!call_aot_invoke_native_func(comp_ctx, func_ctx, func_idx, func_type,
  2738. param_types + 1, param_values + 1,
  2739. func_param_count, param_cell_num, ret_type,
  2740. wasm_ret_type, &value_ret, &res))
  2741. goto fail;
  2742. /* Check whether exception was thrown when executing the function */
  2743. if (comp_ctx->enable_bound_check
  2744. && !check_call_return(comp_ctx, func_ctx, res))
  2745. goto fail;
  2746. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  2747. /* Get function return values, for aot_invoke_native_func, the extra ret
  2748. * values are put into param's array */
  2749. if (func_result_count > 0) {
  2750. /* Push the first result to stack */
  2751. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  2752. /* Load extra result from its address and push to stack */
  2753. for (i = 1; i < func_result_count; i++) {
  2754. ret_type = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  2755. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  2756. if (!(ext_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  2757. param_values[func_param_count + i],
  2758. buf))) {
  2759. aot_set_last_error("llvm build load failed.");
  2760. goto fail;
  2761. }
  2762. LLVMSetAlignment(ext_ret, 4);
  2763. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  2764. }
  2765. }
  2766. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  2767. aot_set_last_error("llvm build br failed.");
  2768. goto fail;
  2769. }
  2770. /* Translate call non-import block */
  2771. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_non_import);
  2772. /* Load function pointer */
  2773. if (!(func_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, OPQ_PTR_TYPE,
  2774. func_ctx->func_ptrs, &func_idx, 1,
  2775. "func_ptr_tmp"))) {
  2776. aot_set_last_error("llvm build inbounds gep failed.");
  2777. goto fail;
  2778. }
  2779. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, func_ptr,
  2780. "func_ptr"))) {
  2781. aot_set_last_error("llvm build load failed.");
  2782. goto fail;
  2783. }
  2784. if (!(llvm_func_type =
  2785. LLVMFunctionType(ret_type, param_types, total_param_count, false))
  2786. || !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
  2787. aot_set_last_error("llvm add function type failed.");
  2788. goto fail;
  2789. }
  2790. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  2791. llvm_func_ptr_type, "indirect_func"))) {
  2792. aot_set_last_error("llvm build bit cast failed.");
  2793. goto fail;
  2794. }
  2795. if (!(value_ret = LLVMBuildCall2(comp_ctx->builder, llvm_func_type, func,
  2796. param_values, total_param_count,
  2797. func_result_count > 0 ? "ret" : ""))) {
  2798. aot_set_last_error("llvm build call failed.");
  2799. goto fail;
  2800. }
  2801. /* Set calling convention for the call with the func's calling
  2802. convention */
  2803. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  2804. if (tail_call)
  2805. LLVMSetTailCall(value_ret, true);
  2806. /* Check whether exception was thrown when executing the function */
  2807. if (!tail_call
  2808. && (comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  2809. && !check_exception_thrown(comp_ctx, func_ctx))
  2810. goto fail;
  2811. if (func_result_count > 0) {
  2812. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  2813. /* Push the first result to stack */
  2814. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  2815. /* Load extra result from its address and push to stack */
  2816. for (i = 1; i < func_result_count; i++) {
  2817. ret_type = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  2818. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  2819. if (!(ext_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  2820. param_values[func_param_count + i],
  2821. buf))) {
  2822. aot_set_last_error("llvm build load failed.");
  2823. goto fail;
  2824. }
  2825. LLVMSetAlignment(ext_ret, 4);
  2826. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  2827. }
  2828. }
  2829. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  2830. aot_set_last_error("llvm build br failed.");
  2831. goto fail;
  2832. }
  2833. /* Translate function return block */
  2834. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  2835. for (i = 0; i < func_result_count; i++) {
  2836. PUSH(result_phis[i], func_type->types[func_param_count + i]);
  2837. }
  2838. if (comp_ctx->aux_stack_frame_type
  2839. && !comp_ctx->call_stack_features.frame_per_function) {
  2840. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  2841. if (!free_frame_for_aot_func(comp_ctx, func_ctx))
  2842. goto fail;
  2843. #endif
  2844. }
  2845. /* Insert suspend check point */
  2846. if (comp_ctx->enable_thread_mgr) {
  2847. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  2848. goto fail;
  2849. }
  2850. ret = true;
  2851. fail:
  2852. if (param_values)
  2853. wasm_runtime_free(param_values);
  2854. if (param_types)
  2855. wasm_runtime_free(param_types);
  2856. if (result_phis)
  2857. wasm_runtime_free(result_phis);
  2858. return ret;
  2859. }
  2860. #endif /* end of WASM_ENABLE_GC != 0 */