aot_emit_function.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_emit_function.h"
  6. #include "aot_emit_exception.h"
  7. #include "aot_emit_control.h"
  8. #include "aot_emit_table.h"
  9. #include "../aot/aot_runtime.h"
  10. #if WASM_ENABLE_GC != 0
  11. #include "aot_emit_gc.h"
  12. #endif
  13. #define ADD_BASIC_BLOCK(block, name) \
  14. do { \
  15. if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
  16. func_ctx->func, name))) { \
  17. aot_set_last_error("llvm add basic block failed."); \
  18. goto fail; \
  19. } \
  20. } while (0)
  21. static bool
  22. is_win_platform(AOTCompContext *comp_ctx)
  23. {
  24. char *triple = LLVMGetTargetMachineTriple(comp_ctx->target_machine);
  25. bool ret;
  26. bh_assert(triple);
  27. ret = (strstr(triple, "win32") || strstr(triple, "win")) ? true : false;
  28. LLVMDisposeMessage(triple);
  29. return ret;
  30. }
  31. static bool
  32. create_func_return_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  33. {
  34. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  35. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  36. /* Create function return block if it isn't created */
  37. if (!func_ctx->func_return_block) {
  38. if (!(func_ctx->func_return_block = LLVMAppendBasicBlockInContext(
  39. comp_ctx->context, func_ctx->func, "func_ret"))) {
  40. aot_set_last_error("llvm add basic block failed.");
  41. return false;
  42. }
  43. /* Create return IR */
  44. LLVMPositionBuilderAtEnd(comp_ctx->builder,
  45. func_ctx->func_return_block);
  46. if (!comp_ctx->enable_bound_check) {
  47. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_ALREADY_THROWN,
  48. false, NULL, NULL)) {
  49. return false;
  50. }
  51. }
  52. else if (!aot_build_zero_function_ret(comp_ctx, func_ctx,
  53. aot_func_type)) {
  54. return false;
  55. }
  56. }
  57. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  58. return true;
  59. }
  60. /* Check whether there was exception thrown, if yes, return directly */
  61. static bool
  62. check_exception_thrown(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  63. {
  64. LLVMBasicBlockRef block_curr, check_exce_succ;
  65. LLVMValueRef value, cmp;
  66. /* Create function return block if it isn't created */
  67. if (!create_func_return_block(comp_ctx, func_ctx))
  68. return false;
  69. /* Load the first byte of aot_module_inst->cur_exception, and check
  70. whether it is '\0'. If yes, no exception was thrown. */
  71. if (!(value = LLVMBuildLoad2(comp_ctx->builder, INT8_TYPE,
  72. func_ctx->cur_exception, "exce_value"))
  73. || !(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, value, I8_ZERO,
  74. "cmp"))) {
  75. aot_set_last_error("llvm build icmp failed.");
  76. return false;
  77. }
  78. /* Add check exection success block */
  79. if (!(check_exce_succ = LLVMAppendBasicBlockInContext(
  80. comp_ctx->context, func_ctx->func, "check_exce_succ"))) {
  81. aot_set_last_error("llvm add basic block failed.");
  82. return false;
  83. }
  84. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  85. LLVMMoveBasicBlockAfter(check_exce_succ, block_curr);
  86. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  87. /* Create condition br */
  88. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_exce_succ,
  89. func_ctx->func_return_block)) {
  90. aot_set_last_error("llvm build cond br failed.");
  91. return false;
  92. }
  93. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_exce_succ);
  94. return true;
  95. }
  96. /* Check whether there was exception thrown, if yes, return directly */
  97. static bool
  98. check_call_return(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  99. LLVMValueRef res)
  100. {
  101. LLVMBasicBlockRef block_curr, check_call_succ;
  102. LLVMValueRef cmp;
  103. /* Create function return block if it isn't created */
  104. if (!create_func_return_block(comp_ctx, func_ctx))
  105. return false;
  106. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, res, I8_ZERO,
  107. "cmp"))) {
  108. aot_set_last_error("llvm build icmp failed.");
  109. return false;
  110. }
  111. /* Add check exection success block */
  112. if (!(check_call_succ = LLVMAppendBasicBlockInContext(
  113. comp_ctx->context, func_ctx->func, "check_call_succ"))) {
  114. aot_set_last_error("llvm add basic block failed.");
  115. return false;
  116. }
  117. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  118. LLVMMoveBasicBlockAfter(check_call_succ, block_curr);
  119. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  120. /* Create condition br */
  121. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_call_succ,
  122. func_ctx->func_return_block)) {
  123. aot_set_last_error("llvm build cond br failed.");
  124. return false;
  125. }
  126. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_call_succ);
  127. return true;
  128. }
  129. static bool
  130. call_aot_invoke_native_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  131. LLVMValueRef func_idx, AOTFuncType *aot_func_type,
  132. LLVMTypeRef *param_types,
  133. LLVMValueRef *param_values, uint32 param_count,
  134. uint32 param_cell_num, LLVMTypeRef ret_type,
  135. uint8 wasm_ret_type, LLVMValueRef *p_value_ret,
  136. LLVMValueRef *p_res)
  137. {
  138. LLVMTypeRef func_type, func_ptr_type, func_param_types[4];
  139. LLVMTypeRef ret_ptr_type, elem_ptr_type;
  140. LLVMValueRef func, elem_idx, elem_ptr;
  141. LLVMValueRef func_param_values[4], value_ret = NULL, res;
  142. char buf[32], *func_name = "aot_invoke_native";
  143. uint32 i, cell_num = 0;
  144. /* prepare function type of aot_invoke_native */
  145. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  146. func_param_types[1] = I32_TYPE; /* func_idx */
  147. func_param_types[2] = I32_TYPE; /* argc */
  148. func_param_types[3] = INT32_PTR_TYPE; /* argv */
  149. if (!(func_type =
  150. LLVMFunctionType(INT8_TYPE, func_param_types, 4, false))) {
  151. aot_set_last_error("llvm add function type failed.");
  152. return false;
  153. }
  154. /* prepare function pointer */
  155. if (comp_ctx->is_jit_mode) {
  156. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  157. aot_set_last_error("create LLVM function type failed.");
  158. return false;
  159. }
  160. /* JIT mode, call the function directly */
  161. if (!(func = I64_CONST((uint64)(uintptr_t)llvm_jit_invoke_native))
  162. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  163. aot_set_last_error("create LLVM value failed.");
  164. return false;
  165. }
  166. }
  167. else if (comp_ctx->is_indirect_mode) {
  168. int32 func_index;
  169. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  170. aot_set_last_error("create LLVM function type failed.");
  171. return false;
  172. }
  173. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  174. if (func_index < 0) {
  175. return false;
  176. }
  177. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  178. func_ptr_type, func_index))) {
  179. return false;
  180. }
  181. }
  182. else {
  183. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  184. && !(func =
  185. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  186. aot_set_last_error("add LLVM function failed.");
  187. return false;
  188. }
  189. }
  190. if (param_cell_num > 64) {
  191. aot_set_last_error("prepare native arguments failed: "
  192. "maximum 64 parameter cell number supported.");
  193. return false;
  194. }
  195. /* prepare frame_lp */
  196. for (i = 0; i < param_count; i++) {
  197. if (!(elem_idx = I32_CONST(cell_num))
  198. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  199. aot_set_last_error("llvm add const or pointer type failed.");
  200. return false;
  201. }
  202. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  203. if (!(elem_ptr =
  204. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  205. func_ctx->argv_buf, &elem_idx, 1, buf))
  206. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  207. elem_ptr_type, buf))) {
  208. aot_set_last_error("llvm build bit cast failed.");
  209. return false;
  210. }
  211. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  212. elem_ptr))) {
  213. aot_set_last_error("llvm build store failed.");
  214. return false;
  215. }
  216. LLVMSetAlignment(res, 1);
  217. cell_num += wasm_value_type_cell_num_internal(aot_func_type->types[i],
  218. comp_ctx->pointer_size);
  219. }
  220. func_param_values[0] = func_ctx->exec_env;
  221. func_param_values[1] = func_idx;
  222. func_param_values[2] = I32_CONST(param_cell_num);
  223. func_param_values[3] = func_ctx->argv_buf;
  224. if (!func_param_values[2]) {
  225. aot_set_last_error("llvm create const failed.");
  226. return false;
  227. }
  228. /* call aot_invoke_native() function */
  229. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  230. func_param_values, 4, "res"))) {
  231. aot_set_last_error("llvm build call failed.");
  232. return false;
  233. }
  234. /* get function return value */
  235. if (wasm_ret_type != VALUE_TYPE_VOID) {
  236. if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  237. aot_set_last_error("llvm add pointer type failed.");
  238. return false;
  239. }
  240. if (!(value_ret =
  241. LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
  242. ret_ptr_type, "argv_ret"))) {
  243. aot_set_last_error("llvm build bit cast failed.");
  244. return false;
  245. }
  246. if (!(*p_value_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  247. value_ret, "value_ret"))) {
  248. aot_set_last_error("llvm build load failed.");
  249. return false;
  250. }
  251. }
  252. *p_res = res;
  253. return true;
  254. }
  255. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  256. static bool
  257. call_aot_alloc_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  258. LLVMValueRef func_idx)
  259. {
  260. LLVMValueRef param_values[2], ret_value, value, func;
  261. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  262. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  263. LLVMBasicBlockRef frame_alloc_fail, frame_alloc_success;
  264. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  265. param_types[0] = comp_ctx->exec_env_type;
  266. param_types[1] = I32_TYPE;
  267. ret_type = INT8_TYPE;
  268. if (comp_ctx->is_jit_mode)
  269. GET_AOT_FUNCTION(llvm_jit_alloc_frame, 2);
  270. else
  271. GET_AOT_FUNCTION(aot_alloc_frame, 2);
  272. param_values[0] = func_ctx->exec_env;
  273. param_values[1] = func_idx;
  274. if (!(ret_value =
  275. LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  276. 2, "call_aot_alloc_frame"))) {
  277. aot_set_last_error("llvm build call failed.");
  278. return false;
  279. }
  280. if (!(ret_value = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, ret_value,
  281. I8_ZERO, "frame_alloc_ret"))) {
  282. aot_set_last_error("llvm build icmp failed.");
  283. return false;
  284. }
  285. ADD_BASIC_BLOCK(frame_alloc_fail, "frame_alloc_fail");
  286. ADD_BASIC_BLOCK(frame_alloc_success, "frame_alloc_success");
  287. LLVMMoveBasicBlockAfter(frame_alloc_fail, block_curr);
  288. LLVMMoveBasicBlockAfter(frame_alloc_success, block_curr);
  289. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, frame_alloc_success,
  290. frame_alloc_fail)) {
  291. aot_set_last_error("llvm build cond br failed.");
  292. return false;
  293. }
  294. /* If frame alloc failed, return this function
  295. so the runtime can catch the exception */
  296. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_fail);
  297. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  298. return false;
  299. }
  300. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_success);
  301. return true;
  302. fail:
  303. return false;
  304. }
  305. static bool
  306. alloc_frame_for_aot_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  307. uint32 func_idx)
  308. {
  309. LLVMValueRef wasm_stack_top_bound = func_ctx->wasm_stack_top_bound;
  310. LLVMValueRef wasm_stack_top_ptr = func_ctx->wasm_stack_top_ptr,
  311. wasm_stack_top;
  312. LLVMValueRef wasm_stack_top_max, wasm_stack_top_new, offset, cmp;
  313. LLVMValueRef cur_frame, new_frame, prev_frame_ptr;
  314. LLVMValueRef cur_frame_ptr = func_ctx->cur_frame_ptr;
  315. LLVMValueRef frame_sp, frame_sp_ptr, frame_ref, frame_ref_ptr;
  316. LLVMValueRef func_idx_ptr, func_idx_val, func_inst_ptr, func_inst;
  317. LLVMTypeRef int8_ptr_type;
  318. LLVMBasicBlockRef check_wasm_stack_succ;
  319. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  320. uint32 param_cell_num = 0, local_cell_num = 0, i, j, k;
  321. uint32 max_local_cell_num, max_stack_cell_num;
  322. uint32 all_cell_num, frame_size, frame_size_with_outs_area;
  323. uint32 aot_frame_ptr_num = offsetof(AOTFrame, lp) / sizeof(uintptr_t);
  324. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  325. AOTFuncType *func_type;
  326. AOTFunc *aot_func = NULL;
  327. /* Get param_cell_num, local_cell_num and max_stack_cell_num */
  328. if (func_idx < import_func_count) {
  329. func_type = import_funcs[func_idx].func_type;
  330. for (i = 0; i < func_type->param_count; i++)
  331. param_cell_num += wasm_value_type_cell_num_internal(
  332. func_type->types[i], comp_ctx->pointer_size);
  333. max_local_cell_num = param_cell_num > 2 ? param_cell_num : 2;
  334. max_stack_cell_num = 0;
  335. }
  336. else {
  337. aot_func = comp_ctx->comp_data->funcs[func_idx - import_func_count];
  338. param_cell_num = aot_func->param_cell_num;
  339. local_cell_num = aot_func->local_cell_num;
  340. max_local_cell_num = param_cell_num + local_cell_num;
  341. max_stack_cell_num = aot_func->max_stack_cell_num;
  342. }
  343. all_cell_num = max_local_cell_num + max_stack_cell_num;
  344. /* Get size of the frame to allocate and get size with outs_area to
  345. check whether wasm operand stack is overflow */
  346. if (!comp_ctx->is_jit_mode) {
  347. /* Refer to aot_alloc_frame */
  348. if (!comp_ctx->enable_gc) {
  349. frame_size =
  350. comp_ctx->pointer_size * aot_frame_ptr_num + all_cell_num * 4;
  351. }
  352. else
  353. frame_size = comp_ctx->pointer_size * aot_frame_ptr_num
  354. + align_uint(all_cell_num * 5, 4);
  355. frame_size_with_outs_area = frame_size
  356. + comp_ctx->pointer_size * aot_frame_ptr_num
  357. + max_stack_cell_num * 4;
  358. }
  359. else {
  360. /* Refer to wasm_interp_interp_frame_size */
  361. if (!comp_ctx->enable_gc)
  362. frame_size = offsetof(WASMInterpFrame, lp) + all_cell_num * 4;
  363. else
  364. frame_size =
  365. offsetof(WASMInterpFrame, lp) + align_uint(all_cell_num * 5, 4);
  366. frame_size_with_outs_area =
  367. frame_size + offsetof(WASMInterpFrame, lp) + max_stack_cell_num * 4;
  368. }
  369. /* `int8 **` type */
  370. int8_ptr_type = LLVMPointerType(INT8_PTR_TYPE, 0);
  371. if (!int8_ptr_type) {
  372. aot_set_last_error("create llvm pointer type failed");
  373. return false;
  374. }
  375. /* Get exec_env->wasm_stack.top */
  376. if (!(wasm_stack_top =
  377. LLVMBuildLoad2(comp_ctx->builder, INT8_PTR_TYPE,
  378. wasm_stack_top_ptr, "wasm_stack_top"))) {
  379. aot_set_last_error("load wasm_stack.top failed");
  380. return false;
  381. }
  382. cur_frame = func_ctx->cur_frame;
  383. new_frame = wasm_stack_top;
  384. /* Check whether wasm operand stack is overflow */
  385. offset = I32_CONST(frame_size_with_outs_area);
  386. CHECK_LLVM_CONST(offset);
  387. if (!(wasm_stack_top_max = LLVMBuildInBoundsGEP2(
  388. comp_ctx->builder, INT8_TYPE, wasm_stack_top, &offset, 1,
  389. "wasm_stack_top_max"))) {
  390. aot_set_last_error("llvm build in bounds gep failed");
  391. return false;
  392. }
  393. if (!(check_wasm_stack_succ = LLVMAppendBasicBlockInContext(
  394. comp_ctx->context, func_ctx->func, "check_wasm_stack_succ"))) {
  395. aot_set_last_error("llvm add basic block failed.");
  396. return false;
  397. }
  398. LLVMMoveBasicBlockAfter(check_wasm_stack_succ,
  399. LLVMGetInsertBlock(comp_ctx->builder));
  400. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, wasm_stack_top_max,
  401. wasm_stack_top_bound, "cmp"))) {
  402. aot_set_last_error("llvm build icmp failed");
  403. return false;
  404. }
  405. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_OPERAND_STACK_OVERFLOW,
  406. true, cmp, check_wasm_stack_succ))) {
  407. return false;
  408. }
  409. /* exec_env->wasm_stack.top += frame_size */
  410. offset = I32_CONST(frame_size);
  411. CHECK_LLVM_CONST(offset);
  412. if (!(wasm_stack_top_new = LLVMBuildInBoundsGEP2(
  413. comp_ctx->builder, INT8_TYPE, wasm_stack_top, &offset, 1,
  414. "wasm_stack_top_new"))) {
  415. aot_set_last_error("llvm build in bounds gep failed");
  416. return false;
  417. }
  418. if (!LLVMBuildStore(comp_ctx->builder, wasm_stack_top_new,
  419. wasm_stack_top_ptr)) {
  420. aot_set_last_error("llvm build store failed");
  421. return false;
  422. }
  423. if (func_idx < import_func_count) {
  424. /* Only need to initialize new_frame->sp when it's import function
  425. otherwise they will be committed in AOT code if needed */
  426. /* new_frame->sp = new_frame->lp + max_local_cell_num */
  427. if (!comp_ctx->is_jit_mode)
  428. offset = I32_CONST(comp_ctx->pointer_size * 5);
  429. else
  430. offset = I32_CONST(offsetof(WASMInterpFrame, sp));
  431. CHECK_LLVM_CONST(offset);
  432. if (!(frame_sp_ptr =
  433. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, new_frame,
  434. &offset, 1, "frame_sp_addr"))
  435. || !(frame_sp_ptr =
  436. LLVMBuildBitCast(comp_ctx->builder, frame_sp_ptr,
  437. int8_ptr_type, "frame_sp_ptr"))) {
  438. aot_set_last_error("llvm get frame_sp_ptr failed");
  439. return false;
  440. }
  441. if (!comp_ctx->is_jit_mode)
  442. offset = I32_CONST(comp_ctx->pointer_size * aot_frame_ptr_num
  443. + max_local_cell_num * sizeof(uint32));
  444. else
  445. offset = I32_CONST(offsetof(WASMInterpFrame, lp)
  446. + max_local_cell_num * sizeof(uint32));
  447. CHECK_LLVM_CONST(offset);
  448. if (!(frame_sp =
  449. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, new_frame,
  450. &offset, 1, "frame_sp"))) {
  451. aot_set_last_error("llvm build in bounds gep failed");
  452. return false;
  453. }
  454. if (!LLVMBuildStore(comp_ctx->builder, frame_sp, frame_sp_ptr)) {
  455. aot_set_last_error("llvm build store failed");
  456. return false;
  457. }
  458. }
  459. #if WASM_ENABLE_GC != 0
  460. if (comp_ctx->enable_gc) {
  461. if (!comp_ctx->is_jit_mode) {
  462. /* new_frame->frame_ref = new_frame->lp + max_local_cell_num
  463. + max_stack_cell_num */
  464. offset = I32_CONST(comp_ctx->pointer_size * 6);
  465. CHECK_LLVM_CONST(offset);
  466. if (!(frame_ref_ptr = LLVMBuildInBoundsGEP2(
  467. comp_ctx->builder, INT8_TYPE, new_frame, &offset, 1,
  468. "frame_ref_addr"))
  469. || !(frame_ref_ptr =
  470. LLVMBuildBitCast(comp_ctx->builder, frame_ref_ptr,
  471. int8_ptr_type, "frame_ref_ptr"))) {
  472. aot_set_last_error("llvm get frame_ref_ptr failed");
  473. return false;
  474. }
  475. offset = I32_CONST(comp_ctx->pointer_size * aot_frame_ptr_num
  476. + (max_local_cell_num + max_stack_cell_num)
  477. * sizeof(uint32));
  478. CHECK_LLVM_CONST(offset);
  479. if (!(frame_ref = LLVMBuildInBoundsGEP2(comp_ctx->builder,
  480. INT8_TYPE, new_frame,
  481. &offset, 1, "frame_ref"))) {
  482. aot_set_last_error("llvm build in bounds gep failed");
  483. return false;
  484. }
  485. if (!LLVMBuildStore(comp_ctx->builder, frame_ref, frame_ref_ptr)) {
  486. aot_set_last_error("llvm build store failed");
  487. return false;
  488. }
  489. }
  490. else {
  491. /* Get frame_ref in WASMInterpFrame */
  492. offset = I32_CONST(offsetof(WASMInterpFrame, lp)
  493. + (max_local_cell_num + max_stack_cell_num)
  494. * sizeof(uint32));
  495. CHECK_LLVM_CONST(offset);
  496. if (!(frame_ref = LLVMBuildInBoundsGEP2(comp_ctx->builder,
  497. INT8_TYPE, new_frame,
  498. &offset, 1, "frame_ref"))) {
  499. aot_set_last_error("llvm build in bounds gep failed");
  500. return false;
  501. }
  502. }
  503. /* Initialize frame ref flags for import function */
  504. if (func_idx < import_func_count) {
  505. func_type = import_funcs[func_idx].func_type;
  506. for (i = 0, j = 0; i < func_type->param_count; i++) {
  507. if (aot_is_type_gc_reftype(func_type->types[i])
  508. && !wasm_is_reftype_i31ref(func_type->types[i])) {
  509. for (k = 0; k < comp_ctx->pointer_size / sizeof(uint32);
  510. k++) {
  511. /* frame_ref[j++] = 1 */
  512. offset = I32_CONST(j);
  513. CHECK_LLVM_CONST(offset);
  514. frame_ref_ptr = LLVMBuildInBoundsGEP2(
  515. comp_ctx->builder, INT8_TYPE, frame_ref, &offset, 1,
  516. "frame_ref_ptr");
  517. if (!LLVMBuildStore(comp_ctx->builder, I8_ONE,
  518. frame_ref_ptr)) {
  519. aot_set_last_error("llvm build store failed");
  520. return false;
  521. }
  522. j++;
  523. }
  524. }
  525. else {
  526. uint32 value_type_cell_num =
  527. wasm_value_type_cell_num_internal(
  528. func_type->types[i], comp_ctx->pointer_size);
  529. for (k = 0; k < value_type_cell_num; k++) {
  530. /* frame_ref[j++] = 0 */
  531. offset = I32_CONST(j);
  532. CHECK_LLVM_CONST(offset);
  533. frame_ref_ptr = LLVMBuildInBoundsGEP2(
  534. comp_ctx->builder, INT8_TYPE, frame_ref, &offset, 1,
  535. "frame_ref_ptr");
  536. if (!LLVMBuildStore(comp_ctx->builder, I8_ZERO,
  537. frame_ref_ptr)) {
  538. aot_set_last_error("llvm build store failed");
  539. return false;
  540. }
  541. j++;
  542. }
  543. }
  544. }
  545. for (; j < 2; j++) {
  546. /* frame_ref[j++] = 0 */
  547. offset = I32_CONST(j);
  548. CHECK_LLVM_CONST(offset);
  549. frame_ref_ptr = LLVMBuildInBoundsGEP2(
  550. comp_ctx->builder, INT8_TYPE, frame_ref, &offset, 1,
  551. "frame_ref_ptr");
  552. if (!LLVMBuildStore(comp_ctx->builder, I8_ZERO,
  553. frame_ref_ptr)) {
  554. aot_set_last_error("llvm build store failed");
  555. return false;
  556. }
  557. }
  558. }
  559. }
  560. #endif /* end of WASM_ENABLE_GC != 0 */
  561. /* new_frame->prev_frame = cur_frame */
  562. if (!(prev_frame_ptr = LLVMBuildBitCast(comp_ctx->builder, new_frame,
  563. int8_ptr_type, "prev_frame_ptr"))) {
  564. aot_set_last_error("llvm build bitcast failed");
  565. return false;
  566. }
  567. if (!LLVMBuildStore(comp_ctx->builder, cur_frame, prev_frame_ptr)) {
  568. aot_set_last_error("llvm build store failed");
  569. return false;
  570. }
  571. if (!comp_ctx->is_jit_mode) {
  572. /* aot mode: new_frame->func_idx = func_idx */
  573. func_idx_val = comp_ctx->pointer_size == sizeof(uint64)
  574. ? I64_CONST(func_idx)
  575. : I32_CONST(func_idx);
  576. offset = I32_CONST(comp_ctx->pointer_size);
  577. CHECK_LLVM_CONST(func_idx_val);
  578. CHECK_LLVM_CONST(offset);
  579. if (!(func_idx_ptr =
  580. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, new_frame,
  581. &offset, 1, "func_idx_addr"))
  582. || !(func_idx_ptr =
  583. LLVMBuildBitCast(comp_ctx->builder, func_idx_ptr,
  584. INTPTR_T_PTR_TYPE, "func_idx_ptr"))) {
  585. aot_set_last_error("llvm get func_idx_ptr failed");
  586. return false;
  587. }
  588. if (!LLVMBuildStore(comp_ctx->builder, func_idx_val, func_idx_ptr)) {
  589. aot_set_last_error("llvm build store failed");
  590. return false;
  591. }
  592. }
  593. else {
  594. /* jit mode: frame->function = module_inst->e->functions + func_index */
  595. LLVMValueRef functions;
  596. uint32 offset_functions =
  597. get_module_inst_extra_offset(comp_ctx)
  598. + offsetof(WASMModuleInstanceExtra, functions);
  599. offset = I32_CONST(offset_functions);
  600. CHECK_LLVM_CONST(offset);
  601. if (!(functions = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  602. func_ctx->aot_inst, &offset, 1,
  603. "functions_addr"))) {
  604. aot_set_last_error("llvm build inbounds gep failed");
  605. return false;
  606. }
  607. if (!(functions = LLVMBuildBitCast(comp_ctx->builder, functions,
  608. int8_ptr_type, "functions_ptr"))) {
  609. aot_set_last_error("llvm build bitcast failed");
  610. return false;
  611. }
  612. if (!(functions = LLVMBuildLoad2(comp_ctx->builder, INT8_PTR_TYPE,
  613. functions, "functions"))) {
  614. aot_set_last_error("llvm build load failed");
  615. return false;
  616. }
  617. offset = I32_CONST(sizeof(WASMFunctionInstance) * func_idx);
  618. CHECK_LLVM_CONST(offset);
  619. if (!(func_inst =
  620. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, functions,
  621. &offset, 1, "func_inst"))) {
  622. aot_set_last_error("llvm build inbounds gep failed");
  623. return false;
  624. }
  625. offset = I32_CONST(offsetof(WASMInterpFrame, function));
  626. CHECK_LLVM_CONST(offset);
  627. if (!(func_inst_ptr =
  628. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, new_frame,
  629. &offset, 1, "func_inst_addr"))
  630. || !(func_inst_ptr =
  631. LLVMBuildBitCast(comp_ctx->builder, func_inst_ptr,
  632. int8_ptr_type, "func_inst_ptr"))) {
  633. aot_set_last_error("llvm get func_inst_ptr failed");
  634. return false;
  635. }
  636. if (!LLVMBuildStore(comp_ctx->builder, func_inst, func_inst_ptr)) {
  637. aot_set_last_error("llvm build store failed");
  638. return false;
  639. }
  640. }
  641. /* No need to initialize new_frame->sp and new_frame->ip_offset
  642. since they will be committed in AOT code if needed */
  643. /* exec_env->cur_frame = new_frame */
  644. if (!LLVMBuildStore(comp_ctx->builder, new_frame, cur_frame_ptr)) {
  645. aot_set_last_error("llvm build store failed");
  646. return false;
  647. }
  648. if (comp_ctx->enable_perf_profiling || comp_ctx->enable_memory_profiling) {
  649. LLVMTypeRef param_types[2], func_type, func_ptr_type;
  650. LLVMValueRef param_values[2], func, res;
  651. char *func_name = "aot_frame_update_profile_info";
  652. /* Call aot_frame_update_profile_info for AOT or
  653. llvm_jit_frame_update_profile_info for JIT */
  654. param_types[0] = comp_ctx->exec_env_type;
  655. param_types[1] = INT8_TYPE;
  656. if (!(func_type = LLVMFunctionType(VOID_TYPE, param_types, 2, false))) {
  657. aot_set_last_error("llvm add function type failed.");
  658. return false;
  659. }
  660. if (comp_ctx->is_jit_mode) {
  661. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  662. aot_set_last_error("create LLVM function type failed.");
  663. return false;
  664. }
  665. /* JIT mode, call the function directly */
  666. if (!(func = I64_CONST(
  667. (uint64)(uintptr_t)llvm_jit_frame_update_profile_info))
  668. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  669. aot_set_last_error("create LLVM value failed.");
  670. return false;
  671. }
  672. }
  673. else if (comp_ctx->is_indirect_mode) {
  674. int32 func_index;
  675. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  676. aot_set_last_error("create LLVM function type failed.");
  677. return false;
  678. }
  679. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  680. if (func_index < 0) {
  681. return false;
  682. }
  683. if (!(func =
  684. aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  685. func_ptr_type, func_index))) {
  686. return false;
  687. }
  688. }
  689. else {
  690. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  691. && !(func = LLVMAddFunction(func_ctx->module, func_name,
  692. func_type))) {
  693. aot_set_last_error("add LLVM function failed.");
  694. return false;
  695. }
  696. }
  697. param_values[0] = func_ctx->exec_env;
  698. param_values[1] = I8_ONE;
  699. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  700. param_values, 2, ""))) {
  701. aot_set_last_error("llvm build call failed.");
  702. return false;
  703. }
  704. }
  705. return true;
  706. fail:
  707. return false;
  708. }
  709. static bool
  710. free_frame_for_aot_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  711. {
  712. LLVMValueRef cur_frame_ptr = func_ctx->cur_frame_ptr, cur_frame;
  713. LLVMValueRef wasm_stack_top_ptr = func_ctx->wasm_stack_top_ptr;
  714. LLVMTypeRef int8_ptr_type;
  715. /* `int8 **` type */
  716. int8_ptr_type = LLVMPointerType(INT8_PTR_TYPE, 0);
  717. if (!int8_ptr_type) {
  718. aot_set_last_error("create llvm pointer type failed");
  719. return false;
  720. }
  721. if (comp_ctx->enable_perf_profiling) {
  722. LLVMTypeRef param_types[2], func_type, func_ptr_type;
  723. LLVMValueRef param_values[2], func, res;
  724. char *func_name = "aot_frame_update_profile_info";
  725. /* call aot_frame_update_profile_info for AOT or
  726. llvm_jit_frame_update_profile_info for JIT */
  727. param_types[0] = comp_ctx->exec_env_type;
  728. param_types[1] = INT8_TYPE;
  729. if (!(func_type = LLVMFunctionType(VOID_TYPE, param_types, 2, false))) {
  730. aot_set_last_error("llvm add function type failed.");
  731. return false;
  732. }
  733. if (comp_ctx->is_jit_mode) {
  734. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  735. aot_set_last_error("create LLVM function type failed.");
  736. return false;
  737. }
  738. /* JIT mode, call the function directly */
  739. if (!(func = I64_CONST(
  740. (uint64)(uintptr_t)llvm_jit_frame_update_profile_info))
  741. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  742. aot_set_last_error("create LLVM value failed.");
  743. return false;
  744. }
  745. }
  746. else if (comp_ctx->is_indirect_mode) {
  747. int32 func_index;
  748. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  749. aot_set_last_error("create LLVM function type failed.");
  750. return false;
  751. }
  752. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  753. if (func_index < 0) {
  754. return false;
  755. }
  756. if (!(func =
  757. aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  758. func_ptr_type, func_index))) {
  759. return false;
  760. }
  761. }
  762. else {
  763. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  764. && !(func = LLVMAddFunction(func_ctx->module, func_name,
  765. func_type))) {
  766. aot_set_last_error("add LLVM function failed.");
  767. return false;
  768. }
  769. }
  770. param_values[0] = func_ctx->exec_env;
  771. param_values[1] = I8_ZERO;
  772. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  773. param_values, 2, ""))) {
  774. aot_set_last_error("llvm build call failed.");
  775. return false;
  776. }
  777. }
  778. /* cur_frame = exec_env->cur_frame */
  779. if (!(cur_frame = LLVMBuildLoad2(comp_ctx->builder, INT8_PTR_TYPE,
  780. cur_frame_ptr, "cur_frame"))) {
  781. aot_set_last_error("llvm build load failed");
  782. return false;
  783. }
  784. /* exec_env->wasm_stack.top = cur_frame */
  785. if (!LLVMBuildStore(comp_ctx->builder, cur_frame, wasm_stack_top_ptr)) {
  786. aot_set_last_error("llvm build store failed");
  787. return false;
  788. }
  789. /* exec_env->cur_frame = prev_frame */
  790. if (!LLVMBuildStore(comp_ctx->builder, func_ctx->cur_frame,
  791. cur_frame_ptr)) {
  792. aot_set_last_error("llvm build store failed");
  793. return false;
  794. }
  795. return true;
  796. }
  797. #endif /* end of (WASM_ENABLE_DUMP_CALL_STACK != 0) \
  798. || (WASM_ENABLE_PERF_PROFILING != 0) */
  799. /**
  800. * Check whether the app address and its buffer are inside the linear memory,
  801. * if no, throw exception
  802. */
  803. static bool
  804. check_app_addr_and_convert(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  805. bool is_str_arg, LLVMValueRef app_addr,
  806. LLVMValueRef buf_size,
  807. LLVMValueRef *p_native_addr_converted)
  808. {
  809. LLVMTypeRef func_type, func_ptr_type, func_param_types[5];
  810. LLVMValueRef func, func_param_values[5], res, native_addr_ptr;
  811. char *func_name = "aot_check_app_addr_and_convert";
  812. /* prepare function type of aot_check_app_addr_and_convert */
  813. func_param_types[0] = comp_ctx->aot_inst_type; /* module_inst */
  814. func_param_types[1] = INT8_TYPE; /* is_str_arg */
  815. func_param_types[2] = I32_TYPE; /* app_offset */
  816. func_param_types[3] = I32_TYPE; /* buf_size */
  817. func_param_types[4] =
  818. comp_ctx->basic_types.int8_pptr_type; /* p_native_addr */
  819. if (!(func_type =
  820. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  821. aot_set_last_error("llvm add function type failed.");
  822. return false;
  823. }
  824. /* prepare function pointer */
  825. if (comp_ctx->is_jit_mode) {
  826. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  827. aot_set_last_error("create LLVM function type failed.");
  828. return false;
  829. }
  830. /* JIT mode, call the function directly */
  831. if (!(func =
  832. I64_CONST((uint64)(uintptr_t)jit_check_app_addr_and_convert))
  833. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  834. aot_set_last_error("create LLVM value failed.");
  835. return false;
  836. }
  837. }
  838. else if (comp_ctx->is_indirect_mode) {
  839. int32 func_index;
  840. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  841. aot_set_last_error("create LLVM function type failed.");
  842. return false;
  843. }
  844. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  845. if (func_index < 0) {
  846. return false;
  847. }
  848. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  849. func_ptr_type, func_index))) {
  850. return false;
  851. }
  852. }
  853. else {
  854. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  855. && !(func =
  856. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  857. aot_set_last_error("add LLVM function failed.");
  858. return false;
  859. }
  860. }
  861. if (!(native_addr_ptr = LLVMBuildBitCast(
  862. comp_ctx->builder, func_ctx->argv_buf,
  863. comp_ctx->basic_types.int8_pptr_type, "p_native_addr"))) {
  864. aot_set_last_error("llvm build bit cast failed.");
  865. return false;
  866. }
  867. func_param_values[0] = func_ctx->aot_inst;
  868. func_param_values[1] = I8_CONST(is_str_arg);
  869. func_param_values[2] = app_addr;
  870. func_param_values[3] = buf_size;
  871. func_param_values[4] = native_addr_ptr;
  872. if (!func_param_values[1]) {
  873. aot_set_last_error("llvm create const failed.");
  874. return false;
  875. }
  876. /* call aot_check_app_addr_and_convert() function */
  877. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  878. func_param_values, 5, "res"))) {
  879. aot_set_last_error("llvm build call failed.");
  880. return false;
  881. }
  882. /* Check whether exception was thrown when executing the function */
  883. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  884. && !check_call_return(comp_ctx, func_ctx, res)) {
  885. return false;
  886. }
  887. if (!(*p_native_addr_converted =
  888. LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, native_addr_ptr,
  889. "native_addr"))) {
  890. aot_set_last_error("llvm build load failed.");
  891. return false;
  892. }
  893. return true;
  894. }
  895. static void
  896. aot_estimate_and_record_stack_usage_for_function_call(
  897. const AOTCompContext *comp_ctx, AOTFuncContext *caller_func_ctx,
  898. const AOTFuncType *callee_func_type)
  899. {
  900. unsigned int size;
  901. if (!(comp_ctx->enable_stack_bound_check
  902. || comp_ctx->enable_stack_estimation)) {
  903. return;
  904. }
  905. size =
  906. aot_estimate_stack_usage_for_function_call(comp_ctx, callee_func_type);
  907. /*
  908. * only record the max value, assuming that LLVM emits machine code
  909. * which rewinds the stack before making the next call in the
  910. * function.
  911. */
  912. if (caller_func_ctx->stack_consumption_for_func_call < size) {
  913. caller_func_ctx->stack_consumption_for_func_call = size;
  914. }
  915. }
  916. static bool
  917. commit_params_to_frame_of_import_func(AOTCompContext *comp_ctx,
  918. AOTFuncContext *func_ctx,
  919. AOTFuncType *func_type,
  920. const LLVMValueRef *param_values)
  921. {
  922. uint32 i, n;
  923. for (i = 0, n = 0; i < func_type->param_count; i++, n++) {
  924. switch (func_type->types[i]) {
  925. case VALUE_TYPE_I32:
  926. if (!aot_frame_store_value(
  927. comp_ctx, param_values[i], VALUE_TYPE_I32,
  928. func_ctx->cur_frame,
  929. offset_of_local_in_outs_area(comp_ctx, n)))
  930. return false;
  931. break;
  932. case VALUE_TYPE_I64:
  933. if (!aot_frame_store_value(
  934. comp_ctx, param_values[i], VALUE_TYPE_I64,
  935. func_ctx->cur_frame,
  936. offset_of_local_in_outs_area(comp_ctx, n)))
  937. return false;
  938. n++;
  939. break;
  940. case VALUE_TYPE_F32:
  941. if (!aot_frame_store_value(
  942. comp_ctx, param_values[i], VALUE_TYPE_F32,
  943. func_ctx->cur_frame,
  944. offset_of_local_in_outs_area(comp_ctx, n)))
  945. return false;
  946. break;
  947. case VALUE_TYPE_F64:
  948. if (!aot_frame_store_value(
  949. comp_ctx, param_values[i], VALUE_TYPE_F64,
  950. func_ctx->cur_frame,
  951. offset_of_local_in_outs_area(comp_ctx, n)))
  952. return false;
  953. n++;
  954. break;
  955. case VALUE_TYPE_FUNCREF:
  956. case VALUE_TYPE_EXTERNREF:
  957. if (comp_ctx->enable_ref_types) {
  958. if (!aot_frame_store_value(
  959. comp_ctx, param_values[i], VALUE_TYPE_I32,
  960. func_ctx->cur_frame,
  961. offset_of_local_in_outs_area(comp_ctx, n)))
  962. return false;
  963. }
  964. #if WASM_ENABLE_GC != 0
  965. else if (comp_ctx->enable_gc) {
  966. if (!aot_frame_store_value(
  967. comp_ctx, param_values[i], VALUE_TYPE_GC_REF,
  968. func_ctx->cur_frame,
  969. offset_of_local_in_outs_area(comp_ctx, n)))
  970. return false;
  971. if (comp_ctx->pointer_size == sizeof(uint64))
  972. n++;
  973. }
  974. #endif
  975. else {
  976. bh_assert(0);
  977. }
  978. break;
  979. #if WASM_ENABLE_GC != 0
  980. case REF_TYPE_NULLFUNCREF:
  981. case REF_TYPE_NULLEXTERNREF:
  982. case REF_TYPE_NULLREF:
  983. /* case REF_TYPE_FUNCREF: */
  984. /* case REF_TYPE_EXTERNREF: */
  985. case REF_TYPE_ANYREF:
  986. case REF_TYPE_EQREF:
  987. case REF_TYPE_HT_NULLABLE:
  988. case REF_TYPE_HT_NON_NULLABLE:
  989. case REF_TYPE_I31REF:
  990. case REF_TYPE_STRUCTREF:
  991. case REF_TYPE_ARRAYREF:
  992. case VALUE_TYPE_GC_REF:
  993. #if WASM_ENABLE_STRINGREF != 0
  994. case REF_TYPE_STRINGREF:
  995. case REF_TYPE_STRINGVIEWWTF8:
  996. case REF_TYPE_STRINGVIEWWTF16:
  997. case REF_TYPE_STRINGVIEWITER:
  998. #endif
  999. if (!aot_frame_store_value(
  1000. comp_ctx, param_values[i], VALUE_TYPE_GC_REF,
  1001. func_ctx->cur_frame,
  1002. offset_of_local_in_outs_area(comp_ctx, n)))
  1003. return false;
  1004. if (comp_ctx->pointer_size == sizeof(uint64))
  1005. n++;
  1006. break;
  1007. #endif
  1008. default:
  1009. bh_assert(0);
  1010. break;
  1011. }
  1012. }
  1013. return true;
  1014. }
  1015. bool
  1016. aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1017. uint32 func_idx, bool tail_call, const uint8 *frame_ip_call)
  1018. {
  1019. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  1020. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  1021. uint32 func_count = comp_ctx->func_ctx_count, param_cell_num = 0;
  1022. uint32 ext_ret_cell_num = 0, cell_num = 0;
  1023. AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
  1024. AOTFuncType *func_type;
  1025. LLVMTypeRef *param_types = NULL, ret_type;
  1026. LLVMTypeRef ext_ret_ptr_type;
  1027. LLVMValueRef *param_values = NULL, value_ret = NULL, func;
  1028. LLVMValueRef import_func_idx, res;
  1029. LLVMValueRef ext_ret, ext_ret_ptr, ext_ret_idx;
  1030. int32 i, j = 0, param_count, result_count, ext_ret_count;
  1031. uint64 total_size;
  1032. uint8 wasm_ret_type;
  1033. uint8 *ext_ret_types = NULL;
  1034. const char *signature = NULL;
  1035. bool ret = false;
  1036. char buf[32];
  1037. /* Check function index */
  1038. if (func_idx >= import_func_count + func_count) {
  1039. aot_set_last_error("Function index out of range.");
  1040. return false;
  1041. }
  1042. /* Get function type */
  1043. if (func_idx < import_func_count) {
  1044. func_type = import_funcs[func_idx].func_type;
  1045. signature = import_funcs[func_idx].signature;
  1046. }
  1047. else {
  1048. func_type =
  1049. func_ctxes[func_idx - import_func_count]->aot_func->func_type;
  1050. }
  1051. aot_estimate_and_record_stack_usage_for_function_call(comp_ctx, func_ctx,
  1052. func_type);
  1053. /* Commit stack operands, sp and ip */
  1054. if (comp_ctx->aot_frame) {
  1055. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  1056. return false;
  1057. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, comp_ctx->aot_frame->sp,
  1058. frame_ip_call))
  1059. return false;
  1060. }
  1061. #if WASM_ENABLE_THREAD_MGR != 0
  1062. /* Insert suspend check point */
  1063. if (comp_ctx->enable_thread_mgr) {
  1064. if (!check_suspend_flags(comp_ctx, func_ctx, true))
  1065. return false;
  1066. }
  1067. #endif
  1068. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1069. if (comp_ctx->enable_aux_stack_frame) {
  1070. if (!alloc_frame_for_aot_func(comp_ctx, func_ctx, func_idx))
  1071. return false;
  1072. }
  1073. #endif
  1074. /* Get param cell number */
  1075. param_cell_num = func_type->param_cell_num;
  1076. /* Allocate memory for parameters.
  1077. * Parameters layout:
  1078. * - exec env
  1079. * - wasm function's parameters
  1080. * - extra results'(except the first one) addresses
  1081. */
  1082. param_count = (int32)func_type->param_count;
  1083. result_count = (int32)func_type->result_count;
  1084. ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  1085. total_size =
  1086. sizeof(LLVMValueRef) * (uint64)(param_count + 1 + ext_ret_count);
  1087. if (total_size >= UINT32_MAX
  1088. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  1089. aot_set_last_error("allocate memory failed.");
  1090. return false;
  1091. }
  1092. /* First parameter is exec env */
  1093. param_values[j++] = func_ctx->exec_env;
  1094. /* Pop parameters from stack */
  1095. for (i = param_count - 1; i >= 0; i--)
  1096. POP(param_values[i + j], func_type->types[i]);
  1097. /* Set parameters for multiple return values, the first return value
  1098. is returned by function return value, and the other return values
  1099. are returned by function parameters with pointer types */
  1100. if (ext_ret_count > 0) {
  1101. ext_ret_types = func_type->types + param_count + 1;
  1102. ext_ret_cell_num = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  1103. if (ext_ret_cell_num > 64) {
  1104. aot_set_last_error("prepare extra results's return "
  1105. "address arguments failed: "
  1106. "maximum 64 parameter cell number supported.");
  1107. goto fail;
  1108. }
  1109. for (i = 0; i < ext_ret_count; i++) {
  1110. if (!(ext_ret_idx = I32_CONST(cell_num))
  1111. || !(ext_ret_ptr_type =
  1112. LLVMPointerType(TO_LLVM_TYPE(ext_ret_types[i]), 0))) {
  1113. aot_set_last_error("llvm add const or pointer type failed.");
  1114. goto fail;
  1115. }
  1116. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i);
  1117. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(
  1118. comp_ctx->builder, I32_TYPE, func_ctx->argv_buf,
  1119. &ext_ret_idx, 1, buf))) {
  1120. aot_set_last_error("llvm build GEP failed.");
  1121. goto fail;
  1122. }
  1123. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i);
  1124. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  1125. ext_ret_ptr_type, buf))) {
  1126. aot_set_last_error("llvm build bit cast failed.");
  1127. goto fail;
  1128. }
  1129. param_values[param_count + 1 + i] = ext_ret_ptr;
  1130. cell_num += wasm_value_type_cell_num_internal(
  1131. ext_ret_types[i], comp_ctx->pointer_size);
  1132. }
  1133. }
  1134. if (func_idx < import_func_count) {
  1135. if (comp_ctx->enable_aux_stack_frame
  1136. && !commit_params_to_frame_of_import_func(
  1137. comp_ctx, func_ctx, func_type, param_values + 1)) {
  1138. goto fail;
  1139. }
  1140. if (!(import_func_idx = I32_CONST(func_idx))) {
  1141. aot_set_last_error("llvm build inbounds gep failed.");
  1142. goto fail;
  1143. }
  1144. /* Initialize parameter types of the LLVM function */
  1145. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  1146. if (total_size >= UINT32_MAX
  1147. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  1148. aot_set_last_error("allocate memory failed.");
  1149. goto fail;
  1150. }
  1151. j = 0;
  1152. param_types[j++] = comp_ctx->exec_env_type;
  1153. for (i = 0; i < param_count; i++, j++) {
  1154. param_types[j] = TO_LLVM_TYPE(func_type->types[i]);
  1155. /* If the signature can be gotten, e.g. the signature of the builtin
  1156. native libraries, just check the app offset and buf size, and
  1157. then convert app offset to native addr and call the native func
  1158. directly, no need to call aot_invoke_native to call it */
  1159. if (signature) {
  1160. LLVMValueRef native_addr, native_addr_size;
  1161. if (signature[i + 1] == '*' || signature[i + 1] == '$') {
  1162. param_types[j] = INT8_PTR_TYPE;
  1163. }
  1164. if (signature[i + 1] == '*') {
  1165. if (signature[i + 2] == '~')
  1166. native_addr_size = param_values[i + 2];
  1167. else
  1168. native_addr_size = I32_ONE;
  1169. if (!check_app_addr_and_convert(
  1170. comp_ctx, func_ctx, false, param_values[j],
  1171. native_addr_size, &native_addr)) {
  1172. goto fail;
  1173. }
  1174. param_values[j] = native_addr;
  1175. }
  1176. else if (signature[i + 1] == '$') {
  1177. native_addr_size = I32_ZERO;
  1178. if (!check_app_addr_and_convert(
  1179. comp_ctx, func_ctx, true, param_values[j],
  1180. native_addr_size, &native_addr)) {
  1181. goto fail;
  1182. }
  1183. param_values[j] = native_addr;
  1184. }
  1185. }
  1186. }
  1187. if (func_type->result_count) {
  1188. wasm_ret_type = func_type->types[func_type->param_count];
  1189. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  1190. }
  1191. else {
  1192. wasm_ret_type = VALUE_TYPE_VOID;
  1193. ret_type = VOID_TYPE;
  1194. }
  1195. if (!signature) {
  1196. /* call aot_invoke_native() */
  1197. if (!call_aot_invoke_native_func(
  1198. comp_ctx, func_ctx, import_func_idx, func_type,
  1199. param_types + 1, param_values + 1, param_count,
  1200. param_cell_num, ret_type, wasm_ret_type, &value_ret, &res))
  1201. goto fail;
  1202. /* Check whether there was exception thrown when executing
  1203. the function */
  1204. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1205. && !check_call_return(comp_ctx, func_ctx, res))
  1206. goto fail;
  1207. }
  1208. else { /* call native func directly */
  1209. LLVMTypeRef native_func_type, func_ptr_type;
  1210. LLVMValueRef func_ptr;
  1211. if (!(native_func_type = LLVMFunctionType(
  1212. ret_type, param_types, param_count + 1, false))) {
  1213. aot_set_last_error("llvm add function type failed.");
  1214. goto fail;
  1215. }
  1216. if (!(func_ptr_type = LLVMPointerType(native_func_type, 0))) {
  1217. aot_set_last_error("create LLVM function type failed.");
  1218. goto fail;
  1219. }
  1220. /* Load function pointer */
  1221. if (!(func_ptr = LLVMBuildInBoundsGEP2(
  1222. comp_ctx->builder, OPQ_PTR_TYPE, func_ctx->func_ptrs,
  1223. &import_func_idx, 1, "native_func_ptr_tmp"))) {
  1224. aot_set_last_error("llvm build inbounds gep failed.");
  1225. goto fail;
  1226. }
  1227. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE,
  1228. func_ptr, "native_func_ptr"))) {
  1229. aot_set_last_error("llvm build load failed.");
  1230. goto fail;
  1231. }
  1232. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  1233. func_ptr_type, "native_func"))) {
  1234. aot_set_last_error("llvm bit cast failed.");
  1235. goto fail;
  1236. }
  1237. /* Call the function */
  1238. if (!(value_ret = LLVMBuildCall2(
  1239. comp_ctx->builder, native_func_type, func, param_values,
  1240. (uint32)param_count + 1 + ext_ret_count,
  1241. (func_type->result_count > 0 ? "call" : "")))) {
  1242. aot_set_last_error("LLVM build call failed.");
  1243. goto fail;
  1244. }
  1245. /* Check whether there was exception thrown when executing
  1246. the function */
  1247. if (!check_exception_thrown(comp_ctx, func_ctx)) {
  1248. goto fail;
  1249. }
  1250. }
  1251. }
  1252. else {
  1253. #if LLVM_VERSION_MAJOR >= 14
  1254. LLVMTypeRef llvm_func_type;
  1255. #endif
  1256. if (comp_ctx->is_indirect_mode) {
  1257. LLVMTypeRef func_ptr_type;
  1258. if (!(func_ptr_type = LLVMPointerType(
  1259. func_ctxes[func_idx - import_func_count]->func_type,
  1260. 0))) {
  1261. aot_set_last_error("construct func ptr type failed.");
  1262. goto fail;
  1263. }
  1264. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->func_ptrs,
  1265. func_ptr_type, func_idx))) {
  1266. goto fail;
  1267. }
  1268. }
  1269. else {
  1270. if (func_ctxes[func_idx - import_func_count] == func_ctx) {
  1271. /* recursive call */
  1272. func = func_ctx->precheck_func;
  1273. }
  1274. else {
  1275. if (!comp_ctx->is_jit_mode) {
  1276. func =
  1277. func_ctxes[func_idx - import_func_count]->precheck_func;
  1278. }
  1279. else {
  1280. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  1281. func =
  1282. func_ctxes[func_idx - import_func_count]->precheck_func;
  1283. #else
  1284. /* JIT tier-up, load func ptr from func_ptrs[func_idx] */
  1285. LLVMValueRef func_ptr, func_idx_const;
  1286. LLVMTypeRef func_ptr_type;
  1287. if (!(func_idx_const = I32_CONST(func_idx))) {
  1288. aot_set_last_error("llvm build const failed.");
  1289. goto fail;
  1290. }
  1291. if (!(func_ptr = LLVMBuildInBoundsGEP2(
  1292. comp_ctx->builder, OPQ_PTR_TYPE,
  1293. func_ctx->func_ptrs, &func_idx_const, 1,
  1294. "func_ptr_tmp"))) {
  1295. aot_set_last_error("llvm build inbounds gep failed.");
  1296. goto fail;
  1297. }
  1298. if (!(func_ptr =
  1299. LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE,
  1300. func_ptr, "func_ptr"))) {
  1301. aot_set_last_error("llvm build load failed.");
  1302. goto fail;
  1303. }
  1304. if (!(func_ptr_type = LLVMPointerType(
  1305. func_ctxes[func_idx - import_func_count]
  1306. ->func_type,
  1307. 0))) {
  1308. aot_set_last_error("construct func ptr type failed.");
  1309. goto fail;
  1310. }
  1311. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  1312. func_ptr_type,
  1313. "indirect_func"))) {
  1314. aot_set_last_error("llvm build bit cast failed.");
  1315. goto fail;
  1316. }
  1317. #endif /* end of !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0) */
  1318. }
  1319. }
  1320. }
  1321. #if LLVM_VERSION_MAJOR >= 14
  1322. llvm_func_type = func_ctxes[func_idx - import_func_count]->func_type;
  1323. #endif
  1324. /* Call the function */
  1325. if (!(value_ret = LLVMBuildCall2(
  1326. comp_ctx->builder, llvm_func_type, func, param_values,
  1327. (uint32)param_count + 1 + ext_ret_count,
  1328. (func_type->result_count > 0 ? "call" : "")))) {
  1329. aot_set_last_error("LLVM build call failed.");
  1330. goto fail;
  1331. }
  1332. /* Set calling convention for the call with the func's calling
  1333. convention */
  1334. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  1335. if (tail_call)
  1336. LLVMSetTailCall(value_ret, true);
  1337. /* Check whether there was exception thrown when executing
  1338. the function */
  1339. if (!tail_call
  1340. && (comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1341. && !check_exception_thrown(comp_ctx, func_ctx))
  1342. goto fail;
  1343. }
  1344. if (func_type->result_count > 0) {
  1345. /* Push the first result to stack */
  1346. PUSH(value_ret, func_type->types[func_type->param_count]);
  1347. /* Load extra result from its address and push to stack */
  1348. for (i = 0; i < ext_ret_count; i++) {
  1349. snprintf(buf, sizeof(buf), "func%d_ext_ret%d", func_idx, i);
  1350. if (!(ext_ret = LLVMBuildLoad2(
  1351. comp_ctx->builder, TO_LLVM_TYPE(ext_ret_types[i]),
  1352. param_values[1 + param_count + i], buf))) {
  1353. aot_set_last_error("llvm build load failed.");
  1354. goto fail;
  1355. }
  1356. PUSH(ext_ret, ext_ret_types[i]);
  1357. }
  1358. }
  1359. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1360. if (comp_ctx->enable_aux_stack_frame) {
  1361. if (!free_frame_for_aot_func(comp_ctx, func_ctx))
  1362. goto fail;
  1363. }
  1364. #endif
  1365. #if WASM_ENABLE_THREAD_MGR != 0
  1366. /* Insert suspend check point */
  1367. if (comp_ctx->enable_thread_mgr) {
  1368. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  1369. goto fail;
  1370. }
  1371. #endif
  1372. ret = true;
  1373. fail:
  1374. if (param_types)
  1375. wasm_runtime_free(param_types);
  1376. if (param_values)
  1377. wasm_runtime_free(param_values);
  1378. return ret;
  1379. }
  1380. static bool
  1381. call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1382. AOTFuncType *aot_func_type,
  1383. LLVMValueRef func_type_idx, LLVMValueRef table_idx,
  1384. LLVMValueRef table_elem_idx,
  1385. LLVMTypeRef *param_types,
  1386. LLVMValueRef *param_values, uint32 param_count,
  1387. uint32 param_cell_num, uint32 result_count,
  1388. uint8 *wasm_ret_types, LLVMValueRef *value_rets,
  1389. LLVMValueRef *p_res)
  1390. {
  1391. LLVMTypeRef func_type, func_ptr_type, func_param_types[6];
  1392. LLVMTypeRef ret_type, ret_ptr_type, elem_ptr_type;
  1393. LLVMValueRef func, ret_idx, ret_ptr, elem_idx, elem_ptr;
  1394. LLVMValueRef func_param_values[6], res = NULL;
  1395. char buf[32], *func_name = "aot_call_indirect";
  1396. uint32 i, cell_num = 0, ret_cell_num, argv_cell_num;
  1397. /* prepare function type of aot_call_indirect */
  1398. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  1399. func_param_types[1] = I32_TYPE; /* table_idx */
  1400. func_param_types[2] = I32_TYPE; /* table_elem_idx */
  1401. func_param_types[3] = I32_TYPE; /* argc */
  1402. func_param_types[4] = INT32_PTR_TYPE; /* argv */
  1403. if (!(func_type =
  1404. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  1405. aot_set_last_error("llvm add function type failed.");
  1406. return false;
  1407. }
  1408. /* prepare function pointer */
  1409. if (comp_ctx->is_jit_mode) {
  1410. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1411. aot_set_last_error("create LLVM function type failed.");
  1412. return false;
  1413. }
  1414. /* JIT mode, call the function directly */
  1415. if (!(func = I64_CONST((uint64)(uintptr_t)llvm_jit_call_indirect))
  1416. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  1417. aot_set_last_error("create LLVM value failed.");
  1418. return false;
  1419. }
  1420. }
  1421. else if (comp_ctx->is_indirect_mode) {
  1422. int32 func_index;
  1423. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1424. aot_set_last_error("create LLVM function type failed.");
  1425. return false;
  1426. }
  1427. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  1428. if (func_index < 0) {
  1429. return false;
  1430. }
  1431. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1432. func_ptr_type, func_index))) {
  1433. return false;
  1434. }
  1435. }
  1436. else {
  1437. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  1438. && !(func =
  1439. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  1440. aot_set_last_error("add LLVM function failed.");
  1441. return false;
  1442. }
  1443. }
  1444. ret_cell_num = wasm_get_cell_num(wasm_ret_types, result_count);
  1445. argv_cell_num =
  1446. param_cell_num > ret_cell_num ? param_cell_num : ret_cell_num;
  1447. if (argv_cell_num > 64) {
  1448. aot_set_last_error("prepare native arguments failed: "
  1449. "maximum 64 parameter cell number supported.");
  1450. return false;
  1451. }
  1452. /* prepare frame_lp */
  1453. for (i = 0; i < param_count; i++) {
  1454. if (!(elem_idx = I32_CONST(cell_num))
  1455. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  1456. aot_set_last_error("llvm add const or pointer type failed.");
  1457. return false;
  1458. }
  1459. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  1460. if (!(elem_ptr =
  1461. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  1462. func_ctx->argv_buf, &elem_idx, 1, buf))
  1463. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  1464. elem_ptr_type, buf))) {
  1465. aot_set_last_error("llvm build bit cast failed.");
  1466. return false;
  1467. }
  1468. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  1469. elem_ptr))) {
  1470. aot_set_last_error("llvm build store failed.");
  1471. return false;
  1472. }
  1473. LLVMSetAlignment(res, 1);
  1474. cell_num += wasm_value_type_cell_num_internal(aot_func_type->types[i],
  1475. comp_ctx->pointer_size);
  1476. }
  1477. func_param_values[0] = func_ctx->exec_env;
  1478. func_param_values[1] = table_idx;
  1479. func_param_values[2] = table_elem_idx;
  1480. func_param_values[3] = I32_CONST(param_cell_num);
  1481. func_param_values[4] = func_ctx->argv_buf;
  1482. if (!func_param_values[3]) {
  1483. aot_set_last_error("llvm create const failed.");
  1484. return false;
  1485. }
  1486. /* call aot_call_indirect() function */
  1487. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1488. func_param_values, 5, "res"))) {
  1489. aot_set_last_error("llvm build call failed.");
  1490. return false;
  1491. }
  1492. /* get function result values */
  1493. cell_num = 0;
  1494. for (i = 0; i < result_count; i++) {
  1495. ret_type = TO_LLVM_TYPE(wasm_ret_types[i]);
  1496. if (!(ret_idx = I32_CONST(cell_num))
  1497. || !(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  1498. aot_set_last_error("llvm add const or pointer type failed.");
  1499. return false;
  1500. }
  1501. snprintf(buf, sizeof(buf), "argv_ret%d", i);
  1502. if (!(ret_ptr =
  1503. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  1504. func_ctx->argv_buf, &ret_idx, 1, buf))
  1505. || !(ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ret_ptr,
  1506. ret_ptr_type, buf))) {
  1507. aot_set_last_error("llvm build GEP or bit cast failed.");
  1508. return false;
  1509. }
  1510. snprintf(buf, sizeof(buf), "ret%d", i);
  1511. if (!(value_rets[i] =
  1512. LLVMBuildLoad2(comp_ctx->builder, ret_type, ret_ptr, buf))) {
  1513. aot_set_last_error("llvm build load failed.");
  1514. return false;
  1515. }
  1516. cell_num += wasm_value_type_cell_num_internal(wasm_ret_types[i],
  1517. comp_ctx->pointer_size);
  1518. }
  1519. *p_res = res;
  1520. return true;
  1521. }
  1522. bool
  1523. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1524. uint32 type_idx, uint32 tbl_idx,
  1525. const uint8 *frame_ip_call_indirect)
  1526. {
  1527. AOTFuncType *func_type;
  1528. LLVMValueRef tbl_idx_value, elem_idx, func_idx;
  1529. LLVMValueRef table_elem_base, table_elem_addr, table_elem;
  1530. LLVMValueRef ftype_idx_ptr, ftype_idx, ftype_idx_const;
  1531. LLVMValueRef cmp_func_obj, cmp_elem_idx, cmp_func_idx, cmp_ftype_idx;
  1532. LLVMValueRef func, func_ptr, table_size_const;
  1533. LLVMValueRef ext_ret_offset, ext_ret_ptr, ext_ret, res;
  1534. LLVMValueRef *param_values = NULL, *value_rets = NULL;
  1535. LLVMValueRef *result_phis = NULL, value_ret, import_func_count;
  1536. LLVMTypeRef *param_types = NULL, ret_type;
  1537. LLVMTypeRef llvm_func_type, llvm_func_ptr_type;
  1538. LLVMTypeRef ext_ret_ptr_type;
  1539. LLVMBasicBlockRef check_func_obj_succ, check_elem_idx_succ,
  1540. check_ftype_idx_succ;
  1541. LLVMBasicBlockRef check_func_idx_succ, block_return, block_curr;
  1542. LLVMBasicBlockRef block_call_import, block_call_non_import;
  1543. LLVMValueRef offset;
  1544. uint32 total_param_count, func_param_count, func_result_count;
  1545. uint32 ext_cell_num, param_cell_num, i, j;
  1546. uint8 wasm_ret_type, *wasm_ret_types;
  1547. uint64 total_size;
  1548. char buf[32];
  1549. bool ret = false;
  1550. /* Check function type index */
  1551. if (type_idx >= comp_ctx->comp_data->type_count) {
  1552. aot_set_last_error("function type index out of range");
  1553. return false;
  1554. }
  1555. /* Find the equivalent function type whose type index is the smallest:
  1556. the callee function's type index is also converted to the smallest
  1557. one in wasm loader, so we can just check whether the two type indexes
  1558. are equal (the type index of call_indirect opcode and callee func),
  1559. we don't need to check whether the whole function types are equal,
  1560. including param types and result types. */
  1561. type_idx =
  1562. wasm_get_smallest_type_idx((WASMTypePtr *)comp_ctx->comp_data->types,
  1563. comp_ctx->comp_data->type_count, type_idx);
  1564. ftype_idx_const = I32_CONST(type_idx);
  1565. CHECK_LLVM_CONST(ftype_idx_const);
  1566. func_type = (AOTFuncType *)comp_ctx->comp_data->types[type_idx];
  1567. aot_estimate_and_record_stack_usage_for_function_call(comp_ctx, func_ctx,
  1568. func_type);
  1569. /* Commit stack operands, sp and ip */
  1570. if (comp_ctx->aot_frame) {
  1571. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  1572. return false;
  1573. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, comp_ctx->aot_frame->sp,
  1574. frame_ip_call_indirect))
  1575. return false;
  1576. }
  1577. #if WASM_ENABLE_THREAD_MGR != 0
  1578. /* Insert suspend check point */
  1579. if (comp_ctx->enable_thread_mgr) {
  1580. if (!check_suspend_flags(comp_ctx, func_ctx, true))
  1581. return false;
  1582. }
  1583. #endif
  1584. func_param_count = func_type->param_count;
  1585. func_result_count = func_type->result_count;
  1586. POP_I32(elem_idx);
  1587. /* get the cur size of the table instance */
  1588. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  1589. + offsetof(AOTTableInstance, cur_size)))) {
  1590. HANDLE_FAILURE("LLVMConstInt");
  1591. goto fail;
  1592. }
  1593. if (!(table_size_const = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1594. func_ctx->aot_inst, &offset,
  1595. 1, "cur_size_i8p"))) {
  1596. HANDLE_FAILURE("LLVMBuildGEP");
  1597. goto fail;
  1598. }
  1599. if (!(table_size_const =
  1600. LLVMBuildBitCast(comp_ctx->builder, table_size_const,
  1601. INT32_PTR_TYPE, "cur_siuze_i32p"))) {
  1602. HANDLE_FAILURE("LLVMBuildBitCast");
  1603. goto fail;
  1604. }
  1605. if (!(table_size_const = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE,
  1606. table_size_const, "cur_size"))) {
  1607. HANDLE_FAILURE("LLVMBuildLoad");
  1608. goto fail;
  1609. }
  1610. /* Check if (uint32)elem index >= table size */
  1611. if (!(cmp_elem_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGE, elem_idx,
  1612. table_size_const, "cmp_elem_idx"))) {
  1613. aot_set_last_error("llvm build icmp failed.");
  1614. goto fail;
  1615. }
  1616. /* Throw exception if elem index >= table size */
  1617. if (!(check_elem_idx_succ = LLVMAppendBasicBlockInContext(
  1618. comp_ctx->context, func_ctx->func, "check_elem_idx_succ"))) {
  1619. aot_set_last_error("llvm add basic block failed.");
  1620. goto fail;
  1621. }
  1622. LLVMMoveBasicBlockAfter(check_elem_idx_succ,
  1623. LLVMGetInsertBlock(comp_ctx->builder));
  1624. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNDEFINED_ELEMENT, true,
  1625. cmp_elem_idx, check_elem_idx_succ)))
  1626. goto fail;
  1627. /* load data as i32* */
  1628. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  1629. + offsetof(AOTTableInstance, elems)))) {
  1630. HANDLE_FAILURE("LLVMConstInt");
  1631. goto fail;
  1632. }
  1633. if (!(table_elem_base = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1634. func_ctx->aot_inst, &offset,
  1635. 1, "table_elem_base_i8p"))) {
  1636. aot_set_last_error("llvm build add failed.");
  1637. goto fail;
  1638. }
  1639. /* Load function index */
  1640. if (comp_ctx->enable_gc) {
  1641. /* table elem is func_obj when gc is enabled */
  1642. if (!(table_elem_base =
  1643. LLVMBuildBitCast(comp_ctx->builder, table_elem_base,
  1644. GC_REF_PTR_TYPE, "table_elem_base"))) {
  1645. HANDLE_FAILURE("LLVMBuildBitCast");
  1646. goto fail;
  1647. }
  1648. if (!(table_elem_addr = LLVMBuildInBoundsGEP2(
  1649. comp_ctx->builder, GC_REF_TYPE, table_elem_base, &elem_idx, 1,
  1650. "table_elem_addr"))) {
  1651. HANDLE_FAILURE("LLVMBuildNUWAdd");
  1652. goto fail;
  1653. }
  1654. if (!(table_elem = LLVMBuildLoad2(comp_ctx->builder, GC_REF_TYPE,
  1655. table_elem_addr, "table_elem"))) {
  1656. aot_set_last_error("llvm build load failed.");
  1657. goto fail;
  1658. }
  1659. /* Check if func object is NULL */
  1660. if (!(cmp_func_obj = LLVMBuildIsNull(comp_ctx->builder, table_elem,
  1661. "cmp_func_obj"))) {
  1662. aot_set_last_error("llvm build isnull failed.");
  1663. goto fail;
  1664. }
  1665. /* Throw exception if func object is NULL */
  1666. if (!(check_func_obj_succ = LLVMAppendBasicBlockInContext(
  1667. comp_ctx->context, func_ctx->func, "check_func_obj_succ"))) {
  1668. aot_set_last_error("llvm add basic block failed.");
  1669. goto fail;
  1670. }
  1671. LLVMMoveBasicBlockAfter(check_func_obj_succ,
  1672. LLVMGetInsertBlock(comp_ctx->builder));
  1673. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNINITIALIZED_ELEMENT,
  1674. true, cmp_func_obj, check_func_obj_succ)))
  1675. goto fail;
  1676. /* Get the func idx bound of the WASMFuncObject, the offset may be
  1677. * different in 32-bit runtime and 64-bit runtime since WASMObjectHeader
  1678. * is uintptr_t. Use comp_ctx->pointer_size as the
  1679. * offsetof(WASMFuncObject, func_idx_bound)
  1680. */
  1681. if (!(offset = I32_CONST(comp_ctx->pointer_size))) {
  1682. HANDLE_FAILURE("LLVMConstInt");
  1683. goto fail;
  1684. }
  1685. if (!(func_idx = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1686. table_elem, &offset, 1,
  1687. "func_idx_bound_i8p"))) {
  1688. HANDLE_FAILURE("LLVMBuildGEP");
  1689. goto fail;
  1690. }
  1691. if (!(func_idx =
  1692. LLVMBuildBitCast(comp_ctx->builder, func_idx, INT32_PTR_TYPE,
  1693. "func_idx_bound_i32p"))) {
  1694. HANDLE_FAILURE("LLVMBuildBitCast");
  1695. goto fail;
  1696. }
  1697. if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, func_idx,
  1698. "func_idx_bound"))) {
  1699. HANDLE_FAILURE("LLVMBuildLoad");
  1700. goto fail;
  1701. }
  1702. }
  1703. else {
  1704. if (!(table_elem_base =
  1705. LLVMBuildBitCast(comp_ctx->builder, table_elem_base,
  1706. INTPTR_T_PTR_TYPE, "table_elem_base"))) {
  1707. HANDLE_FAILURE("LLVMBuildBitCast");
  1708. goto fail;
  1709. }
  1710. if (!(table_elem_addr = LLVMBuildInBoundsGEP2(
  1711. comp_ctx->builder, INTPTR_T_TYPE, table_elem_base, &elem_idx,
  1712. 1, "table_elem_addr"))) {
  1713. HANDLE_FAILURE("LLVMBuildNUWAdd");
  1714. goto fail;
  1715. }
  1716. if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, INTPTR_T_TYPE,
  1717. table_elem_addr, "func_idx"))) {
  1718. aot_set_last_error("llvm build load failed.");
  1719. goto fail;
  1720. }
  1721. if (!(func_idx = LLVMBuildIntCast2(comp_ctx->builder, func_idx,
  1722. I32_TYPE, true, "func_idx_i32"))) {
  1723. aot_set_last_error("llvm build int cast failed.");
  1724. goto fail;
  1725. }
  1726. /* Check if func_idx == -1 */
  1727. if (!(cmp_func_idx =
  1728. LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, func_idx,
  1729. I32_NEG_ONE, "cmp_func_idx"))) {
  1730. aot_set_last_error("llvm build icmp failed.");
  1731. goto fail;
  1732. }
  1733. /* Throw exception if func_idx == -1 */
  1734. if (!(check_func_idx_succ = LLVMAppendBasicBlockInContext(
  1735. comp_ctx->context, func_ctx->func, "check_func_idx_succ"))) {
  1736. aot_set_last_error("llvm add basic block failed.");
  1737. goto fail;
  1738. }
  1739. LLVMMoveBasicBlockAfter(check_func_idx_succ,
  1740. LLVMGetInsertBlock(comp_ctx->builder));
  1741. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNINITIALIZED_ELEMENT,
  1742. true, cmp_func_idx, check_func_idx_succ)))
  1743. goto fail;
  1744. }
  1745. /* Load function type index */
  1746. if (!(ftype_idx_ptr = LLVMBuildInBoundsGEP2(
  1747. comp_ctx->builder, I32_TYPE, func_ctx->func_type_indexes,
  1748. &func_idx, 1, "ftype_idx_ptr"))) {
  1749. aot_set_last_error("llvm build inbounds gep failed.");
  1750. goto fail;
  1751. }
  1752. if (!(ftype_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, ftype_idx_ptr,
  1753. "ftype_idx"))) {
  1754. aot_set_last_error("llvm build load failed.");
  1755. goto fail;
  1756. }
  1757. /* Check if function type index not equal */
  1758. if (!(cmp_ftype_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, ftype_idx,
  1759. ftype_idx_const, "cmp_ftype_idx"))) {
  1760. aot_set_last_error("llvm build icmp failed.");
  1761. goto fail;
  1762. }
  1763. /* Throw exception if ftype_idx != ftype_idx_const */
  1764. if (!(check_ftype_idx_succ = LLVMAppendBasicBlockInContext(
  1765. comp_ctx->context, func_ctx->func, "check_ftype_idx_succ"))) {
  1766. aot_set_last_error("llvm add basic block failed.");
  1767. goto fail;
  1768. }
  1769. LLVMMoveBasicBlockAfter(check_ftype_idx_succ,
  1770. LLVMGetInsertBlock(comp_ctx->builder));
  1771. if (!(aot_emit_exception(comp_ctx, func_ctx,
  1772. EXCE_INVALID_FUNCTION_TYPE_INDEX, true,
  1773. cmp_ftype_idx, check_ftype_idx_succ)))
  1774. goto fail;
  1775. /* Initialize parameter types of the LLVM function */
  1776. total_param_count = 1 + func_param_count;
  1777. /* Extra function results' addresses (except the first one) are
  1778. appended to aot function parameters. */
  1779. if (func_result_count > 1)
  1780. total_param_count += func_result_count - 1;
  1781. total_size = sizeof(LLVMTypeRef) * (uint64)total_param_count;
  1782. if (total_size >= UINT32_MAX
  1783. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  1784. aot_set_last_error("allocate memory failed.");
  1785. goto fail;
  1786. }
  1787. /* Prepare param types */
  1788. j = 0;
  1789. param_types[j++] = comp_ctx->exec_env_type;
  1790. for (i = 0; i < func_param_count; i++)
  1791. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  1792. for (i = 1; i < func_result_count; i++, j++) {
  1793. param_types[j] = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1794. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  1795. aot_set_last_error("llvm get pointer type failed.");
  1796. goto fail;
  1797. }
  1798. }
  1799. /* Resolve return type of the LLVM function */
  1800. if (func_result_count) {
  1801. wasm_ret_type = func_type->types[func_param_count];
  1802. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  1803. }
  1804. else {
  1805. wasm_ret_type = VALUE_TYPE_VOID;
  1806. ret_type = VOID_TYPE;
  1807. }
  1808. /* Allocate memory for parameters */
  1809. total_size = sizeof(LLVMValueRef) * (uint64)total_param_count;
  1810. if (total_size >= UINT32_MAX
  1811. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  1812. aot_set_last_error("allocate memory failed.");
  1813. goto fail;
  1814. }
  1815. /* First parameter is exec env */
  1816. j = 0;
  1817. param_values[j++] = func_ctx->exec_env;
  1818. /* Pop parameters from stack */
  1819. for (i = func_param_count - 1; (int32)i >= 0; i--)
  1820. POP(param_values[i + j], func_type->types[i]);
  1821. /* Prepare extra parameters */
  1822. ext_cell_num = 0;
  1823. for (i = 1; i < func_result_count; i++) {
  1824. ext_ret_offset = I32_CONST(ext_cell_num);
  1825. CHECK_LLVM_CONST(ext_ret_offset);
  1826. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i - 1);
  1827. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  1828. func_ctx->argv_buf,
  1829. &ext_ret_offset, 1, buf))) {
  1830. aot_set_last_error("llvm build GEP failed.");
  1831. goto fail;
  1832. }
  1833. ext_ret_ptr_type = param_types[func_param_count + i];
  1834. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i - 1);
  1835. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  1836. ext_ret_ptr_type, buf))) {
  1837. aot_set_last_error("llvm build bit cast failed.");
  1838. goto fail;
  1839. }
  1840. param_values[func_param_count + i] = ext_ret_ptr;
  1841. ext_cell_num += wasm_value_type_cell_num_internal(
  1842. func_type->types[func_param_count + i], comp_ctx->pointer_size);
  1843. }
  1844. if (ext_cell_num > 64) {
  1845. aot_set_last_error("prepare call-indirect arguments failed: "
  1846. "maximum 64 extra cell number supported.");
  1847. goto fail;
  1848. }
  1849. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1850. if (comp_ctx->enable_aux_stack_frame) {
  1851. /* TODO: use current frame instead of allocating new frame
  1852. for WASM_OP_RETURN_CALL_INDIRECT */
  1853. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx))
  1854. goto fail;
  1855. }
  1856. #endif
  1857. /* Add basic blocks */
  1858. block_call_import = LLVMAppendBasicBlockInContext(
  1859. comp_ctx->context, func_ctx->func, "call_import");
  1860. block_call_non_import = LLVMAppendBasicBlockInContext(
  1861. comp_ctx->context, func_ctx->func, "call_non_import");
  1862. block_return = LLVMAppendBasicBlockInContext(comp_ctx->context,
  1863. func_ctx->func, "func_return");
  1864. if (!block_call_import || !block_call_non_import || !block_return) {
  1865. aot_set_last_error("llvm add basic block failed.");
  1866. goto fail;
  1867. }
  1868. LLVMMoveBasicBlockAfter(block_call_import,
  1869. LLVMGetInsertBlock(comp_ctx->builder));
  1870. LLVMMoveBasicBlockAfter(block_call_non_import, block_call_import);
  1871. LLVMMoveBasicBlockAfter(block_return, block_call_non_import);
  1872. import_func_count = I32_CONST(comp_ctx->comp_data->import_func_count);
  1873. CHECK_LLVM_CONST(import_func_count);
  1874. /* Check if func_idx < import_func_count */
  1875. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT, func_idx,
  1876. import_func_count, "cmp_func_idx"))) {
  1877. aot_set_last_error("llvm build icmp failed.");
  1878. goto fail;
  1879. }
  1880. /* If func_idx < import_func_count, jump to call import block,
  1881. else jump to call non-import block */
  1882. if (!LLVMBuildCondBr(comp_ctx->builder, cmp_func_idx, block_call_import,
  1883. block_call_non_import)) {
  1884. aot_set_last_error("llvm build cond br failed.");
  1885. goto fail;
  1886. }
  1887. /* Add result phis for return block */
  1888. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  1889. if (func_result_count > 0) {
  1890. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1891. if (total_size >= UINT32_MAX
  1892. || !(result_phis = wasm_runtime_malloc((uint32)total_size))) {
  1893. aot_set_last_error("allocate memory failed.");
  1894. goto fail;
  1895. }
  1896. memset(result_phis, 0, (uint32)total_size);
  1897. for (i = 0; i < func_result_count; i++) {
  1898. LLVMTypeRef tmp_type =
  1899. TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1900. if (!(result_phis[i] =
  1901. LLVMBuildPhi(comp_ctx->builder, tmp_type, "phi"))) {
  1902. aot_set_last_error("llvm build phi failed.");
  1903. goto fail;
  1904. }
  1905. }
  1906. }
  1907. /* Translate call import block */
  1908. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_import);
  1909. if (comp_ctx->enable_aux_stack_frame
  1910. && !commit_params_to_frame_of_import_func(comp_ctx, func_ctx, func_type,
  1911. param_values + 1)) {
  1912. goto fail;
  1913. }
  1914. /* Allocate memory for result values */
  1915. if (func_result_count > 0) {
  1916. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1917. if (total_size >= UINT32_MAX
  1918. || !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
  1919. aot_set_last_error("allocate memory failed.");
  1920. goto fail;
  1921. }
  1922. memset(value_rets, 0, (uint32)total_size);
  1923. }
  1924. param_cell_num = func_type->param_cell_num;
  1925. wasm_ret_types = func_type->types + func_type->param_count;
  1926. tbl_idx_value = I32_CONST(tbl_idx);
  1927. if (!tbl_idx_value) {
  1928. aot_set_last_error("llvm create const failed.");
  1929. goto fail;
  1930. }
  1931. if (!call_aot_call_indirect_func(
  1932. comp_ctx, func_ctx, func_type, ftype_idx, tbl_idx_value, elem_idx,
  1933. param_types + 1, param_values + 1, func_param_count, param_cell_num,
  1934. func_result_count, wasm_ret_types, value_rets, &res))
  1935. goto fail;
  1936. /* Check whether exception was thrown when executing the function */
  1937. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1938. && !check_call_return(comp_ctx, func_ctx, res))
  1939. goto fail;
  1940. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1941. for (i = 0; i < func_result_count; i++) {
  1942. LLVMAddIncoming(result_phis[i], &value_rets[i], &block_curr, 1);
  1943. }
  1944. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1945. aot_set_last_error("llvm build br failed.");
  1946. goto fail;
  1947. }
  1948. /* Translate call non-import block */
  1949. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_non_import);
  1950. /* Load function pointer */
  1951. if (!(func_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, OPQ_PTR_TYPE,
  1952. func_ctx->func_ptrs, &func_idx, 1,
  1953. "func_ptr_tmp"))) {
  1954. aot_set_last_error("llvm build inbounds gep failed.");
  1955. goto fail;
  1956. }
  1957. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, func_ptr,
  1958. "func_ptr"))) {
  1959. aot_set_last_error("llvm build load failed.");
  1960. goto fail;
  1961. }
  1962. if (!(llvm_func_type =
  1963. LLVMFunctionType(ret_type, param_types, total_param_count, false))
  1964. || !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
  1965. aot_set_last_error("llvm add function type failed.");
  1966. goto fail;
  1967. }
  1968. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  1969. llvm_func_ptr_type, "indirect_func"))) {
  1970. aot_set_last_error("llvm build bit cast failed.");
  1971. goto fail;
  1972. }
  1973. if (!(value_ret = LLVMBuildCall2(comp_ctx->builder, llvm_func_type, func,
  1974. param_values, total_param_count,
  1975. func_result_count > 0 ? "ret" : ""))) {
  1976. aot_set_last_error("llvm build call failed.");
  1977. goto fail;
  1978. }
  1979. /* Check whether exception was thrown when executing the function */
  1980. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1981. && !check_exception_thrown(comp_ctx, func_ctx))
  1982. goto fail;
  1983. if (func_result_count > 0) {
  1984. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1985. /* Push the first result to stack */
  1986. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  1987. /* Load extra result from its address and push to stack */
  1988. for (i = 1; i < func_result_count; i++) {
  1989. ret_type = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1990. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  1991. if (!(ext_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  1992. param_values[func_param_count + i],
  1993. buf))) {
  1994. aot_set_last_error("llvm build load failed.");
  1995. goto fail;
  1996. }
  1997. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  1998. }
  1999. }
  2000. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  2001. aot_set_last_error("llvm build br failed.");
  2002. goto fail;
  2003. }
  2004. /* Translate function return block */
  2005. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  2006. for (i = 0; i < func_result_count; i++) {
  2007. PUSH(result_phis[i], func_type->types[func_param_count + i]);
  2008. }
  2009. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  2010. if (comp_ctx->enable_aux_stack_frame) {
  2011. if (!free_frame_for_aot_func(comp_ctx, func_ctx))
  2012. goto fail;
  2013. }
  2014. #endif
  2015. #if WASM_ENABLE_THREAD_MGR != 0
  2016. /* Insert suspend check point */
  2017. if (comp_ctx->enable_thread_mgr) {
  2018. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  2019. goto fail;
  2020. }
  2021. #endif
  2022. ret = true;
  2023. fail:
  2024. if (param_values)
  2025. wasm_runtime_free(param_values);
  2026. if (param_types)
  2027. wasm_runtime_free(param_types);
  2028. if (value_rets)
  2029. wasm_runtime_free(value_rets);
  2030. if (result_phis)
  2031. wasm_runtime_free(result_phis);
  2032. return ret;
  2033. }
  2034. bool
  2035. aot_compile_op_ref_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  2036. {
  2037. if (comp_ctx->enable_gc)
  2038. PUSH_GC_REF(GC_REF_NULL);
  2039. else
  2040. PUSH_I32(REF_NULL);
  2041. return true;
  2042. fail:
  2043. return false;
  2044. }
  2045. bool
  2046. aot_compile_op_ref_is_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  2047. {
  2048. LLVMValueRef lhs = NULL, res;
  2049. if (comp_ctx->enable_gc) {
  2050. POP_GC_REF(lhs);
  2051. if (!(res = LLVMBuildIsNull(comp_ctx->builder, lhs, "lhs is null"))) {
  2052. HANDLE_FAILURE("LLVMBuildIsNull");
  2053. goto fail;
  2054. }
  2055. }
  2056. else {
  2057. POP_I32(lhs);
  2058. if (!(res = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, lhs, REF_NULL,
  2059. "cmp_w_null"))) {
  2060. HANDLE_FAILURE("LLVMBuildICmp");
  2061. goto fail;
  2062. }
  2063. }
  2064. if (!(res = LLVMBuildZExt(comp_ctx->builder, res, I32_TYPE, "r_i"))) {
  2065. HANDLE_FAILURE("LLVMBuildZExt");
  2066. goto fail;
  2067. }
  2068. PUSH_I32(res);
  2069. return true;
  2070. fail:
  2071. return false;
  2072. }
  2073. bool
  2074. aot_compile_op_ref_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  2075. uint32 func_idx, const uint8 *frame_ip_ref_func)
  2076. {
  2077. LLVMValueRef ref_idx;
  2078. #if WASM_ENABLE_GC != 0
  2079. LLVMValueRef gc_obj;
  2080. #endif
  2081. if (!(ref_idx = I32_CONST(func_idx))) {
  2082. HANDLE_FAILURE("LLVMConstInt");
  2083. goto fail;
  2084. }
  2085. #if WASM_ENABLE_GC != 0
  2086. if (comp_ctx->enable_gc) {
  2087. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  2088. return false;
  2089. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, comp_ctx->aot_frame->sp,
  2090. frame_ip_ref_func))
  2091. return false;
  2092. if (!aot_call_aot_create_func_obj(comp_ctx, func_ctx, ref_idx,
  2093. &gc_obj)) {
  2094. goto fail;
  2095. }
  2096. PUSH_GC_REF(gc_obj);
  2097. }
  2098. else
  2099. #endif
  2100. {
  2101. PUSH_I32(ref_idx);
  2102. }
  2103. return true;
  2104. fail:
  2105. return false;
  2106. }
  2107. #if WASM_ENABLE_GC != 0
  2108. bool
  2109. aot_compile_op_call_ref(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  2110. uint32 type_idx, bool tail_call,
  2111. const uint8 *frame_ip_call_ref)
  2112. {
  2113. AOTFuncType *func_type;
  2114. LLVMValueRef func_obj, func_idx;
  2115. LLVMValueRef cmp_func_obj, cmp_func_idx;
  2116. LLVMValueRef func, func_ptr;
  2117. LLVMValueRef ext_ret_offset, ext_ret_ptr, ext_ret, res;
  2118. LLVMValueRef *param_values = NULL;
  2119. LLVMValueRef *result_phis = NULL, value_ret, import_func_count;
  2120. LLVMTypeRef *param_types = NULL, ret_type;
  2121. LLVMTypeRef llvm_func_type, llvm_func_ptr_type;
  2122. LLVMTypeRef ext_ret_ptr_type;
  2123. LLVMBasicBlockRef check_func_obj_succ, block_return, block_curr;
  2124. LLVMBasicBlockRef block_call_import, block_call_non_import;
  2125. LLVMValueRef offset;
  2126. uint32 total_param_count, func_param_count, func_result_count;
  2127. uint32 ext_cell_num, param_cell_num, i, j;
  2128. uint8 wasm_ret_type;
  2129. uint64 total_size;
  2130. char buf[32];
  2131. bool ret = false;
  2132. /* Check function type index */
  2133. bh_assert(type_idx < comp_ctx->comp_data->type_count);
  2134. func_type = (AOTFuncType *)comp_ctx->comp_data->types[type_idx];
  2135. aot_estimate_and_record_stack_usage_for_function_call(comp_ctx, func_ctx,
  2136. func_type);
  2137. func_param_count = func_type->param_count;
  2138. func_result_count = func_type->result_count;
  2139. param_cell_num = func_type->param_cell_num;
  2140. /* Commit stack operands, sp and ip to aot frame */
  2141. if (comp_ctx->aot_frame) {
  2142. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  2143. return false;
  2144. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, comp_ctx->aot_frame->sp,
  2145. frame_ip_call_ref))
  2146. return false;
  2147. }
  2148. #if WASM_ENABLE_THREAD_MGR != 0
  2149. /* Insert suspend check point */
  2150. if (comp_ctx->enable_thread_mgr) {
  2151. if (!check_suspend_flags(comp_ctx, func_ctx, true))
  2152. return false;
  2153. }
  2154. #endif
  2155. POP_GC_REF(func_obj);
  2156. /* Check if func object is NULL */
  2157. if (!(cmp_func_obj =
  2158. LLVMBuildIsNull(comp_ctx->builder, func_obj, "cmp_func_obj"))) {
  2159. aot_set_last_error("llvm build isnull failed.");
  2160. goto fail;
  2161. }
  2162. /* Throw exception if func object is NULL */
  2163. if (!(check_func_obj_succ = LLVMAppendBasicBlockInContext(
  2164. comp_ctx->context, func_ctx->func, "check_func_obj_succ"))) {
  2165. aot_set_last_error("llvm add basic block failed.");
  2166. goto fail;
  2167. }
  2168. LLVMMoveBasicBlockAfter(check_func_obj_succ,
  2169. LLVMGetInsertBlock(comp_ctx->builder));
  2170. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_FUNC_OBJ, true,
  2171. cmp_func_obj, check_func_obj_succ)))
  2172. goto fail;
  2173. /* Get the func idx bound of the WASMFuncObject, the offset may be
  2174. * different in 32-bit runtime and 64-bit runtime since WASMObjectHeader
  2175. * is uintptr_t. Use comp_ctx->pointer_size as the
  2176. * offsetof(WASMFuncObject, func_idx_bound) */
  2177. if (!(offset = I32_CONST(comp_ctx->pointer_size))) {
  2178. HANDLE_FAILURE("LLVMConstInt");
  2179. goto fail;
  2180. }
  2181. if (!(func_idx =
  2182. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, func_obj,
  2183. &offset, 1, "func_idx_bound_i8p"))) {
  2184. HANDLE_FAILURE("LLVMBuildGEP");
  2185. goto fail;
  2186. }
  2187. if (!(func_idx = LLVMBuildBitCast(comp_ctx->builder, func_idx,
  2188. INT32_PTR_TYPE, "func_idx_bound_i32p"))) {
  2189. HANDLE_FAILURE("LLVMBuildBitCast");
  2190. goto fail;
  2191. }
  2192. if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, func_idx,
  2193. "func_idx_bound"))) {
  2194. HANDLE_FAILURE("LLVMBuildLoad");
  2195. goto fail;
  2196. }
  2197. /* Initialize parameter types of the LLVM function */
  2198. total_param_count = 1 + func_param_count;
  2199. /* Extra function results' addresses (except the first one) are
  2200. appended to aot function parameters. */
  2201. if (func_result_count > 1)
  2202. total_param_count += func_result_count - 1;
  2203. total_size = sizeof(LLVMTypeRef) * (uint64)total_param_count;
  2204. if (total_size >= UINT32_MAX
  2205. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  2206. aot_set_last_error("allocate memory failed.");
  2207. goto fail;
  2208. }
  2209. /* Prepare param types */
  2210. j = 0;
  2211. param_types[j++] = comp_ctx->exec_env_type;
  2212. for (i = 0; i < func_param_count; i++)
  2213. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  2214. for (i = 1; i < func_result_count; i++, j++) {
  2215. param_types[j] = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  2216. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  2217. aot_set_last_error("llvm get pointer type failed.");
  2218. goto fail;
  2219. }
  2220. }
  2221. /* Resolve return type of the LLVM function */
  2222. if (func_result_count) {
  2223. wasm_ret_type = func_type->types[func_param_count];
  2224. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  2225. }
  2226. else {
  2227. wasm_ret_type = VALUE_TYPE_VOID;
  2228. ret_type = VOID_TYPE;
  2229. }
  2230. /* Allocate memory for parameters */
  2231. total_size = sizeof(LLVMValueRef) * (uint64)total_param_count;
  2232. if (total_size >= UINT32_MAX
  2233. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  2234. aot_set_last_error("allocate memory failed.");
  2235. goto fail;
  2236. }
  2237. /* First parameter is exec env */
  2238. j = 0;
  2239. param_values[j++] = func_ctx->exec_env;
  2240. /* Pop parameters from stack */
  2241. for (i = func_param_count - 1; (int32)i >= 0; i--)
  2242. POP(param_values[i + j], func_type->types[i]);
  2243. /* Prepare extra parameters */
  2244. ext_cell_num = 0;
  2245. for (i = 1; i < func_result_count; i++) {
  2246. ext_ret_offset = I32_CONST(ext_cell_num);
  2247. CHECK_LLVM_CONST(ext_ret_offset);
  2248. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i - 1);
  2249. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  2250. func_ctx->argv_buf,
  2251. &ext_ret_offset, 1, buf))) {
  2252. aot_set_last_error("llvm build GEP failed.");
  2253. goto fail;
  2254. }
  2255. ext_ret_ptr_type = param_types[func_param_count + i];
  2256. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i - 1);
  2257. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  2258. ext_ret_ptr_type, buf))) {
  2259. aot_set_last_error("llvm build bit cast failed.");
  2260. goto fail;
  2261. }
  2262. param_values[func_param_count + i] = ext_ret_ptr;
  2263. ext_cell_num += wasm_value_type_cell_num_internal(
  2264. func_type->types[func_param_count + i], comp_ctx->pointer_size);
  2265. }
  2266. if (ext_cell_num > 64) {
  2267. aot_set_last_error("prepare call-indirect arguments failed: "
  2268. "maximum 64 extra cell number supported.");
  2269. goto fail;
  2270. }
  2271. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  2272. if (comp_ctx->enable_aux_stack_frame) {
  2273. /* TODO: use current frame instead of allocating new frame
  2274. for WASM_OP_RETURN_CALL_REF */
  2275. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx))
  2276. goto fail;
  2277. }
  2278. #endif
  2279. /* Add basic blocks */
  2280. block_call_import = LLVMAppendBasicBlockInContext(
  2281. comp_ctx->context, func_ctx->func, "call_import");
  2282. block_call_non_import = LLVMAppendBasicBlockInContext(
  2283. comp_ctx->context, func_ctx->func, "call_non_import");
  2284. block_return = LLVMAppendBasicBlockInContext(comp_ctx->context,
  2285. func_ctx->func, "func_return");
  2286. if (!block_call_import || !block_call_non_import || !block_return) {
  2287. aot_set_last_error("llvm add basic block failed.");
  2288. goto fail;
  2289. }
  2290. LLVMMoveBasicBlockAfter(block_call_import,
  2291. LLVMGetInsertBlock(comp_ctx->builder));
  2292. LLVMMoveBasicBlockAfter(block_call_non_import, block_call_import);
  2293. LLVMMoveBasicBlockAfter(block_return, block_call_non_import);
  2294. import_func_count = I32_CONST(comp_ctx->comp_data->import_func_count);
  2295. CHECK_LLVM_CONST(import_func_count);
  2296. /* Check if func_idx < import_func_count */
  2297. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT, func_idx,
  2298. import_func_count, "cmp_func_idx"))) {
  2299. aot_set_last_error("llvm build icmp failed.");
  2300. goto fail;
  2301. }
  2302. /* If func_idx < import_func_count, jump to call import block,
  2303. else jump to call non-import block */
  2304. if (!LLVMBuildCondBr(comp_ctx->builder, cmp_func_idx, block_call_import,
  2305. block_call_non_import)) {
  2306. aot_set_last_error("llvm build cond br failed.");
  2307. goto fail;
  2308. }
  2309. /* Add result phis for return block */
  2310. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  2311. if (func_result_count > 0) {
  2312. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  2313. if (total_size >= UINT32_MAX
  2314. || !(result_phis = wasm_runtime_malloc((uint32)total_size))) {
  2315. aot_set_last_error("allocate memory failed.");
  2316. goto fail;
  2317. }
  2318. memset(result_phis, 0, (uint32)total_size);
  2319. for (i = 0; i < func_result_count; i++) {
  2320. LLVMTypeRef tmp_type =
  2321. TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  2322. if (!(result_phis[i] =
  2323. LLVMBuildPhi(comp_ctx->builder, tmp_type, "phi"))) {
  2324. aot_set_last_error("llvm build phi failed.");
  2325. goto fail;
  2326. }
  2327. }
  2328. }
  2329. /* Translate call import block */
  2330. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_import);
  2331. if (comp_ctx->enable_aux_stack_frame
  2332. && !commit_params_to_frame_of_import_func(comp_ctx, func_ctx, func_type,
  2333. param_values + 1)) {
  2334. goto fail;
  2335. }
  2336. /* Similar to opcode call_indirect, but for opcode ref.func needs to call
  2337. * aot_invoke_native_func instead */
  2338. if (!call_aot_invoke_native_func(comp_ctx, func_ctx, func_idx, func_type,
  2339. param_types + 1, param_values + 1,
  2340. func_param_count, param_cell_num, ret_type,
  2341. wasm_ret_type, &value_ret, &res))
  2342. goto fail;
  2343. /* Check whether exception was thrown when executing the function */
  2344. if (comp_ctx->enable_bound_check
  2345. && !check_call_return(comp_ctx, func_ctx, res))
  2346. goto fail;
  2347. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  2348. /* Get function return values, for aot_invoke_native_func, the extra ret
  2349. * values are put into param's array */
  2350. if (func_result_count > 0) {
  2351. /* Push the first result to stack */
  2352. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  2353. /* Load extra result from its address and push to stack */
  2354. for (i = 1; i < func_result_count; i++) {
  2355. ret_type = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  2356. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  2357. if (!(ext_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  2358. param_values[func_param_count + i],
  2359. buf))) {
  2360. aot_set_last_error("llvm build load failed.");
  2361. goto fail;
  2362. }
  2363. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  2364. }
  2365. }
  2366. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  2367. aot_set_last_error("llvm build br failed.");
  2368. goto fail;
  2369. }
  2370. /* Translate call non-import block */
  2371. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_non_import);
  2372. /* Load function pointer */
  2373. if (!(func_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, OPQ_PTR_TYPE,
  2374. func_ctx->func_ptrs, &func_idx, 1,
  2375. "func_ptr_tmp"))) {
  2376. aot_set_last_error("llvm build inbounds gep failed.");
  2377. goto fail;
  2378. }
  2379. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, func_ptr,
  2380. "func_ptr"))) {
  2381. aot_set_last_error("llvm build load failed.");
  2382. goto fail;
  2383. }
  2384. if (!(llvm_func_type =
  2385. LLVMFunctionType(ret_type, param_types, total_param_count, false))
  2386. || !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
  2387. aot_set_last_error("llvm add function type failed.");
  2388. goto fail;
  2389. }
  2390. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  2391. llvm_func_ptr_type, "indirect_func"))) {
  2392. aot_set_last_error("llvm build bit cast failed.");
  2393. goto fail;
  2394. }
  2395. if (!(value_ret = LLVMBuildCall2(comp_ctx->builder, llvm_func_type, func,
  2396. param_values, total_param_count,
  2397. func_result_count > 0 ? "ret" : ""))) {
  2398. aot_set_last_error("llvm build call failed.");
  2399. goto fail;
  2400. }
  2401. /* Set calling convention for the call with the func's calling
  2402. convention */
  2403. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  2404. if (tail_call)
  2405. LLVMSetTailCall(value_ret, true);
  2406. /* Check whether exception was thrown when executing the function */
  2407. if (!tail_call
  2408. && (comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  2409. && !check_exception_thrown(comp_ctx, func_ctx))
  2410. goto fail;
  2411. if (func_result_count > 0) {
  2412. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  2413. /* Push the first result to stack */
  2414. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  2415. /* Load extra result from its address and push to stack */
  2416. for (i = 1; i < func_result_count; i++) {
  2417. ret_type = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  2418. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  2419. if (!(ext_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  2420. param_values[func_param_count + i],
  2421. buf))) {
  2422. aot_set_last_error("llvm build load failed.");
  2423. goto fail;
  2424. }
  2425. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  2426. }
  2427. }
  2428. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  2429. aot_set_last_error("llvm build br failed.");
  2430. goto fail;
  2431. }
  2432. /* Translate function return block */
  2433. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  2434. for (i = 0; i < func_result_count; i++) {
  2435. PUSH(result_phis[i], func_type->types[func_param_count + i]);
  2436. }
  2437. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  2438. if (comp_ctx->enable_aux_stack_frame) {
  2439. if (!free_frame_for_aot_func(comp_ctx, func_ctx))
  2440. goto fail;
  2441. }
  2442. #endif
  2443. #if WASM_ENABLE_THREAD_MGR != 0
  2444. /* Insert suspend check point */
  2445. if (comp_ctx->enable_thread_mgr) {
  2446. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  2447. goto fail;
  2448. }
  2449. #endif
  2450. ret = true;
  2451. fail:
  2452. if (param_values)
  2453. wasm_runtime_free(param_values);
  2454. if (param_types)
  2455. wasm_runtime_free(param_types);
  2456. if (result_phis)
  2457. wasm_runtime_free(result_phis);
  2458. return ret;
  2459. }
  2460. #endif /* end of WASM_ENABLE_GC != 0 */