aot_emit_function.c 68 KB

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