aot_emit_function.c 118 KB

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