aot_emit_function.c 78 KB

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