aot_emit_function.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502
  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. #define ADD_BASIC_BLOCK(block, name) \
  11. do { \
  12. if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
  13. func_ctx->func, name))) { \
  14. aot_set_last_error("llvm add basic block failed."); \
  15. goto fail; \
  16. } \
  17. } while (0)
  18. static bool
  19. create_func_return_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  20. {
  21. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  22. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  23. /* Create function return block if it isn't created */
  24. if (!func_ctx->func_return_block) {
  25. if (!(func_ctx->func_return_block = LLVMAppendBasicBlockInContext(
  26. comp_ctx->context, func_ctx->func, "func_ret"))) {
  27. aot_set_last_error("llvm add basic block failed.");
  28. return false;
  29. }
  30. /* Create return IR */
  31. LLVMPositionBuilderAtEnd(comp_ctx->builder,
  32. func_ctx->func_return_block);
  33. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  34. return false;
  35. }
  36. }
  37. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  38. return true;
  39. }
  40. /* Check whether there was exception thrown, if yes, return directly */
  41. static bool
  42. check_exception_thrown(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  43. {
  44. LLVMBasicBlockRef block_curr, check_except_succ;
  45. LLVMValueRef value, cmp;
  46. /* Create function return block if it isn't created */
  47. if (!create_func_return_block(comp_ctx, func_ctx))
  48. return false;
  49. /* Load the first byte of aot_module_inst->cur_exception, and check
  50. whether it is '\0'. If yes, no exception was thrown. */
  51. if (!(value = LLVMBuildLoad(comp_ctx->builder, func_ctx->cur_exception,
  52. "exce_value"))
  53. || !(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, value, I8_ZERO,
  54. "cmp"))) {
  55. aot_set_last_error("llvm build icmp failed.");
  56. return false;
  57. }
  58. /* Add check exection success block */
  59. if (!(check_except_succ = LLVMAppendBasicBlockInContext(
  60. comp_ctx->context, func_ctx->func, "check_except_succ"))) {
  61. aot_set_last_error("llvm add basic block failed.");
  62. return false;
  63. }
  64. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  65. LLVMMoveBasicBlockAfter(check_except_succ, block_curr);
  66. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  67. /* Create condition br */
  68. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_except_succ,
  69. func_ctx->func_return_block)) {
  70. aot_set_last_error("llvm build cond br failed.");
  71. return false;
  72. }
  73. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_except_succ);
  74. return true;
  75. }
  76. /* Check whether there was exception thrown, if yes, return directly */
  77. static bool
  78. check_call_return(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  79. LLVMValueRef res)
  80. {
  81. LLVMBasicBlockRef block_curr, check_call_succ;
  82. LLVMValueRef cmp;
  83. /* Create function return block if it isn't created */
  84. if (!create_func_return_block(comp_ctx, func_ctx))
  85. return false;
  86. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, res, I8_ZERO,
  87. "cmp"))) {
  88. aot_set_last_error("llvm build icmp failed.");
  89. return false;
  90. }
  91. /* Add check exection success block */
  92. if (!(check_call_succ = LLVMAppendBasicBlockInContext(
  93. comp_ctx->context, func_ctx->func, "check_call_succ"))) {
  94. aot_set_last_error("llvm add basic block failed.");
  95. return false;
  96. }
  97. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  98. LLVMMoveBasicBlockAfter(check_call_succ, block_curr);
  99. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  100. /* Create condition br */
  101. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_call_succ,
  102. func_ctx->func_return_block)) {
  103. aot_set_last_error("llvm build cond br failed.");
  104. return false;
  105. }
  106. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_call_succ);
  107. return true;
  108. }
  109. #if 0
  110. static bool
  111. call_aot_invoke_native_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  112. LLVMValueRef func_idx, AOTFuncType *aot_func_type,
  113. LLVMTypeRef *param_types,
  114. LLVMValueRef *param_values, uint32 param_count,
  115. uint32 param_cell_num, LLVMTypeRef ret_type,
  116. uint8 wasm_ret_type, LLVMValueRef *p_value_ret,
  117. LLVMValueRef *p_res)
  118. {
  119. LLVMTypeRef func_type, func_ptr_type, func_param_types[4];
  120. LLVMTypeRef ret_ptr_type, elem_ptr_type;
  121. LLVMValueRef func, elem_idx, elem_ptr;
  122. LLVMValueRef func_param_values[4], value_ret = NULL, res;
  123. char buf[32], *func_name = "aot_invoke_native";
  124. uint32 i, cell_num = 0;
  125. /* prepare function type of aot_invoke_native */
  126. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  127. func_param_types[1] = I32_TYPE; /* func_idx */
  128. func_param_types[2] = I32_TYPE; /* argc */
  129. func_param_types[3] = INT32_PTR_TYPE; /* argv */
  130. if (!(func_type =
  131. LLVMFunctionType(INT8_TYPE, func_param_types, 4, false))) {
  132. aot_set_last_error("llvm add function type failed.");
  133. return false;
  134. }
  135. /* prepare function pointer */
  136. if (comp_ctx->is_jit_mode) {
  137. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  138. aot_set_last_error("create LLVM function type failed.");
  139. return false;
  140. }
  141. /* JIT mode, call the function directly */
  142. if (!(func = I64_CONST((uint64)(uintptr_t)aot_invoke_native))
  143. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  144. aot_set_last_error("create LLVM value failed.");
  145. return false;
  146. }
  147. }
  148. else if (comp_ctx->is_indirect_mode) {
  149. int32 func_index;
  150. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  151. aot_set_last_error("create LLVM function type failed.");
  152. return false;
  153. }
  154. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  155. if (func_index < 0) {
  156. return false;
  157. }
  158. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  159. func_ptr_type, func_index))) {
  160. return false;
  161. }
  162. }
  163. else {
  164. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  165. && !(func =
  166. LLVMAddFunction(comp_ctx->module, func_name, func_type))) {
  167. aot_set_last_error("add LLVM function failed.");
  168. return false;
  169. }
  170. }
  171. if (param_cell_num > 64) {
  172. aot_set_last_error("prepare native arguments failed: "
  173. "maximum 64 parameter cell number supported.");
  174. return false;
  175. }
  176. /* prepare frame_lp */
  177. for (i = 0; i < param_count; i++) {
  178. if (!(elem_idx = I32_CONST(cell_num))
  179. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  180. aot_set_last_error("llvm add const or pointer type failed.");
  181. return false;
  182. }
  183. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  184. if (!(elem_ptr = LLVMBuildInBoundsGEP(
  185. comp_ctx->builder, func_ctx->argv_buf, &elem_idx, 1, buf))
  186. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  187. elem_ptr_type, buf))) {
  188. aot_set_last_error("llvm build bit cast failed.");
  189. return false;
  190. }
  191. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  192. elem_ptr))) {
  193. aot_set_last_error("llvm build store failed.");
  194. return false;
  195. }
  196. LLVMSetAlignment(res, 1);
  197. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  198. }
  199. func_param_values[0] = func_ctx->exec_env;
  200. func_param_values[1] = func_idx;
  201. func_param_values[2] = I32_CONST(param_cell_num);
  202. func_param_values[3] = func_ctx->argv_buf;
  203. if (!func_param_values[2]) {
  204. aot_set_last_error("llvm create const failed.");
  205. return false;
  206. }
  207. /* call aot_invoke_native() function */
  208. if (!(res = LLVMBuildCall(comp_ctx->builder, func, func_param_values, 4,
  209. "res"))) {
  210. aot_set_last_error("llvm build call failed.");
  211. return false;
  212. }
  213. /* get function return value */
  214. if (wasm_ret_type != VALUE_TYPE_VOID) {
  215. if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  216. aot_set_last_error("llvm add pointer type failed.");
  217. return false;
  218. }
  219. if (!(value_ret =
  220. LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
  221. ret_ptr_type, "argv_ret"))) {
  222. aot_set_last_error("llvm build bit cast failed.");
  223. return false;
  224. }
  225. if (!(*p_value_ret =
  226. LLVMBuildLoad(comp_ctx->builder, value_ret, "value_ret"))) {
  227. aot_set_last_error("llvm build load failed.");
  228. return false;
  229. }
  230. }
  231. *p_res = res;
  232. return true;
  233. }
  234. #endif
  235. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  236. static bool
  237. call_aot_alloc_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  238. LLVMValueRef func_idx)
  239. {
  240. LLVMValueRef param_values[2], ret_value, value, func;
  241. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  242. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  243. LLVMBasicBlockRef frame_alloc_fail, frame_alloc_success;
  244. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  245. param_types[0] = comp_ctx->exec_env_type;
  246. param_types[1] = I32_TYPE;
  247. ret_type = INT8_TYPE;
  248. GET_AOT_FUNCTION(aot_alloc_frame, 2);
  249. param_values[0] = func_ctx->exec_env;
  250. param_values[1] = func_idx;
  251. if (!(ret_value = LLVMBuildCall(comp_ctx->builder, func, param_values, 2,
  252. "call_aot_alloc_frame"))) {
  253. aot_set_last_error("llvm build call failed.");
  254. return false;
  255. }
  256. if (!(ret_value = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, ret_value,
  257. I8_ZERO, "frame_alloc_ret"))) {
  258. aot_set_last_error("llvm build icmp failed.");
  259. return false;
  260. }
  261. ADD_BASIC_BLOCK(frame_alloc_fail, "frame_alloc_fail");
  262. ADD_BASIC_BLOCK(frame_alloc_success, "frame_alloc_success");
  263. LLVMMoveBasicBlockAfter(frame_alloc_fail, block_curr);
  264. LLVMMoveBasicBlockAfter(frame_alloc_success, block_curr);
  265. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, frame_alloc_success,
  266. frame_alloc_fail)) {
  267. aot_set_last_error("llvm build cond br failed.");
  268. return false;
  269. }
  270. /* If frame alloc failed, return this function
  271. so the runtime can catch the exception */
  272. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_fail);
  273. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  274. return false;
  275. }
  276. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_success);
  277. return true;
  278. fail:
  279. return false;
  280. }
  281. static bool
  282. call_aot_free_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  283. {
  284. LLVMValueRef param_values[1], ret_value, value, func;
  285. LLVMTypeRef param_types[1], ret_type, func_type, func_ptr_type;
  286. param_types[0] = comp_ctx->exec_env_type;
  287. ret_type = INT8_TYPE;
  288. GET_AOT_FUNCTION(aot_free_frame, 1);
  289. param_values[0] = func_ctx->exec_env;
  290. if (!(ret_value = LLVMBuildCall(comp_ctx->builder, func, param_values, 1,
  291. "call_aot_free_frame"))) {
  292. aot_set_last_error("llvm build call failed.");
  293. return false;
  294. }
  295. return true;
  296. }
  297. #endif /* end of (WASM_ENABLE_DUMP_CALL_STACK != 0) \
  298. || (WASM_ENABLE_PERF_PROFILING != 0) */
  299. static bool
  300. check_stack_boundary(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  301. uint32 callee_cell_num)
  302. {
  303. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  304. LLVMBasicBlockRef check_stack;
  305. LLVMValueRef callee_local_size, stack_bound, cmp;
  306. if (!(callee_local_size = I32_CONST(callee_cell_num * 4))) {
  307. aot_set_last_error("llvm build const failed.");
  308. return false;
  309. }
  310. if (!(stack_bound = LLVMBuildInBoundsGEP(
  311. comp_ctx->builder, func_ctx->native_stack_bound,
  312. &callee_local_size, 1, "stack_bound"))) {
  313. aot_set_last_error("llvm build inbound gep failed.");
  314. return false;
  315. }
  316. if (!(check_stack = LLVMAppendBasicBlockInContext(
  317. comp_ctx->context, func_ctx->func, "check_stack"))) {
  318. aot_set_last_error("llvm add basic block failed.");
  319. return false;
  320. }
  321. LLVMMoveBasicBlockAfter(check_stack, block_curr);
  322. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT,
  323. func_ctx->last_alloca, stack_bound, "cmp"))) {
  324. aot_set_last_error("llvm build icmp failed.");
  325. return false;
  326. }
  327. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NATIVE_STACK_OVERFLOW,
  328. true, cmp, check_stack)) {
  329. return false;
  330. }
  331. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_stack);
  332. return true;
  333. }
  334. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  335. bool
  336. call_aot_resolve_and_call_import_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  337. LLVMValueRef import_func_id, AOTFuncType *aot_func_type,
  338. LLVMTypeRef *param_types, LLVMValueRef *param_values,
  339. uint32 param_count, uint32 param_cell_num,
  340. LLVMTypeRef ret_type, uint8 wasm_ret_type,
  341. LLVMValueRef *p_value_ret, LLVMValueRef *p_res)
  342. {
  343. #define PARAM_COUNT 4
  344. LLVMTypeRef func_type, ret_ptr_type, elem_ptr_type;
  345. LLVMTypeRef func_ptr_type, func_param_types[PARAM_COUNT];
  346. LLVMValueRef func, elem_idx, elem_ptr;
  347. LLVMValueRef func_param_values[PARAM_COUNT], value_ret = NULL, res = NULL;
  348. char buf[32], *func_name = "aot_resolve_function";
  349. uint32 i = 0, cell_num = 0;
  350. /* prepare function type of aot_call_indirect */
  351. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  352. func_param_types[1] = I32_TYPE; /* import func id */
  353. func_param_types[2] = I32_TYPE; /* argc */
  354. func_param_types[3] = INT32_PTR_TYPE; /* argv */
  355. if (!(func_type = LLVMFunctionType(INT8_TYPE, func_param_types, PARAM_COUNT, false))) {
  356. aot_set_last_error("llvm add function type failed.");
  357. return false;
  358. }
  359. /* prepare function pointer */
  360. if (comp_ctx->is_jit_mode) {
  361. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  362. aot_set_last_error("create LLVM function type failed.");
  363. return false;
  364. }
  365. /* JIT mode, call the function directly */
  366. if (!(func = I64_CONST((uint64)(uintptr_t)aot_resolve_function))
  367. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  368. aot_set_last_error("create LLVM value failed.");
  369. return false;
  370. }
  371. }
  372. else {
  373. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  374. && !(func = LLVMAddFunction(comp_ctx->module,
  375. func_name, func_type))) {
  376. aot_set_last_error("add LLVM function failed.");
  377. return false;
  378. }
  379. }
  380. if (param_cell_num > 64) {
  381. aot_set_last_error("prepare native arguments failed: "
  382. "maximum 64 parameter cell number supported.");
  383. return false;
  384. }
  385. /* prepare frame_lp */
  386. for (i = 0; i < param_count; i++) {
  387. if (!(elem_idx = I32_CONST(cell_num))
  388. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  389. aot_set_last_error("llvm add const or pointer type failed.");
  390. return false;
  391. }
  392. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  393. if (!(elem_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  394. func_ctx->argv_buf, &elem_idx, 1, buf))
  395. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  396. elem_ptr_type, buf))) {
  397. aot_set_last_error("llvm build bit cast failed.");
  398. return false;
  399. }
  400. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i], elem_ptr))) {
  401. aot_set_last_error("llvm build store failed.");
  402. return false;
  403. }
  404. LLVMSetAlignment(res, 1);
  405. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  406. }
  407. func_param_values[0] = func_ctx->exec_env;
  408. func_param_values[1] = import_func_id;
  409. func_param_values[2] = I32_CONST(param_cell_num);
  410. func_param_values[3] = func_ctx->argv_buf;
  411. if (!func_param_values[2]) {
  412. aot_set_last_error("llvm create const failed.");
  413. return false;
  414. }
  415. /* call wasm_program_resolve_aot_function() function */
  416. if (!(res = LLVMBuildCall(comp_ctx->builder, func,
  417. func_param_values, PARAM_COUNT, "res"))) {
  418. aot_set_last_error("llvm build call failed.");
  419. return false;
  420. }
  421. /* get function return value */
  422. if (wasm_ret_type != VALUE_TYPE_VOID) {
  423. if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  424. aot_set_last_error("llvm add pointer type failed.");
  425. return false;
  426. }
  427. if (!(value_ret = LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
  428. ret_ptr_type, "argv_ret"))) {
  429. aot_set_last_error("llvm build bit cast failed.");
  430. return false;
  431. }
  432. if (!(*p_value_ret = LLVMBuildLoad(comp_ctx->builder, value_ret,
  433. "value_ret"))) {
  434. aot_set_last_error("llvm build load failed.");
  435. return false;
  436. }
  437. }
  438. *p_res = res;
  439. return true;
  440. #undef PARAM_COUNT
  441. }
  442. #endif
  443. static LLVMValueRef
  444. aot_emit_call_native(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  445. uint32 func_idx, AOTFuncType *func_type,
  446. LLVMTypeRef ret_type, int32 ext_ret_count,
  447. LLVMTypeRef *param_types, LLVMValueRef *param_values,
  448. int32 param_count)
  449. {
  450. LLVMTypeRef func_ref_type;
  451. LLVMValueRef func, value_ret;
  452. if (!(func_ref_type =
  453. LLVMFunctionType(ret_type, param_types, param_count + 1, false))) {
  454. aot_set_last_error("llvm add function type failed.");
  455. return NULL;
  456. }
  457. func_ref_type = LLVMPointerType(func_ref_type, 0);
  458. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->func_ptrs,
  459. func_ref_type, func_idx))) {
  460. return NULL;
  461. }
  462. if (!(value_ret =
  463. LLVMBuildCall(comp_ctx->builder, func, param_values,
  464. (uint32)param_count + 1 + ext_ret_count,
  465. (func_type->result_count > 0 ? "call" : "")))) {
  466. aot_set_last_error("LLVM build call failed.");
  467. return NULL;
  468. }
  469. return value_ret;
  470. }
  471. bool
  472. aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  473. uint32 func_idx, bool tail_call)
  474. {
  475. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  476. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  477. uint32 func_count = comp_ctx->func_ctx_count;
  478. uint32 ext_ret_cell_num = 0, cell_num = 0;
  479. AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
  480. AOTFuncType *func_type;
  481. AOTFunc *aot_func;
  482. LLVMTypeRef *param_types = NULL, ret_type;
  483. LLVMTypeRef ext_ret_ptr_type;
  484. LLVMValueRef *param_values = NULL, func;
  485. LLVMValueRef import_func_idx;
  486. LLVMValueRef ext_ret, ext_ret_ptr, ext_ret_idx;
  487. LLVMValueRef value_ret_phi = NULL;
  488. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  489. uint32 param_cell_num = 0;
  490. LLVMBasicBlockRef block_func_not_linked, block_func_linked, check_call_succ, check_except_succ;
  491. LLVMValueRef value_ret_incoming[2];
  492. LLVMValueRef result;
  493. LLVMValueRef if_func_linked, if_res_succ, const_zero_ref;
  494. LLVMTypeRef pointer_type;
  495. #endif
  496. int32 i, j = 0, param_count, result_count, ext_ret_count;
  497. uint64 total_size;
  498. uint32 callee_cell_num;
  499. uint8 wasm_ret_type;
  500. uint8 *ext_ret_types = NULL;
  501. bool ret = false;
  502. char buf[32];
  503. #if WASM_ENABLE_THREAD_MGR != 0
  504. /* Insert suspend check point */
  505. if (comp_ctx->enable_thread_mgr) {
  506. if (!check_suspend_flags(comp_ctx, func_ctx))
  507. return false;
  508. }
  509. #endif
  510. /* Check function index */
  511. if (func_idx >= import_func_count + func_count) {
  512. aot_set_last_error("Function index out of range.");
  513. return false;
  514. }
  515. /* Get function type */
  516. if (func_idx < import_func_count)
  517. func_type = import_funcs[func_idx].func_type;
  518. else
  519. func_type =
  520. func_ctxes[func_idx - import_func_count]->aot_func->func_type;
  521. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  522. if (comp_ctx->enable_aux_stack_frame) {
  523. LLVMValueRef func_idx_const;
  524. if (!(func_idx_const = I32_CONST(func_idx))) {
  525. aot_set_last_error("llvm build const failed.");
  526. return false;
  527. }
  528. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx_const))
  529. return false;
  530. }
  531. #endif
  532. /* Allocate memory for parameters.
  533. * Parameters layout:
  534. * - exec env
  535. * - wasm function's parameters
  536. * - extra results'(except the first one) addresses
  537. */
  538. param_count = (int32)func_type->param_count;
  539. result_count = (int32)func_type->result_count;
  540. ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  541. total_size =
  542. sizeof(LLVMValueRef) * (uint64)(param_count + 1 + ext_ret_count);
  543. if (total_size >= UINT32_MAX
  544. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  545. aot_set_last_error("allocate memory failed.");
  546. return false;
  547. }
  548. /* First parameter is exec env */
  549. param_values[j++] = func_ctx->exec_env;
  550. /* Pop parameters from stack */
  551. for (i = param_count - 1; i >= 0; i--)
  552. POP(param_values[i + j], func_type->types[i]);
  553. /* Set parameters for multiple return values, the first return value
  554. is returned by function return value, and the other return values
  555. are returned by function parameters with pointer types */
  556. if (ext_ret_count > 0) {
  557. ext_ret_types = func_type->types + param_count + 1;
  558. ext_ret_cell_num = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  559. if (ext_ret_cell_num > 64) {
  560. aot_set_last_error("prepare extra results's return "
  561. "address arguments failed: "
  562. "maximum 64 parameter cell number supported.");
  563. goto fail;
  564. }
  565. for (i = 0; i < ext_ret_count; i++) {
  566. if (!(ext_ret_idx = I32_CONST(cell_num))
  567. || !(ext_ret_ptr_type =
  568. LLVMPointerType(TO_LLVM_TYPE(ext_ret_types[i]), 0))) {
  569. aot_set_last_error("llvm add const or pointer type failed.");
  570. goto fail;
  571. }
  572. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i);
  573. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  574. func_ctx->argv_buf,
  575. &ext_ret_idx, 1, buf))) {
  576. aot_set_last_error("llvm build GEP failed.");
  577. goto fail;
  578. }
  579. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i);
  580. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  581. ext_ret_ptr_type, buf))) {
  582. aot_set_last_error("llvm build bit cast failed.");
  583. goto fail;
  584. }
  585. param_values[param_count + 1 + i] = ext_ret_ptr;
  586. cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
  587. }
  588. }
  589. if (func_idx < import_func_count) {
  590. if (!(import_func_idx = I32_CONST(func_idx))) {
  591. aot_set_last_error("llvm build inbounds gep failed.");
  592. goto fail;
  593. }
  594. /* Initialize parameter types of the LLVM function */
  595. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  596. if (total_size >= UINT32_MAX
  597. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  598. aot_set_last_error("allocate memory failed.");
  599. goto fail;
  600. }
  601. j = 0;
  602. param_types[j++] = comp_ctx->exec_env_type;
  603. for (i = 0; i < param_count; i++)
  604. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  605. if (func_type->result_count) {
  606. wasm_ret_type = func_type->types[func_type->param_count];
  607. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  608. }
  609. else {
  610. wasm_ret_type = VALUE_TYPE_VOID;
  611. ret_type = VOID_TYPE;
  612. }
  613. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  614. /* Get param cell number */
  615. param_cell_num = func_type->param_cell_num;
  616. //aot_call_debugtrap_intrinsic(comp_ctx, func_ctx);
  617. LLVMValueRef func_id_ref = I32_CONST(func_idx);
  618. if (!(func =
  619. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->func_ptrs,
  620. &func_id_ref, 1, "func_ptr_tmp"))) {
  621. aot_set_last_error("llvm build inbounds gep failed.");
  622. goto fail;
  623. }
  624. if (!(func = LLVMBuildLoad(comp_ctx->builder, func, "func_ptr"))) {
  625. aot_set_last_error("llvm build load failed.");
  626. goto fail;
  627. }
  628. if (comp_ctx->pointer_size == sizeof(uint64)) {
  629. pointer_type = I64_TYPE;
  630. const_zero_ref = I64_ZERO;
  631. } else {
  632. pointer_type = I32_TYPE;
  633. const_zero_ref = I32_ZERO;
  634. }
  635. func = LLVMBuildPtrToInt(comp_ctx->builder, func, pointer_type, "func_pointer_value");
  636. if (!(if_func_linked = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE,
  637. func, const_zero_ref, "is_func_linked"))) {
  638. aot_set_last_error("llvm build icmp failed.");
  639. goto fail;
  640. }
  641. if (!(block_func_not_linked =
  642. LLVMAppendBasicBlockInContext(comp_ctx->context,
  643. func_ctx->func,
  644. "block_func_not_linked"))) {
  645. aot_set_last_error("llvm add basic block failed.");
  646. goto fail;
  647. }
  648. block_func_linked =
  649. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  650. "block_func_linked");
  651. if (!(check_call_succ = LLVMAppendBasicBlockInContext(
  652. comp_ctx->context, func_ctx->func, "check_call_succ"))) {
  653. aot_set_last_error("llvm add basic block failed.");
  654. goto fail;
  655. }
  656. LLVMMoveBasicBlockAfter(block_func_linked,
  657. LLVMGetInsertBlock(comp_ctx->builder));
  658. LLVMMoveBasicBlockAfter(block_func_not_linked, block_func_linked);
  659. LLVMMoveBasicBlockAfter(check_call_succ, block_func_not_linked);
  660. if (!LLVMBuildCondBr(comp_ctx->builder, if_func_linked,
  661. block_func_linked, block_func_not_linked)) {
  662. aot_set_last_error("llvm build cond br failed.");
  663. goto fail;
  664. }
  665. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_call_succ);
  666. if (wasm_ret_type != VALUE_TYPE_VOID) {
  667. if (!(value_ret_phi = LLVMBuildPhi(comp_ctx->builder,
  668. ret_type, "value_ret_phi"))) {
  669. aot_set_last_error("llvm build phi failed.");
  670. goto fail;
  671. }
  672. }
  673. /* Check whether exception was thrown when executing the function */
  674. if (!check_exception_thrown(comp_ctx, func_ctx))
  675. goto fail;
  676. check_except_succ = LLVMGetInsertBlock(comp_ctx->builder);
  677. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_func_linked);
  678. //aot_call_debugtrap_intrinsic(comp_ctx, func_ctx);
  679. value_ret_incoming[0] = aot_emit_call_native(comp_ctx, func_ctx,
  680. func_idx, func_type,
  681. ret_type, ext_ret_count,
  682. param_types, param_values,
  683. param_count);
  684. if (!value_ret_incoming[0])
  685. goto fail;
  686. if (wasm_ret_type != VALUE_TYPE_VOID) {
  687. LLVMAddIncoming(value_ret_phi, &value_ret_incoming[0], &block_func_linked, 1);
  688. }
  689. if (!LLVMBuildBr(comp_ctx->builder, check_call_succ)) {
  690. aot_set_last_error("llvm build br failed.");
  691. goto fail;
  692. }
  693. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_func_not_linked);
  694. // lazy link other module's export func.
  695. if (!call_aot_resolve_and_call_import_func(comp_ctx, func_ctx,
  696. import_func_idx,
  697. func_type,
  698. param_types + 1, param_values + 1,
  699. param_count, param_cell_num,
  700. ret_type, wasm_ret_type, &value_ret_incoming[1],
  701. &result))
  702. goto fail;
  703. /* Create function return block if it isn't created */
  704. if (!create_func_return_block(comp_ctx, func_ctx))
  705. goto fail;
  706. if (!(if_res_succ = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, result, I8_ZERO,
  707. "if_res_succ"))) {
  708. aot_set_last_error("llvm build icmp failed.");
  709. goto fail;
  710. }
  711. /* Create condition br */
  712. if (!LLVMBuildCondBr(comp_ctx->builder, if_res_succ, check_call_succ,
  713. func_ctx->func_return_block)) {
  714. aot_set_last_error("llvm build cond br failed.");
  715. goto fail;
  716. }
  717. if (wasm_ret_type != VALUE_TYPE_VOID) {
  718. LLVMAddIncoming(value_ret_phi, &value_ret_incoming[1], &block_func_not_linked, 1);
  719. }
  720. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_except_succ);
  721. #else
  722. value_ret_phi = aot_emit_call_native(comp_ctx, func_ctx,
  723. func_idx, func_type,
  724. ret_type, ext_ret_count,
  725. param_types, param_values,
  726. param_count);
  727. if (!value_ret_phi)
  728. goto fail;
  729. /* Check whether exception was thrown when executing the function */
  730. if (!check_exception_thrown(comp_ctx, func_ctx))
  731. goto fail;
  732. #endif
  733. }
  734. else {
  735. if (comp_ctx->is_indirect_mode) {
  736. LLVMTypeRef func_ptr_type;
  737. if (!(func_ptr_type = LLVMPointerType(
  738. func_ctxes[func_idx - import_func_count]->func_type,
  739. 0))) {
  740. aot_set_last_error("construct func ptr type failed.");
  741. goto fail;
  742. }
  743. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->func_ptrs,
  744. func_ptr_type, func_idx))) {
  745. goto fail;
  746. }
  747. }
  748. else {
  749. func = func_ctxes[func_idx - import_func_count]->func;
  750. }
  751. aot_func = func_ctxes[func_idx - import_func_count]->aot_func;
  752. callee_cell_num =
  753. aot_func->param_cell_num + aot_func->local_cell_num + 1;
  754. if (comp_ctx->enable_bound_check
  755. && !check_stack_boundary(comp_ctx, func_ctx, callee_cell_num))
  756. goto fail;
  757. /* Call the function */
  758. if (!(value_ret_phi =
  759. LLVMBuildCall(comp_ctx->builder, func, param_values,
  760. (uint32)param_count + 1 + ext_ret_count,
  761. (func_type->result_count > 0 ? "call" : "")))) {
  762. aot_set_last_error("LLVM build call failed.");
  763. goto fail;
  764. }
  765. /* Set calling convention for the call with the func's calling
  766. convention */
  767. LLVMSetInstructionCallConv(value_ret_phi, LLVMGetFunctionCallConv(func));
  768. if (tail_call)
  769. LLVMSetTailCall(value_ret_phi, true);
  770. /* Check whether there was exception thrown when executing
  771. the function */
  772. if (!tail_call && !check_exception_thrown(comp_ctx, func_ctx))
  773. goto fail;
  774. }
  775. if (func_type->result_count > 0) {
  776. /* Push the first result to stack */
  777. PUSH(value_ret_phi, func_type->types[func_type->param_count]);
  778. /* Load extra result from its address and push to stack */
  779. for (i = 0; i < ext_ret_count; i++) {
  780. snprintf(buf, sizeof(buf), "func%d_ext_ret%d", func_idx, i);
  781. if (!(ext_ret =
  782. LLVMBuildLoad(comp_ctx->builder,
  783. param_values[1 + param_count + i], buf))) {
  784. aot_set_last_error("llvm build load failed.");
  785. goto fail;
  786. }
  787. PUSH(ext_ret, ext_ret_types[i]);
  788. }
  789. }
  790. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  791. if (comp_ctx->enable_aux_stack_frame) {
  792. if (!call_aot_free_frame_func(comp_ctx, func_ctx))
  793. goto fail;
  794. }
  795. #endif
  796. ret = true;
  797. fail:
  798. if (param_types)
  799. wasm_runtime_free(param_types);
  800. if (param_values)
  801. wasm_runtime_free(param_values);
  802. return ret;
  803. }
  804. static bool
  805. call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  806. AOTFuncType *aot_func_type,
  807. LLVMValueRef func_type_idx, LLVMValueRef table_idx,
  808. LLVMValueRef table_elem_idx,
  809. LLVMTypeRef *param_types,
  810. LLVMValueRef *param_values, uint32 param_count,
  811. uint32 param_cell_num, uint32 result_count,
  812. uint8 *wasm_ret_types, LLVMValueRef *value_rets,
  813. LLVMValueRef *p_res)
  814. {
  815. LLVMTypeRef func_type, func_ptr_type, func_param_types[6];
  816. LLVMTypeRef ret_type, ret_ptr_type, elem_ptr_type;
  817. LLVMValueRef func, ret_idx, ret_ptr, elem_idx, elem_ptr;
  818. LLVMValueRef func_param_values[6], res = NULL;
  819. char buf[32], *func_name = "aot_call_indirect";
  820. uint32 i, cell_num = 0, ret_cell_num, argv_cell_num;
  821. /* prepare function type of aot_call_indirect */
  822. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  823. func_param_types[1] = I32_TYPE; /* table_idx */
  824. func_param_types[2] = I32_TYPE; /* table_elem_idx */
  825. func_param_types[3] = I32_TYPE; /* argc */
  826. func_param_types[4] = INT32_PTR_TYPE; /* argv */
  827. if (!(func_type =
  828. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  829. aot_set_last_error("llvm add function type failed.");
  830. return false;
  831. }
  832. /* prepare function pointer */
  833. if (comp_ctx->is_jit_mode) {
  834. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  835. aot_set_last_error("create LLVM function type failed.");
  836. return false;
  837. }
  838. /* JIT mode, call the function directly */
  839. if (!(func = I64_CONST((uint64)(uintptr_t)aot_call_indirect))
  840. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  841. aot_set_last_error("create LLVM value failed.");
  842. return false;
  843. }
  844. }
  845. else if (comp_ctx->is_indirect_mode) {
  846. int32 func_index;
  847. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  848. aot_set_last_error("create LLVM function type failed.");
  849. return false;
  850. }
  851. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  852. if (func_index < 0) {
  853. return false;
  854. }
  855. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  856. func_ptr_type, func_index))) {
  857. return false;
  858. }
  859. }
  860. else {
  861. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  862. && !(func =
  863. LLVMAddFunction(comp_ctx->module, func_name, func_type))) {
  864. aot_set_last_error("add LLVM function failed.");
  865. return false;
  866. }
  867. }
  868. ret_cell_num = wasm_get_cell_num(wasm_ret_types, result_count);
  869. argv_cell_num =
  870. param_cell_num > ret_cell_num ? param_cell_num : ret_cell_num;
  871. if (argv_cell_num > 64) {
  872. aot_set_last_error("prepare native arguments failed: "
  873. "maximum 64 parameter cell number supported.");
  874. return false;
  875. }
  876. /* prepare frame_lp */
  877. for (i = 0; i < param_count; i++) {
  878. if (!(elem_idx = I32_CONST(cell_num))
  879. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  880. aot_set_last_error("llvm add const or pointer type failed.");
  881. return false;
  882. }
  883. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  884. if (!(elem_ptr = LLVMBuildInBoundsGEP(
  885. comp_ctx->builder, func_ctx->argv_buf, &elem_idx, 1, buf))
  886. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  887. elem_ptr_type, buf))) {
  888. aot_set_last_error("llvm build bit cast failed.");
  889. return false;
  890. }
  891. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  892. elem_ptr))) {
  893. aot_set_last_error("llvm build store failed.");
  894. return false;
  895. }
  896. LLVMSetAlignment(res, 1);
  897. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  898. }
  899. func_param_values[0] = func_ctx->exec_env;
  900. func_param_values[1] = table_idx;
  901. func_param_values[2] = table_elem_idx;
  902. func_param_values[3] = I32_CONST(param_cell_num);
  903. func_param_values[4] = func_ctx->argv_buf;
  904. if (!func_param_values[3]) {
  905. aot_set_last_error("llvm create const failed.");
  906. return false;
  907. }
  908. /* call aot_call_indirect() function */
  909. if (!(res = LLVMBuildCall(comp_ctx->builder, func, func_param_values, 5,
  910. "res"))) {
  911. aot_set_last_error("llvm build call failed.");
  912. return false;
  913. }
  914. /* get function result values */
  915. cell_num = 0;
  916. for (i = 0; i < result_count; i++) {
  917. ret_type = TO_LLVM_TYPE(wasm_ret_types[i]);
  918. if (!(ret_idx = I32_CONST(cell_num))
  919. || !(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  920. aot_set_last_error("llvm add const or pointer type failed.");
  921. return false;
  922. }
  923. snprintf(buf, sizeof(buf), "argv_ret%d", i);
  924. if (!(ret_ptr = LLVMBuildInBoundsGEP(
  925. comp_ctx->builder, func_ctx->argv_buf, &ret_idx, 1, buf))
  926. || !(ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ret_ptr,
  927. ret_ptr_type, buf))) {
  928. aot_set_last_error("llvm build GEP or bit cast failed.");
  929. return false;
  930. }
  931. snprintf(buf, sizeof(buf), "ret%d", i);
  932. if (!(value_rets[i] = LLVMBuildLoad(comp_ctx->builder, ret_ptr, buf))) {
  933. aot_set_last_error("llvm build load failed.");
  934. return false;
  935. }
  936. cell_num += wasm_value_type_cell_num(wasm_ret_types[i]);
  937. }
  938. *p_res = res;
  939. return true;
  940. }
  941. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  942. static bool
  943. call_aot_call_indirect_with_type_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  944. AOTFuncType *aot_func_type, LLVMValueRef func_type_idx,
  945. LLVMValueRef table_idx, LLVMValueRef table_elem_idx,
  946. LLVMTypeRef *param_types, LLVMValueRef *param_values,
  947. uint32 param_count, uint32 param_cell_num,
  948. uint32 result_count, uint8 *wasm_ret_types,
  949. LLVMValueRef *value_rets, LLVMValueRef *p_res)
  950. {
  951. LLVMTypeRef func_type, func_ptr_type, func_param_types[6];
  952. LLVMTypeRef ret_type, ret_ptr_type, elem_ptr_type;
  953. LLVMValueRef func, ret_idx, ret_ptr, elem_idx, elem_ptr;
  954. LLVMValueRef func_param_values[7], res = NULL;
  955. char buf[32], *func_name = "aot_call_indirect_with_type";
  956. uint32 i, cell_num = 0, ret_cell_num, argv_cell_num;
  957. /* prepare function type of aot_call_indirect_with_type */
  958. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  959. func_param_types[1] = I32_TYPE; /* table_idx */
  960. func_param_types[2] = I32_TYPE; /* table_elem_idx */
  961. func_param_types[3] = I32_TYPE; /* type idx */
  962. func_param_types[4] = I32_TYPE; /* argc */
  963. func_param_types[5] = INT32_PTR_TYPE; /* argv */
  964. if (!(func_type = LLVMFunctionType(INT8_TYPE, func_param_types, 6, false))) {
  965. aot_set_last_error("llvm add function type failed.");
  966. return false;
  967. }
  968. /* prepare function pointer */
  969. if (comp_ctx->is_jit_mode) {
  970. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  971. aot_set_last_error("create LLVM function type failed.");
  972. return false;
  973. }
  974. /* JIT mode, call the function directly */
  975. if (!(func = I64_CONST((uint64)(uintptr_t)aot_call_indirect_with_type))
  976. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  977. aot_set_last_error("create LLVM value failed.");
  978. return false;
  979. }
  980. }
  981. else {
  982. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  983. && !(func = LLVMAddFunction(comp_ctx->module,
  984. func_name, func_type))) {
  985. aot_set_last_error("add LLVM function failed.");
  986. return false;
  987. }
  988. }
  989. ret_cell_num = wasm_get_cell_num(wasm_ret_types, result_count);
  990. argv_cell_num = param_cell_num > ret_cell_num ? param_cell_num : ret_cell_num;
  991. if (argv_cell_num > 64) {
  992. aot_set_last_error("prepare native arguments failed: "
  993. "maximum 64 parameter cell number supported.");
  994. return false;
  995. }
  996. /* prepare frame_lp */
  997. for (i = 0; i < param_count; i++) {
  998. if (!(elem_idx = I32_CONST(cell_num))
  999. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  1000. aot_set_last_error("llvm add const or pointer type failed.");
  1001. return false;
  1002. }
  1003. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  1004. if (!(elem_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  1005. func_ctx->argv_buf, &elem_idx, 1, buf))
  1006. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  1007. elem_ptr_type, buf))) {
  1008. aot_set_last_error("llvm build bit cast failed.");
  1009. return false;
  1010. }
  1011. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i], elem_ptr))) {
  1012. aot_set_last_error("llvm build store failed.");
  1013. return false;
  1014. }
  1015. LLVMSetAlignment(res, 1);
  1016. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  1017. }
  1018. func_param_values[0] = func_ctx->exec_env;
  1019. func_param_values[1] = table_idx;
  1020. func_param_values[2] = table_elem_idx;
  1021. func_param_values[3] = func_type_idx;
  1022. func_param_values[4] = I32_CONST(param_cell_num);
  1023. func_param_values[5] = func_ctx->argv_buf;
  1024. if (!func_param_values[4]) {
  1025. aot_set_last_error("llvm create const failed.");
  1026. return false;
  1027. }
  1028. /* call aot_call_indirect() function */
  1029. if (!(res = LLVMBuildCall(comp_ctx->builder, func,
  1030. func_param_values, 6, "res"))) {
  1031. aot_set_last_error("llvm build call failed.");
  1032. return false;
  1033. }
  1034. /* get function result values */
  1035. cell_num = 0;
  1036. for (i = 0; i < result_count; i++) {
  1037. ret_type = TO_LLVM_TYPE(wasm_ret_types[i]);
  1038. if (!(ret_idx = I32_CONST(cell_num))
  1039. || !(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  1040. aot_set_last_error("llvm add const or pointer type failed.");
  1041. return false;
  1042. }
  1043. snprintf(buf, sizeof(buf), "argv_ret%d", i);
  1044. if (!(ret_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  1045. func_ctx->argv_buf, &ret_idx, 1, buf))
  1046. || !(ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ret_ptr,
  1047. ret_ptr_type, buf))) {
  1048. aot_set_last_error("llvm build GEP or bit cast failed.");
  1049. return false;
  1050. }
  1051. snprintf(buf, sizeof(buf), "ret%d", i);
  1052. if (!(value_rets[i] = LLVMBuildLoad(comp_ctx->builder, ret_ptr, buf))) {
  1053. aot_set_last_error("llvm build load failed.");
  1054. return false;
  1055. }
  1056. cell_num += wasm_value_type_cell_num(wasm_ret_types[i]);
  1057. }
  1058. *p_res = res;
  1059. return true;
  1060. }
  1061. #if 0
  1062. static bool
  1063. aot_compile_program_cache_resolve_result(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1064. LLVMValueRef program_inst_value, LLVMValueRef elem_idx,
  1065. LLVMValueRef func_ptr, LLVMValueRef module_inst)
  1066. {
  1067. LLVMValueRef const_cache_offset, const_func_ptr_offset, const_cached_aot_inst_offset;
  1068. LLVMValueRef const_cache_entry_size, const_cache_line_size;
  1069. LLVMValueRef update_cache_entry_offset, cache_entry_offset, cache_line_offset, resolve_caches_offset, cached_func_offset;
  1070. LLVMValueRef resolve_id_offset, cached_inst_offset;
  1071. LLVMValueRef cached_func_val_offset, cached_func_val;
  1072. LLVMValueRef resolve_cache, mask, cache_line_index;
  1073. LLVMValueRef program_inst, if_program_inst_exists, const_zero_ref;
  1074. LLVMValueRef if_cache_entry_available;
  1075. LLVMBasicBlockRef block_cache_resolve_result, block_call_func, block_check_second_entry, block_update_entry, block_reset_update_entry;
  1076. LLVMTypeRef const_pointer_type;
  1077. if (comp_ctx->pointer_size == sizeof(uint64)) {
  1078. // note: for convenience, the offsets are hardcoded here according to target machine word size
  1079. // in the future, they will be automatically generated before compilation.
  1080. const_pointer_type = INT64_PTR_TYPE;
  1081. const_cache_offset = I32_CONST(40);
  1082. const_func_ptr_offset = I32_CONST(8);
  1083. const_cached_aot_inst_offset = I32_CONST(16);
  1084. const_zero_ref = I64_ZERO;
  1085. } else {
  1086. const_pointer_type = INT32_PTR_TYPE;
  1087. const_cache_offset = I32_CONST(24);
  1088. const_func_ptr_offset = I32_CONST(4);
  1089. const_cached_aot_inst_offset = I32_CONST(8);
  1090. const_zero_ref = I32_ZERO;
  1091. }
  1092. const_cache_entry_size = I32_CONST(sizeof(wasm_resolving_cache_entry));
  1093. const_cache_line_size = I32_CONST(sizeof(wasm_resolving_cache_entry) * 2);
  1094. block_cache_resolve_result =
  1095. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1096. "block_cache_resolve_result");
  1097. block_call_func =
  1098. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1099. "block_call_func");
  1100. block_update_entry =
  1101. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1102. "block_update_entry");
  1103. LLVMMoveBasicBlockAfter(block_cache_resolve_result,
  1104. LLVMGetInsertBlock(comp_ctx->builder));
  1105. LLVMMoveBasicBlockAfter(block_call_func,
  1106. block_cache_resolve_result);
  1107. //aot_call_debugtrap_intrinsic(comp_ctx, func_ctx);
  1108. if_program_inst_exists = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, program_inst_value, const_zero_ref, "if_program_inst_exists");
  1109. LLVMBuildCondBr(comp_ctx->builder, if_program_inst_exists, block_cache_resolve_result, block_call_func);
  1110. // block at which check first cache entry if available
  1111. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_cache_resolve_result);
  1112. program_inst = LLVMBuildIntToPtr(comp_ctx->builder, program_inst_value, INT8_PTR_TYPE, "program_inst");
  1113. if (!(resolve_caches_offset =
  1114. LLVMBuildGEP(comp_ctx->builder, program_inst, &const_cache_offset, 1,
  1115. "resolving_cache_offset"))) {
  1116. HANDLE_FAILURE("LLVMBuildGEP");
  1117. return false;
  1118. }
  1119. if (!(resolve_caches_offset =
  1120. LLVMBuildBitCast(comp_ctx->builder, resolve_caches_offset,
  1121. comp_ctx->basic_types.int8_pptr_type, "resolving_cache_offset"))) {
  1122. HANDLE_FAILURE("LLVMBuildBitCast");
  1123. return false;
  1124. }
  1125. if (!(resolve_cache = LLVMBuildLoad(comp_ctx->builder, resolve_caches_offset, "resolve_cache"))) {
  1126. HANDLE_FAILURE("LLVMBuildLoad");
  1127. return false;
  1128. }
  1129. mask = I32_CONST(PROGRAM_RESOLVING_CACHE_LINE_COUNT - 1);
  1130. cache_line_index = LLVMBuildAnd(comp_ctx->builder, elem_idx, mask, "cache_line_index");
  1131. cache_line_offset = LLVMBuildMul(comp_ctx->builder, cache_line_index, const_cache_line_size, "cache_line_rela_offset");
  1132. //cache_line_offset = LLVMBuildGEP(comp_ctx->builder, resolve_cache, &offset, 1, "cache_line_offset");
  1133. //cache_entry_offset = cache_line_offset;
  1134. cached_func_offset = LLVMBuildAdd(comp_ctx->builder, cache_line_offset, const_func_ptr_offset, "cached_func_offset");
  1135. cached_func_offset = LLVMBuildGEP(comp_ctx->builder, resolve_cache, &cached_func_offset, 1, "cached_func_offset");
  1136. if (!(cached_func_val_offset =
  1137. LLVMBuildBitCast(comp_ctx->builder, cached_func_offset,
  1138. const_pointer_type, "cached_func_value_offset"))) {
  1139. HANDLE_FAILURE("LLVMBuildBitCast");
  1140. return false;
  1141. }
  1142. if (!(cached_func_val = LLVMBuildLoad(comp_ctx->builder, cached_func_val_offset, "cached_func_val"))) {
  1143. HANDLE_FAILURE("LLVMBuildLoad");
  1144. return false;
  1145. }
  1146. if (!(if_cache_entry_available = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  1147. cached_func_val, const_zero_ref, "if_cache_entry_available"))) {
  1148. aot_set_last_error("llvm build icmp failed.");
  1149. return false;
  1150. }
  1151. // block at which update cache entry
  1152. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_update_entry);
  1153. update_cache_entry_offset = LLVMBuildPhi(comp_ctx->builder, I32_TYPE, "cache_entry_offset_phi");
  1154. resolve_id_offset = LLVMBuildGEP(comp_ctx->builder, resolve_cache, &update_cache_entry_offset, 1, "cache_line_offset");
  1155. if (!(resolve_id_offset =
  1156. LLVMBuildBitCast(comp_ctx->builder, resolve_id_offset,
  1157. INT32_PTR_TYPE, "resolve_id_offset"))) {
  1158. HANDLE_FAILURE("LLVMBuildBitCast");
  1159. return false;
  1160. }
  1161. LLVMBuildStore(comp_ctx->builder, elem_idx, resolve_id_offset);
  1162. cached_func_offset = LLVMBuildAdd(comp_ctx->builder, update_cache_entry_offset, const_func_ptr_offset, "cached_func_offset");
  1163. cached_func_offset = LLVMBuildGEP(comp_ctx->builder, resolve_cache, &cached_func_offset, 1, "cached_func_offset");
  1164. if (!(cached_func_offset =
  1165. LLVMBuildBitCast(comp_ctx->builder, cached_func_offset,
  1166. comp_ctx->basic_types.int8_pptr_type, "cached_func_offset"))) {
  1167. HANDLE_FAILURE("LLVMBuildBitCast");
  1168. return false;
  1169. }
  1170. LLVMBuildStore(comp_ctx->builder, func_ptr, cached_func_offset);
  1171. cached_inst_offset = LLVMBuildAdd(comp_ctx->builder, update_cache_entry_offset, const_cached_aot_inst_offset, "cached_inst_offset");
  1172. cached_inst_offset = LLVMBuildGEP(comp_ctx->builder, resolve_cache, &cached_inst_offset, 1, "cached_inst_offset");
  1173. if (!(cached_inst_offset =
  1174. LLVMBuildBitCast(comp_ctx->builder, cached_inst_offset,
  1175. comp_ctx->basic_types.int8_pptr_type, "cached_inst_offset"))) {
  1176. HANDLE_FAILURE("LLVMBuildBitCast");
  1177. return false;
  1178. }
  1179. LLVMBuildStore(comp_ctx->builder, module_inst, cached_inst_offset);
  1180. LLVMBuildBr(comp_ctx->builder, block_call_func);
  1181. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_cache_resolve_result);
  1182. LLVMAddIncoming(update_cache_entry_offset, &cache_line_offset, &block_cache_resolve_result, 1);
  1183. block_check_second_entry =
  1184. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1185. "block_check_second_entry");
  1186. block_reset_update_entry =
  1187. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1188. "block_reset_update_entry");
  1189. LLVMMoveBasicBlockAfter(block_check_second_entry,
  1190. LLVMGetInsertBlock(comp_ctx->builder));
  1191. LLVMMoveBasicBlockAfter(block_reset_update_entry,
  1192. block_check_second_entry);
  1193. LLVMMoveBasicBlockAfter(block_update_entry,
  1194. block_reset_update_entry);
  1195. if (!LLVMBuildCondBr(comp_ctx->builder, if_cache_entry_available,
  1196. block_update_entry, block_check_second_entry)) {
  1197. aot_set_last_error("llvm build cond br failed.");
  1198. return false;
  1199. }
  1200. // block at which check second cache entry if available
  1201. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_check_second_entry);
  1202. cache_entry_offset = LLVMBuildAdd(comp_ctx->builder, cache_line_offset, const_cache_entry_size, "next_cache_entry_offset");
  1203. cached_func_offset = LLVMBuildAdd(comp_ctx->builder, cache_entry_offset, const_func_ptr_offset, "cached_func_offset");
  1204. cached_func_offset = LLVMBuildGEP(comp_ctx->builder, resolve_cache, &cached_func_offset, 1, "cached_func_offset");
  1205. if (!(cached_func_val_offset =
  1206. LLVMBuildBitCast(comp_ctx->builder, cached_func_offset,
  1207. const_pointer_type, "cached_func_value_offset"))) {
  1208. HANDLE_FAILURE("LLVMBuildBitCast");
  1209. return false;
  1210. }
  1211. if (!(cached_func_val = LLVMBuildLoad(comp_ctx->builder, cached_func_val_offset, "cached_func_val"))) {
  1212. HANDLE_FAILURE("LLVMBuildLoad");
  1213. return false;
  1214. }
  1215. LLVMAddIncoming(update_cache_entry_offset, &cache_entry_offset, &block_check_second_entry, 1);
  1216. if (!(if_cache_entry_available = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  1217. cached_func_val, const_zero_ref, "if_cache_entry_available"))) {
  1218. aot_set_last_error("llvm build icmp failed.");
  1219. return false;
  1220. }
  1221. if (!LLVMBuildCondBr(comp_ctx->builder, if_cache_entry_available,
  1222. block_update_entry, block_reset_update_entry)) {
  1223. aot_set_last_error("llvm build cond br failed.");
  1224. return false;
  1225. }
  1226. // block at which reset update entry to first entry
  1227. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_reset_update_entry);
  1228. LLVMAddIncoming(update_cache_entry_offset, &cache_line_offset, &block_reset_update_entry, 1);
  1229. LLVMBuildBr(comp_ctx->builder, block_update_entry);
  1230. // block at which call the real function.
  1231. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_func);
  1232. return true;
  1233. }
  1234. #endif
  1235. static bool
  1236. aot_compile_program_op_call_indirect_cached_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1237. LLVMValueRef elem_idx, LLVMValueRef program_inst,
  1238. LLVMTypeRef *param_types, LLVMValueRef *param_values,
  1239. uint32 param_count, LLVMTypeRef ret_type, uint32 result_count,
  1240. LLVMValueRef *result_phis,
  1241. LLVMBasicBlockRef block_return)
  1242. {
  1243. LLVMValueRef const_cache_offset, const_func_ptr_offset, offset;
  1244. LLVMValueRef cache_entry_size, cache_line_size, cache_entry_offset, cache_line_offset, resolve_caches_offset, cached_func_offset;
  1245. LLVMValueRef env_ref, env_aot_inst_offset, const_env_aot_inst_offset, cached_aot_inst_offset, const_cached_aot_inst_offset, saved_inst, cached_inst;
  1246. LLVMValueRef resolve_cache, mask, cache_line_index, resolve_id;
  1247. LLVMValueRef if_cache_hit;
  1248. LLVMValueRef func, func_ptr, value_ret, ext_ret;
  1249. LLVMTypeRef llvm_func_type, llvm_func_ptr_type;
  1250. LLVMBasicBlockRef block_curr, block_cache_hit, block_cache_lookup_retry, block_resolving;
  1251. uint32 i;
  1252. char buf[128] = "";
  1253. if (comp_ctx->pointer_size == sizeof(uint64)) {
  1254. // note: for convenience, the offsets are hardcoded here according to target machine word size
  1255. // in the future, they will be automatically generated before compilation.
  1256. const_cache_offset = I32_CONST(40);
  1257. const_func_ptr_offset = I32_CONST(8);
  1258. const_env_aot_inst_offset = I32_CONST(16);
  1259. const_cached_aot_inst_offset = I32_CONST(16);
  1260. } else {
  1261. const_cache_offset = I32_CONST(24);
  1262. const_func_ptr_offset = I32_CONST(4);
  1263. const_env_aot_inst_offset = I32_CONST(8);
  1264. const_cached_aot_inst_offset = I32_CONST(8);
  1265. }
  1266. cache_entry_size = I32_CONST(sizeof(wasm_resolving_cache_entry));
  1267. cache_line_size = I32_CONST(sizeof(wasm_resolving_cache_entry) * 2);
  1268. if (!(resolve_caches_offset =
  1269. LLVMBuildGEP(comp_ctx->builder, program_inst, &const_cache_offset, 1,
  1270. "resolving_cache_offset"))) {
  1271. HANDLE_FAILURE("LLVMBuildGEP");
  1272. return false;
  1273. }
  1274. if (!(resolve_caches_offset =
  1275. LLVMBuildBitCast(comp_ctx->builder, resolve_caches_offset,
  1276. comp_ctx->basic_types.int8_pptr_type, "resolving_cache_offset"))) {
  1277. HANDLE_FAILURE("LLVMBuildBitCast");
  1278. return false;
  1279. }
  1280. if (!(resolve_cache = LLVMBuildLoad(comp_ctx->builder, resolve_caches_offset, "resolve_cache"))) {
  1281. HANDLE_FAILURE("LLVMBuildLoad");
  1282. return false;
  1283. }
  1284. mask = I32_CONST(PROGRAM_RESOLVING_CACHE_LINE_COUNT - 1);
  1285. cache_line_index = LLVMBuildAnd(comp_ctx->builder, elem_idx, mask, "cache_line_index");
  1286. offset = LLVMBuildMul(comp_ctx->builder, cache_line_index, cache_line_size, "cache_line_rela_offset");
  1287. cache_line_offset = LLVMBuildGEP(comp_ctx->builder, resolve_cache, &offset, 1, "cache_line_offset");
  1288. if (!(cache_line_offset =
  1289. LLVMBuildBitCast(comp_ctx->builder, cache_line_offset,
  1290. INT32_PTR_TYPE, "resolve_id_offset"))) {
  1291. HANDLE_FAILURE("LLVMBuildBitCast");
  1292. return false;
  1293. }
  1294. if (!(resolve_id = LLVMBuildLoad(comp_ctx->builder, cache_line_offset, "resolve id"))) {
  1295. HANDLE_FAILURE("LLVMBuildLoad");
  1296. return false;
  1297. }
  1298. if (!(if_cache_hit = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  1299. elem_idx, resolve_id, "if_cache_hit"))) {
  1300. aot_set_last_error("llvm build icmp failed.");
  1301. return false;
  1302. }
  1303. block_cache_hit =
  1304. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1305. "cache_hit");
  1306. block_cache_lookup_retry =
  1307. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1308. "cache_lookup_retry");
  1309. block_resolving =
  1310. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1311. "block_resolving");
  1312. LLVMMoveBasicBlockAfter(block_cache_hit,
  1313. LLVMGetInsertBlock(comp_ctx->builder));
  1314. LLVMMoveBasicBlockAfter(block_cache_lookup_retry,
  1315. block_cache_hit);
  1316. LLVMMoveBasicBlockAfter(block_resolving,
  1317. block_cache_lookup_retry);
  1318. if (!LLVMBuildCondBr(comp_ctx->builder, if_cache_hit,
  1319. block_cache_hit, block_cache_lookup_retry)) {
  1320. aot_set_last_error("llvm build cond br failed.");
  1321. return false;
  1322. }
  1323. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_cache_lookup_retry);
  1324. cache_entry_offset = LLVMBuildAdd(comp_ctx->builder, offset, cache_entry_size, "second_entry_offset");
  1325. cache_entry_offset = LLVMBuildGEP(comp_ctx->builder, resolve_cache, &cache_entry_offset, 1, "cache_func_ptr_offset");
  1326. if (!(cache_entry_offset =
  1327. LLVMBuildBitCast(comp_ctx->builder, cache_entry_offset,
  1328. INT32_PTR_TYPE, "resolve_id_offset"))) {
  1329. HANDLE_FAILURE("LLVMBuildBitCast");
  1330. return false;
  1331. }
  1332. if (!(resolve_id = LLVMBuildLoad(comp_ctx->builder, cache_entry_offset, "resolve id"))) {
  1333. HANDLE_FAILURE("LLVMBuildLoad");
  1334. return false;
  1335. }
  1336. if (!(if_cache_hit = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  1337. elem_idx, resolve_id, "if_cache_hit"))) {
  1338. aot_set_last_error("llvm build icmp failed.");
  1339. return false;
  1340. }
  1341. if (!LLVMBuildCondBr(comp_ctx->builder, if_cache_hit,
  1342. block_cache_hit, block_resolving)) {
  1343. aot_set_last_error("llvm build cond br failed.");
  1344. return false;
  1345. }
  1346. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_cache_hit);
  1347. cached_func_offset = LLVMBuildAdd(comp_ctx->builder, offset, const_func_ptr_offset, "cached_func_ptr_offset");
  1348. cached_func_offset = LLVMBuildGEP(comp_ctx->builder, resolve_cache, &cached_func_offset, 1, "cache_func_ptr_offset");
  1349. cached_func_offset = LLVMBuildBitCast(comp_ctx->builder, cached_func_offset, comp_ctx->basic_types.int8_pptr_type, "func_ptr");
  1350. if (!(func_ptr = LLVMBuildLoad(comp_ctx->builder, cached_func_offset, "func_ptr"))) {
  1351. aot_set_last_error("llvm build load failed.");
  1352. return false;
  1353. }
  1354. if (!(llvm_func_type =
  1355. LLVMFunctionType(ret_type, param_types, param_count, false))
  1356. || !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
  1357. aot_set_last_error("llvm add function type failed.");
  1358. return false;
  1359. }
  1360. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  1361. llvm_func_ptr_type, "indirect_func"))) {
  1362. aot_set_last_error("llvm build bit cast failed.");
  1363. return false;
  1364. }
  1365. env_ref = LLVMBuildBitCast(comp_ctx->builder, func_ctx->exec_env, INT8_PTR_TYPE, "env_ref");
  1366. if (!(env_aot_inst_offset =
  1367. LLVMBuildGEP(comp_ctx->builder, env_ref, &const_env_aot_inst_offset, 1,
  1368. "env_aot_inst_offset"))) {
  1369. HANDLE_FAILURE("LLVMBuildGEP");
  1370. return false;
  1371. }
  1372. if (!(env_aot_inst_offset =
  1373. LLVMBuildBitCast(comp_ctx->builder, env_aot_inst_offset,
  1374. comp_ctx->basic_types.int8_pptr_type, "env_aot_inst_offset"))) {
  1375. HANDLE_FAILURE("LLVMBuildBitCast");
  1376. return false;
  1377. }
  1378. if (!(saved_inst = LLVMBuildLoad(comp_ctx->builder, env_aot_inst_offset, "old_inst"))) {
  1379. aot_set_last_error("llvm build load failed.");
  1380. return false;
  1381. }
  1382. cached_aot_inst_offset = LLVMBuildAdd(comp_ctx->builder, offset, const_cached_aot_inst_offset, "cached_aot_inst_offset");
  1383. cached_aot_inst_offset = LLVMBuildGEP(comp_ctx->builder, resolve_cache, &cached_aot_inst_offset, 1, "cached_aot_inst_offset");
  1384. cached_aot_inst_offset = LLVMBuildBitCast(comp_ctx->builder, cached_aot_inst_offset, comp_ctx->basic_types.int8_pptr_type, "cached_aot_inst_offset");
  1385. if (!(cached_inst = LLVMBuildLoad(comp_ctx->builder, cached_aot_inst_offset, "cached_aot_inst"))) {
  1386. aot_set_last_error("llvm build load failed.");
  1387. return false;
  1388. }
  1389. //aot_call_debugtrap_intrinsic(comp_ctx, func_ctx);
  1390. LLVMBuildStore(comp_ctx->builder, cached_inst, env_aot_inst_offset);
  1391. if (!(value_ret = LLVMBuildCall(comp_ctx->builder, func, param_values,
  1392. param_count,
  1393. result_count > 0 ? "ret" : ""))) {
  1394. aot_set_last_error("llvm build call failed.");
  1395. return false;
  1396. }
  1397. /* Check whether exception was thrown when executing the function */
  1398. if (!check_exception_thrown(comp_ctx, func_ctx))
  1399. return false;
  1400. LLVMBuildStore(comp_ctx->builder, saved_inst, env_aot_inst_offset);
  1401. if (result_count > 0) {
  1402. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1403. /* Push the first result to stack */
  1404. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  1405. /* Load extra result from its address and push to stack */
  1406. for (i = 1; i < result_count; i++) {
  1407. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  1408. if (!(ext_ret =
  1409. LLVMBuildLoad(comp_ctx->builder,
  1410. param_values[param_count + i], buf))) {
  1411. aot_set_last_error("llvm build load failed.");
  1412. return false;
  1413. }
  1414. LLVMAddIncoming(result_phis[i], &ext_ret, &block_cache_hit, 1);
  1415. }
  1416. }
  1417. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1418. aot_set_last_error("llvm build br failed.");
  1419. return false;
  1420. }
  1421. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_resolving);
  1422. return true;
  1423. }
  1424. static LLVMValueRef
  1425. aot_compile_program_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1426. LLVMValueRef type_idx, AOTFuncType *func_type,
  1427. LLVMValueRef elem_idx, LLVMValueRef *result_phis, LLVMValueRef elem_idx_phi,
  1428. uint32 tbl_idx,
  1429. LLVMTypeRef *param_types, LLVMValueRef *param_values,
  1430. uint32 param_count, LLVMTypeRef ret_type,
  1431. LLVMBasicBlockRef block_func, LLVMBasicBlockRef block_non_program_mode, LLVMBasicBlockRef block_return)
  1432. {
  1433. LLVMValueRef offset, elem_idx_base, elem_idx_in_tbl;
  1434. LLVMBasicBlockRef block_program_call_indirect, block_call_indirect_and_export, block_curr, block_call_local_tbl;
  1435. LLVMValueRef program_offset, inst_id_offset, program_inst, program_inst_value, inst_id, inst_id_from_elem, if_program_inst_exists, if_call_local_table;
  1436. LLVMValueRef *value_rets = NULL;
  1437. LLVMValueRef tbl_idx_value, res, const_zero_ref;
  1438. LLVMTypeRef pointer_type;
  1439. uint32 param_cell_num;
  1440. uint32 func_param_count, func_result_count;
  1441. uint8 *wasm_ret_types;
  1442. uint32 total_size = 0, i;
  1443. bool ret = false;
  1444. func_param_count = func_type->param_count;
  1445. func_result_count = func_type->result_count;
  1446. if (!(offset = I32_CONST(offsetof(AOTModuleInstance, program)))) {
  1447. HANDLE_FAILURE("LLVMConstInt");
  1448. return NULL;
  1449. }
  1450. if (!(program_offset =
  1451. LLVMBuildGEP(comp_ctx->builder, func_ctx->aot_inst, &offset, 1,
  1452. "program_inst_offset"))) {
  1453. HANDLE_FAILURE("LLVMBuildGEP");
  1454. return NULL;
  1455. }
  1456. if (comp_ctx->pointer_size == sizeof(uint64)) {
  1457. pointer_type = INT64_PTR_TYPE;
  1458. const_zero_ref = I64_ZERO;
  1459. } else {
  1460. pointer_type = INT32_PTR_TYPE;
  1461. const_zero_ref = I32_ZERO;
  1462. }
  1463. if (!(program_offset =
  1464. LLVMBuildBitCast(comp_ctx->builder, program_offset,
  1465. pointer_type, "program_inst_offset"))) {
  1466. HANDLE_FAILURE("LLVMBuildBitCast");
  1467. return NULL;
  1468. }
  1469. if (!(program_inst_value = LLVMBuildLoad(comp_ctx->builder, program_offset, "program"))) {
  1470. HANDLE_FAILURE("LLVMBuildLoad");
  1471. return NULL;
  1472. }
  1473. if (!(if_program_inst_exists = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  1474. program_inst_value, const_zero_ref, "is_in_dlopen_mode"))) {
  1475. aot_set_last_error("llvm build icmp failed.");
  1476. return NULL;
  1477. }
  1478. //if (!(non_program_mode =
  1479. // LLVMAppendBasicBlockInContext(comp_ctx->context,
  1480. // func_ctx->func,
  1481. // "non_program_mode"))) {
  1482. // aot_set_last_error("llvm add basic block failed.");
  1483. // return false;
  1484. //}
  1485. #if 0
  1486. // old way to invoke block_call_import, reserved for debugging.
  1487. if (!LLVMBuildCondBr(comp_ctx->builder, if_program_inst_exists,
  1488. block_non_program_mode, block_func)) {
  1489. aot_set_last_error("llvm build cond br failed.");
  1490. return false;
  1491. }
  1492. #endif
  1493. //#if 0
  1494. block_program_call_indirect =
  1495. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1496. "program_call_indirect");
  1497. block_call_indirect_and_export =
  1498. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1499. "call_indirect_and_export");
  1500. block_call_local_tbl =
  1501. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1502. "block_call_local_tbl");
  1503. LLVMMoveBasicBlockAfter(block_program_call_indirect,
  1504. LLVMGetInsertBlock(comp_ctx->builder));
  1505. LLVMMoveBasicBlockAfter(block_call_indirect_and_export,
  1506. block_program_call_indirect);
  1507. LLVMMoveBasicBlockAfter(block_call_local_tbl,
  1508. block_call_indirect_and_export);
  1509. if (!LLVMBuildCondBr(comp_ctx->builder, if_program_inst_exists,
  1510. block_non_program_mode, block_program_call_indirect)) {
  1511. aot_set_last_error("llvm build cond br failed.");
  1512. return NULL;
  1513. }
  1514. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_program_call_indirect);
  1515. //aot_call_debugtrap_intrinsic(comp_ctx, func_ctx);
  1516. if (!(program_offset =
  1517. LLVMBuildBitCast(comp_ctx->builder, program_offset,
  1518. comp_ctx->basic_types.int8_pptr_type, "program_inst_offset"))) {
  1519. HANDLE_FAILURE("LLVMBuildBitCast");
  1520. return NULL;
  1521. }
  1522. if (!(program_inst = LLVMBuildLoad(comp_ctx->builder, program_offset, "program"))) {
  1523. HANDLE_FAILURE("LLVMBuildLoad");
  1524. return NULL;
  1525. }
  1526. #if 0
  1527. if (!aot_compile_program_op_call_indirect_cached_func(comp_ctx, func_ctx,
  1528. elem_idx, program_inst,
  1529. param_types, param_values,
  1530. param_count, ret_type, func_result_count,
  1531. result_phis, block_return)) {
  1532. return NULL;
  1533. }
  1534. #endif
  1535. if (!(offset = I32_CONST(offsetof(AOTModuleInstance, inst_id)))) {
  1536. HANDLE_FAILURE("LLVMConstInt");
  1537. return NULL;
  1538. }
  1539. if (!(inst_id_offset =
  1540. LLVMBuildGEP(comp_ctx->builder, func_ctx->aot_inst, &offset, 1,
  1541. "inst_id_offset"))) {
  1542. HANDLE_FAILURE("LLVMBuildGEP");
  1543. return NULL;
  1544. }
  1545. if (!(inst_id_offset =
  1546. LLVMBuildBitCast(comp_ctx->builder, inst_id_offset,
  1547. INT32_PTR_TYPE, "inst_id_offset"))) {
  1548. HANDLE_FAILURE("LLVMBuildBitCast");
  1549. return NULL;
  1550. }
  1551. if (!(inst_id = LLVMBuildLoad(comp_ctx->builder, inst_id_offset, "inst_id"))) {
  1552. HANDLE_FAILURE("LLVMBuildLoad");
  1553. return NULL;
  1554. }
  1555. inst_id_from_elem = LLVMBuildAShr(comp_ctx->builder, elem_idx, I32_CONST(TABLE_SPACE_BITS_LEN), "inst_id_from_elem_id");
  1556. inst_id_from_elem = LLVMBuildAdd(comp_ctx->builder, inst_id_from_elem, I32_ONE, "inst_id_from_elem_id");
  1557. if (!(if_call_local_table = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  1558. inst_id_from_elem, inst_id, "if_call_local_table_func"))) {
  1559. aot_set_last_error("llvm build icmp failed.");
  1560. return NULL;
  1561. }
  1562. if (!LLVMBuildCondBr(comp_ctx->builder, if_call_local_table,
  1563. block_call_local_tbl, block_call_indirect_and_export)) {
  1564. aot_set_last_error("llvm build cond br failed.");
  1565. return NULL;
  1566. }
  1567. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_indirect_and_export);
  1568. if (!aot_compile_program_op_call_indirect_cached_func(comp_ctx, func_ctx,
  1569. elem_idx, program_inst,
  1570. param_types, param_values,
  1571. param_count, ret_type, func_result_count,
  1572. result_phis, block_return)) {
  1573. return NULL;
  1574. }
  1575. /* Allocate memory for result values */
  1576. if (func_result_count > 0) {
  1577. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1578. if (total_size >= UINT32_MAX
  1579. || !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
  1580. aot_set_last_error("allocate memory failed.");
  1581. return NULL;
  1582. }
  1583. memset(value_rets, 0, (uint32)total_size);
  1584. }
  1585. param_cell_num = func_type->param_cell_num;
  1586. wasm_ret_types = func_type->types + func_type->param_count;
  1587. tbl_idx_value = I32_CONST(tbl_idx);
  1588. if (!tbl_idx_value) {
  1589. aot_set_last_error("llvm create const failed.");
  1590. goto fail;
  1591. }
  1592. if (!call_aot_call_indirect_with_type_func(comp_ctx, func_ctx,
  1593. func_type, type_idx,
  1594. tbl_idx_value, elem_idx,
  1595. param_types + 1, param_values + 1,
  1596. func_param_count, param_cell_num,
  1597. func_result_count, wasm_ret_types,
  1598. value_rets, &res)) {
  1599. goto fail;
  1600. }
  1601. /* Check whether exception was thrown when executing the function */
  1602. if (!check_call_return(comp_ctx, func_ctx, res))
  1603. goto fail;
  1604. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1605. for (i = 0; i < func_result_count; i++) {
  1606. LLVMAddIncoming(result_phis[i], &value_rets[i], &block_curr, 1);
  1607. }
  1608. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1609. aot_set_last_error("llvm build br failed.");
  1610. goto fail;
  1611. }
  1612. //#endif
  1613. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_local_tbl);
  1614. inst_id = LLVMBuildAdd(comp_ctx->builder, inst_id, I32_NEG_ONE, "inst_idx");
  1615. elem_idx_base = LLVMBuildMul(comp_ctx->builder, inst_id, I32_CONST(TABLE_SPACE_SLOT_SIZE), "elem_idx_base");
  1616. elem_idx_in_tbl = LLVMBuildSub(comp_ctx->builder, elem_idx, elem_idx_base, "elem_idx_in_tbl");
  1617. LLVMAddIncoming(elem_idx_phi, &elem_idx_in_tbl, &block_call_local_tbl, 1);
  1618. LLVMBuildBr(comp_ctx->builder, block_non_program_mode);
  1619. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_non_program_mode);
  1620. ret = true;
  1621. fail:
  1622. if (value_rets)
  1623. wasm_runtime_free(value_rets);
  1624. if (!ret)
  1625. return NULL;
  1626. return program_inst_value;
  1627. }
  1628. #endif
  1629. bool
  1630. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1631. uint32 type_idx, uint32 tbl_idx)
  1632. {
  1633. AOTFuncType *func_type;
  1634. LLVMValueRef tbl_idx_value, elem_idx, table_elem, func_idx;
  1635. LLVMValueRef ftype_idx_ptr, ftype_idx, ftype_idx_const;
  1636. LLVMValueRef cmp_elem_idx, cmp_func_idx, cmp_ftype_idx;
  1637. LLVMValueRef func, func_ptr, table_size_const;
  1638. LLVMValueRef ext_ret_offset, ext_ret_ptr, ext_ret, res;
  1639. LLVMValueRef *param_values = NULL, *value_rets = NULL;
  1640. LLVMValueRef *result_phis = NULL, value_ret, import_func_count;
  1641. LLVMTypeRef *param_types = NULL, ret_type;
  1642. LLVMTypeRef llvm_func_type, llvm_func_ptr_type;
  1643. LLVMTypeRef ext_ret_ptr_type;
  1644. LLVMBasicBlockRef check_elem_idx_succ, check_ftype_idx_succ;
  1645. LLVMBasicBlockRef check_func_idx_succ, block_return, block_curr;
  1646. LLVMBasicBlockRef block_call_import, block_call_non_import;
  1647. LLVMValueRef offset;
  1648. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1649. LLVMValueRef program_inst_value, elem_idx_phi;//, origin_elem_idx;
  1650. LLVMBasicBlockRef block_non_program_mode;
  1651. #endif
  1652. uint32 total_param_count, func_param_count, func_result_count;
  1653. uint32 ext_cell_num, param_cell_num, i, j;
  1654. uint8 wasm_ret_type, *wasm_ret_types;
  1655. uint64 total_size;
  1656. char buf[32];
  1657. bool ret = false;
  1658. /* Check function type index */
  1659. if (type_idx >= comp_ctx->comp_data->func_type_count) {
  1660. aot_set_last_error("function type index out of range");
  1661. return false;
  1662. }
  1663. /* Find the equivalent function type whose type index is the smallest:
  1664. the callee function's type index is also converted to the smallest
  1665. one in wasm loader, so we can just check whether the two type indexes
  1666. are equal (the type index of call_indirect opcode and callee func),
  1667. we don't need to check whether the whole function types are equal,
  1668. including param types and result types. */
  1669. type_idx = wasm_get_smallest_type_idx(comp_ctx->comp_data->func_types,
  1670. comp_ctx->comp_data->func_type_count,
  1671. type_idx);
  1672. ftype_idx_const = I32_CONST(type_idx);
  1673. CHECK_LLVM_CONST(ftype_idx_const);
  1674. func_type = comp_ctx->comp_data->func_types[type_idx];
  1675. func_param_count = func_type->param_count;
  1676. func_result_count = func_type->result_count;
  1677. POP_I32(elem_idx);
  1678. block_call_import =
  1679. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1680. "call_import");
  1681. block_call_non_import =
  1682. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1683. "call_non_import");
  1684. block_return =
  1685. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1686. "func_return");
  1687. if (!block_call_import || !block_call_non_import || !block_return) {
  1688. aot_set_last_error("llvm add basic block failed.");
  1689. goto fail;
  1690. }
  1691. LLVMMoveBasicBlockAfter(block_call_import,
  1692. LLVMGetInsertBlock(comp_ctx->builder));
  1693. LLVMMoveBasicBlockAfter(block_call_non_import, block_call_import);
  1694. LLVMMoveBasicBlockAfter(block_return, block_call_non_import);
  1695. //prepare function parameters and returns
  1696. /* Initialize parameter types of the LLVM function */
  1697. total_param_count = 1 + func_param_count;
  1698. /* Extra function results' addresses (except the first one) are
  1699. * appended to aot function parameters. */
  1700. if (func_result_count > 1)
  1701. total_param_count += func_result_count - 1;
  1702. total_size = sizeof(LLVMTypeRef) * (uint64)total_param_count;
  1703. if (total_size >= UINT32_MAX
  1704. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  1705. aot_set_last_error("allocate memory failed.");
  1706. goto fail;
  1707. }
  1708. /* Prepare param types */
  1709. j = 0;
  1710. param_types[j++] = comp_ctx->exec_env_type;
  1711. for (i = 0; i < func_param_count; i++)
  1712. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  1713. for (i = 1; i < func_result_count; i++, j++) {
  1714. param_types[j] =
  1715. TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1716. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  1717. aot_set_last_error("llvm get pointer type failed.");
  1718. goto fail;
  1719. }
  1720. }
  1721. /* Resolve return type of the LLVM function */
  1722. if (func_result_count) {
  1723. wasm_ret_type = func_type->types[func_param_count];
  1724. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  1725. }
  1726. else {
  1727. wasm_ret_type = VALUE_TYPE_VOID;
  1728. ret_type = VOID_TYPE;
  1729. }
  1730. /* Allocate memory for parameters */
  1731. total_size = sizeof(LLVMValueRef) * (uint64)total_param_count;
  1732. if (total_size >= UINT32_MAX
  1733. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  1734. aot_set_last_error("allocate memory failed.");
  1735. goto fail;
  1736. }
  1737. /* First parameter is exec env */
  1738. j = 0;
  1739. param_values[j++] = func_ctx->exec_env;
  1740. /* Pop parameters from stack */
  1741. for (i = func_param_count - 1; (int32)i >= 0; i--)
  1742. POP(param_values[i + j], func_type->types[i]);
  1743. /* Prepare extra parameters */
  1744. ext_cell_num = 0;
  1745. for (i = 1; i < func_result_count; i++) {
  1746. ext_ret_offset = I32_CONST(ext_cell_num);
  1747. CHECK_LLVM_CONST(ext_ret_offset);
  1748. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i - 1);
  1749. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  1750. func_ctx->argv_buf,
  1751. &ext_ret_offset, 1, buf))) {
  1752. aot_set_last_error("llvm build GEP failed.");
  1753. goto fail;
  1754. }
  1755. ext_ret_ptr_type = param_types[func_param_count + i];
  1756. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i - 1);
  1757. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder,
  1758. ext_ret_ptr, ext_ret_ptr_type,
  1759. buf))) {
  1760. aot_set_last_error("llvm build bit cast failed.");
  1761. goto fail;
  1762. }
  1763. param_values[func_param_count + i] = ext_ret_ptr;
  1764. ext_cell_num += wasm_value_type_cell_num(
  1765. func_type->types[func_param_count + i]);
  1766. }
  1767. if (ext_cell_num > 64) {
  1768. aot_set_last_error("prepare call-indirect arguments failed: "
  1769. "maximum 64 extra cell number supported.");
  1770. goto fail;
  1771. }
  1772. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1773. /* Add result phis for return block */
  1774. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  1775. if (func_result_count > 0) {
  1776. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1777. if (total_size >= UINT32_MAX
  1778. || !(result_phis = wasm_runtime_malloc((uint32)total_size))) {
  1779. aot_set_last_error("allocate memory failed.");
  1780. goto fail;
  1781. }
  1782. memset(result_phis, 0, (uint32)total_size);
  1783. for (i = 0; i < func_result_count; i++) {
  1784. LLVMTypeRef tmp_type =
  1785. TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1786. if (!(result_phis[i] = LLVMBuildPhi(comp_ctx->builder,
  1787. tmp_type, "phi"))) {
  1788. aot_set_last_error("llvm build phi failed.");
  1789. goto fail;
  1790. }
  1791. }
  1792. }
  1793. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  1794. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1795. /* Add basic blocks */
  1796. block_non_program_mode =
  1797. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  1798. "block_non_program_mode");
  1799. LLVMMoveBasicBlockAfter(block_non_program_mode,
  1800. block_curr);
  1801. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_non_program_mode);
  1802. elem_idx_phi = LLVMBuildPhi(comp_ctx->builder, I32_TYPE, "phi");
  1803. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  1804. LLVMAddIncoming(elem_idx_phi, &elem_idx, &block_curr, 1);
  1805. if (!(program_inst_value = aot_compile_program_op_call_indirect(comp_ctx, func_ctx, ftype_idx_const, func_type, elem_idx, result_phis, elem_idx_phi,
  1806. tbl_idx, param_types, param_values, total_param_count, ret_type,
  1807. block_call_import, block_non_program_mode, block_return)))
  1808. goto fail;
  1809. // origin_elem_idx = elem_idx;
  1810. elem_idx = elem_idx_phi;
  1811. #endif
  1812. /* get the cur size of the table instance */
  1813. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  1814. + offsetof(AOTTableInstance, cur_size)))) {
  1815. HANDLE_FAILURE("LLVMConstInt");
  1816. goto fail;
  1817. }
  1818. if (!(table_size_const =
  1819. LLVMBuildGEP(comp_ctx->builder, func_ctx->aot_inst, &offset, 1,
  1820. "cur_size_i8p"))) {
  1821. HANDLE_FAILURE("LLVMBuildGEP");
  1822. goto fail;
  1823. }
  1824. if (!(table_size_const =
  1825. LLVMBuildBitCast(comp_ctx->builder, table_size_const,
  1826. INT32_PTR_TYPE, "cur_siuze_i32p"))) {
  1827. HANDLE_FAILURE("LLVMBuildBitCast");
  1828. goto fail;
  1829. }
  1830. if (!(table_size_const = LLVMBuildLoad(comp_ctx->builder, table_size_const, "cur_size"))) {
  1831. HANDLE_FAILURE("LLVMBuildLoad");
  1832. goto fail;
  1833. }
  1834. /* Check if (uint32)elem index >= table size */
  1835. if (!(cmp_elem_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGE,
  1836. elem_idx, table_size_const,
  1837. "cmp_elem_idx"))) {
  1838. aot_set_last_error("llvm build icmp failed.");
  1839. goto fail;
  1840. }
  1841. /* Throw exception if elem index >= table size */
  1842. if (!(check_elem_idx_succ =
  1843. LLVMAppendBasicBlockInContext(comp_ctx->context,
  1844. func_ctx->func,
  1845. "check_elem_idx_succ"))) {
  1846. aot_set_last_error("llvm add basic block failed.");
  1847. goto fail;
  1848. }
  1849. LLVMMoveBasicBlockAfter(check_elem_idx_succ,
  1850. LLVMGetInsertBlock(comp_ctx->builder));
  1851. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNDEFINED_ELEMENT, true,
  1852. cmp_elem_idx, check_elem_idx_succ)))
  1853. goto fail;
  1854. /* load data as i32* */
  1855. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  1856. + offsetof(AOTTableInstance, data)))) {
  1857. HANDLE_FAILURE("LLVMConstInt");
  1858. goto fail;
  1859. }
  1860. if (!(table_elem = LLVMBuildGEP(comp_ctx->builder, func_ctx->aot_inst,
  1861. &offset, 1, "table_elem_i8p"))) {
  1862. aot_set_last_error("llvm build add failed.");
  1863. goto fail;
  1864. }
  1865. if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem,
  1866. INT32_PTR_TYPE, "table_elem_i32p"))) {
  1867. HANDLE_FAILURE("LLVMBuildBitCast");
  1868. goto fail;
  1869. }
  1870. /* Load function index */
  1871. if (!(table_elem = LLVMBuildGEP(comp_ctx->builder, table_elem, &elem_idx, 1,
  1872. "table_elem"))) {
  1873. HANDLE_FAILURE("LLVMBuildNUWAdd");
  1874. goto fail;
  1875. }
  1876. if (!(func_idx =
  1877. LLVMBuildLoad(comp_ctx->builder, table_elem, "func_idx"))) {
  1878. aot_set_last_error("llvm build load failed.");
  1879. goto fail;
  1880. }
  1881. /* Check if func_idx == -1 */
  1882. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, func_idx,
  1883. I32_NEG_ONE, "cmp_func_idx"))) {
  1884. aot_set_last_error("llvm build icmp failed.");
  1885. goto fail;
  1886. }
  1887. /* Throw exception if func_idx == -1 */
  1888. if (!(check_func_idx_succ = LLVMAppendBasicBlockInContext(
  1889. comp_ctx->context, func_ctx->func, "check_func_idx_succ"))) {
  1890. aot_set_last_error("llvm add basic block failed.");
  1891. goto fail;
  1892. }
  1893. LLVMMoveBasicBlockAfter(check_func_idx_succ,
  1894. LLVMGetInsertBlock(comp_ctx->builder));
  1895. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNINITIALIZED_ELEMENT,
  1896. true, cmp_func_idx, check_func_idx_succ)))
  1897. goto fail;
  1898. /* Load function type index */
  1899. if (!(ftype_idx_ptr = LLVMBuildInBoundsGEP(
  1900. comp_ctx->builder, func_ctx->func_type_indexes, &func_idx, 1,
  1901. "ftype_idx_ptr"))) {
  1902. aot_set_last_error("llvm build inbounds gep failed.");
  1903. goto fail;
  1904. }
  1905. if (!(ftype_idx =
  1906. LLVMBuildLoad(comp_ctx->builder, ftype_idx_ptr, "ftype_idx"))) {
  1907. aot_set_last_error("llvm build load failed.");
  1908. goto fail;
  1909. }
  1910. /* Check if function type index not equal */
  1911. if (!(cmp_ftype_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, ftype_idx,
  1912. ftype_idx_const, "cmp_ftype_idx"))) {
  1913. aot_set_last_error("llvm build icmp failed.");
  1914. goto fail;
  1915. }
  1916. /* Throw exception if ftype_idx != ftype_idx_const */
  1917. if (!(check_ftype_idx_succ = LLVMAppendBasicBlockInContext(
  1918. comp_ctx->context, func_ctx->func, "check_ftype_idx_succ"))) {
  1919. aot_set_last_error("llvm add basic block failed.");
  1920. goto fail;
  1921. }
  1922. LLVMMoveBasicBlockAfter(check_ftype_idx_succ,
  1923. LLVMGetInsertBlock(comp_ctx->builder));
  1924. if (!(aot_emit_exception(comp_ctx, func_ctx,
  1925. EXCE_INVALID_FUNCTION_TYPE_INDEX, true,
  1926. cmp_ftype_idx, check_ftype_idx_succ)))
  1927. goto fail;
  1928. #if WASM_ENABLE_THREAD_MGR != 0
  1929. /* Insert suspend check point */
  1930. if (comp_ctx->enable_thread_mgr) {
  1931. if (!check_suspend_flags(comp_ctx, func_ctx))
  1932. goto fail;
  1933. }
  1934. #endif
  1935. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1936. if (comp_ctx->enable_aux_stack_frame) {
  1937. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx))
  1938. goto fail;
  1939. }
  1940. #endif
  1941. import_func_count = I32_CONST(comp_ctx->comp_data->import_func_count);
  1942. CHECK_LLVM_CONST(import_func_count);
  1943. /* Check if func_idx < import_func_count */
  1944. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT, func_idx,
  1945. import_func_count, "cmp_func_idx"))) {
  1946. aot_set_last_error("llvm build icmp failed.");
  1947. goto fail;
  1948. }
  1949. /* If func_idx < import_func_count, jump to call import block,
  1950. else jump to call non-import block */
  1951. if (!LLVMBuildCondBr(comp_ctx->builder, cmp_func_idx, block_call_import,
  1952. block_call_non_import)) {
  1953. aot_set_last_error("llvm build cond br failed.");
  1954. goto fail;
  1955. }
  1956. /* Translate call import block */
  1957. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_import);
  1958. /* Allocate memory for result values */
  1959. if (func_result_count > 0) {
  1960. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1961. if (total_size >= UINT32_MAX
  1962. || !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
  1963. aot_set_last_error("allocate memory failed.");
  1964. goto fail;
  1965. }
  1966. memset(value_rets, 0, (uint32)total_size);
  1967. }
  1968. param_cell_num = func_type->param_cell_num;
  1969. wasm_ret_types = func_type->types + func_type->param_count;
  1970. tbl_idx_value = I32_CONST(tbl_idx);
  1971. if (!tbl_idx_value) {
  1972. aot_set_last_error("llvm create const failed.");
  1973. goto fail;
  1974. }
  1975. if (!call_aot_call_indirect_func(
  1976. comp_ctx, func_ctx, func_type, ftype_idx, tbl_idx_value, elem_idx,
  1977. param_types + 1, param_values + 1, func_param_count, param_cell_num,
  1978. func_result_count, wasm_ret_types, value_rets, &res))
  1979. goto fail;
  1980. /* Check whether exception was thrown when executing the function */
  1981. if (!check_call_return(comp_ctx, func_ctx, res))
  1982. goto fail;
  1983. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1984. for (i = 0; i < func_result_count; i++) {
  1985. LLVMAddIncoming(result_phis[i], &value_rets[i], &block_curr, 1);
  1986. }
  1987. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1988. aot_set_last_error("llvm build br failed.");
  1989. goto fail;
  1990. }
  1991. /* Translate call non-import block */
  1992. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_non_import);
  1993. if (comp_ctx->enable_bound_check
  1994. && !check_stack_boundary(comp_ctx, func_ctx,
  1995. param_cell_num + ext_cell_num
  1996. + 1
  1997. /* Reserve some local variables */
  1998. + 16))
  1999. goto fail;
  2000. /* Load function pointer */
  2001. if (!(func_ptr =
  2002. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->func_ptrs,
  2003. &func_idx, 1, "func_ptr_tmp"))) {
  2004. aot_set_last_error("llvm build inbounds gep failed.");
  2005. goto fail;
  2006. }
  2007. if (!(func_ptr = LLVMBuildLoad(comp_ctx->builder, func_ptr, "func_ptr"))) {
  2008. aot_set_last_error("llvm build load failed.");
  2009. goto fail;
  2010. }
  2011. if (!(llvm_func_type =
  2012. LLVMFunctionType(ret_type, param_types, total_param_count, false))
  2013. || !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
  2014. aot_set_last_error("llvm add function type failed.");
  2015. goto fail;
  2016. }
  2017. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  2018. llvm_func_ptr_type, "indirect_func"))) {
  2019. aot_set_last_error("llvm build bit cast failed.");
  2020. goto fail;
  2021. }
  2022. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  2023. // seems in aot mode, resolve cache is not helpful for local table function.
  2024. //aot_compile_program_cache_resolve_result(comp_ctx, func_ctx, program_inst_value, origin_elem_idx, func_ptr, func_ctx->aot_inst);
  2025. #endif
  2026. if (!(value_ret = LLVMBuildCall(comp_ctx->builder, func, param_values,
  2027. total_param_count,
  2028. func_result_count > 0 ? "ret" : ""))) {
  2029. aot_set_last_error("llvm build call failed.");
  2030. goto fail;
  2031. }
  2032. /* Check whether exception was thrown when executing the function */
  2033. if (!check_exception_thrown(comp_ctx, func_ctx))
  2034. goto fail;
  2035. if (func_result_count > 0) {
  2036. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  2037. /* Push the first result to stack */
  2038. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  2039. /* Load extra result from its address and push to stack */
  2040. for (i = 1; i < func_result_count; i++) {
  2041. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  2042. if (!(ext_ret =
  2043. LLVMBuildLoad(comp_ctx->builder,
  2044. param_values[func_param_count + i], buf))) {
  2045. aot_set_last_error("llvm build load failed.");
  2046. goto fail;
  2047. }
  2048. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  2049. }
  2050. }
  2051. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  2052. aot_set_last_error("llvm build br failed.");
  2053. goto fail;
  2054. }
  2055. /* Translate function return block */
  2056. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  2057. for (i = 0; i < func_result_count; i++) {
  2058. PUSH(result_phis[i], func_type->types[func_param_count + i]);
  2059. }
  2060. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  2061. if (comp_ctx->enable_aux_stack_frame) {
  2062. if (!call_aot_free_frame_func(comp_ctx, func_ctx))
  2063. goto fail;
  2064. }
  2065. #endif
  2066. ret = true;
  2067. fail:
  2068. if (param_values)
  2069. wasm_runtime_free(param_values);
  2070. if (param_types)
  2071. wasm_runtime_free(param_types);
  2072. if (value_rets)
  2073. wasm_runtime_free(value_rets);
  2074. if (result_phis)
  2075. wasm_runtime_free(result_phis);
  2076. return ret;
  2077. }
  2078. bool
  2079. aot_compile_op_ref_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  2080. {
  2081. PUSH_I32(REF_NULL);
  2082. return true;
  2083. fail:
  2084. return false;
  2085. }
  2086. bool
  2087. aot_compile_op_ref_is_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  2088. {
  2089. LLVMValueRef lhs, res;
  2090. POP_I32(lhs);
  2091. if (!(res = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, lhs, REF_NULL,
  2092. "cmp_w_null"))) {
  2093. HANDLE_FAILURE("LLVMBuildICmp");
  2094. goto fail;
  2095. }
  2096. if (!(res = LLVMBuildZExt(comp_ctx->builder, res, I32_TYPE, "r_i"))) {
  2097. HANDLE_FAILURE("LLVMBuildZExt");
  2098. goto fail;
  2099. }
  2100. PUSH_I32(res);
  2101. return true;
  2102. fail:
  2103. return false;
  2104. }
  2105. bool
  2106. aot_compile_op_ref_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  2107. uint32 func_idx)
  2108. {
  2109. LLVMValueRef ref_idx;
  2110. if (!(ref_idx = I32_CONST(func_idx))) {
  2111. HANDLE_FAILURE("LLVMConstInt");
  2112. goto fail;
  2113. }
  2114. PUSH_I32(ref_idx);
  2115. return true;
  2116. fail:
  2117. return false;
  2118. }