aot_emit_function.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  252. static bool
  253. call_aot_alloc_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  254. LLVMValueRef func_idx)
  255. {
  256. LLVMValueRef param_values[2], ret_value, value, func;
  257. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  258. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  259. LLVMBasicBlockRef frame_alloc_fail, frame_alloc_success;
  260. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  261. param_types[0] = comp_ctx->exec_env_type;
  262. param_types[1] = I32_TYPE;
  263. ret_type = INT8_TYPE;
  264. if (comp_ctx->is_jit_mode)
  265. GET_AOT_FUNCTION(llvm_jit_alloc_frame, 2);
  266. else
  267. GET_AOT_FUNCTION(aot_alloc_frame, 2);
  268. param_values[0] = func_ctx->exec_env;
  269. param_values[1] = func_idx;
  270. if (!(ret_value =
  271. LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  272. 2, "call_aot_alloc_frame"))) {
  273. aot_set_last_error("llvm build call failed.");
  274. return false;
  275. }
  276. if (!(ret_value = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, ret_value,
  277. I8_ZERO, "frame_alloc_ret"))) {
  278. aot_set_last_error("llvm build icmp failed.");
  279. return false;
  280. }
  281. ADD_BASIC_BLOCK(frame_alloc_fail, "frame_alloc_fail");
  282. ADD_BASIC_BLOCK(frame_alloc_success, "frame_alloc_success");
  283. LLVMMoveBasicBlockAfter(frame_alloc_fail, block_curr);
  284. LLVMMoveBasicBlockAfter(frame_alloc_success, block_curr);
  285. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, frame_alloc_success,
  286. frame_alloc_fail)) {
  287. aot_set_last_error("llvm build cond br failed.");
  288. return false;
  289. }
  290. /* If frame alloc failed, return this function
  291. so the runtime can catch the exception */
  292. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_fail);
  293. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  294. return false;
  295. }
  296. LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_success);
  297. return true;
  298. fail:
  299. return false;
  300. }
  301. static bool
  302. call_aot_free_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  303. {
  304. LLVMValueRef param_values[1], ret_value, value, func;
  305. LLVMTypeRef param_types[1], ret_type, func_type, func_ptr_type;
  306. param_types[0] = comp_ctx->exec_env_type;
  307. ret_type = INT8_TYPE;
  308. if (comp_ctx->is_jit_mode)
  309. GET_AOT_FUNCTION(llvm_jit_free_frame, 1);
  310. else
  311. GET_AOT_FUNCTION(aot_free_frame, 1);
  312. param_values[0] = func_ctx->exec_env;
  313. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  314. param_values, 1, "call_aot_free_frame"))) {
  315. aot_set_last_error("llvm build call failed.");
  316. return false;
  317. }
  318. return true;
  319. fail:
  320. return false;
  321. }
  322. #endif /* end of (WASM_ENABLE_DUMP_CALL_STACK != 0) \
  323. || (WASM_ENABLE_PERF_PROFILING != 0) */
  324. /**
  325. * Check whether the app address and its buffer are inside the linear memory,
  326. * if no, throw exception
  327. */
  328. static bool
  329. check_app_addr_and_convert(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  330. bool is_str_arg, LLVMValueRef app_addr,
  331. LLVMValueRef buf_size,
  332. LLVMValueRef *p_native_addr_converted)
  333. {
  334. LLVMTypeRef func_type, func_ptr_type, func_param_types[5];
  335. LLVMValueRef func, func_param_values[5], res, native_addr_ptr;
  336. char *func_name = "aot_check_app_addr_and_convert";
  337. /* prepare function type of aot_check_app_addr_and_convert */
  338. func_param_types[0] = comp_ctx->aot_inst_type; /* module_inst */
  339. func_param_types[1] = INT8_TYPE; /* is_str_arg */
  340. func_param_types[2] = I32_TYPE; /* app_offset */
  341. func_param_types[3] = I32_TYPE; /* buf_size */
  342. func_param_types[4] =
  343. comp_ctx->basic_types.int8_pptr_type; /* p_native_addr */
  344. if (!(func_type =
  345. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  346. aot_set_last_error("llvm add function type failed.");
  347. return false;
  348. }
  349. /* prepare function pointer */
  350. if (comp_ctx->is_jit_mode) {
  351. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  352. aot_set_last_error("create LLVM function type failed.");
  353. return false;
  354. }
  355. /* JIT mode, call the function directly */
  356. if (!(func =
  357. I64_CONST((uint64)(uintptr_t)jit_check_app_addr_and_convert))
  358. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  359. aot_set_last_error("create LLVM value failed.");
  360. return false;
  361. }
  362. }
  363. else if (comp_ctx->is_indirect_mode) {
  364. int32 func_index;
  365. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  366. aot_set_last_error("create LLVM function type failed.");
  367. return false;
  368. }
  369. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  370. if (func_index < 0) {
  371. return false;
  372. }
  373. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  374. func_ptr_type, func_index))) {
  375. return false;
  376. }
  377. }
  378. else {
  379. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  380. && !(func =
  381. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  382. aot_set_last_error("add LLVM function failed.");
  383. return false;
  384. }
  385. }
  386. if (!(native_addr_ptr = LLVMBuildBitCast(
  387. comp_ctx->builder, func_ctx->argv_buf,
  388. comp_ctx->basic_types.int8_pptr_type, "p_native_addr"))) {
  389. aot_set_last_error("llvm build bit cast failed.");
  390. return false;
  391. }
  392. func_param_values[0] = func_ctx->aot_inst;
  393. func_param_values[1] = I8_CONST(is_str_arg);
  394. func_param_values[2] = app_addr;
  395. func_param_values[3] = buf_size;
  396. func_param_values[4] = native_addr_ptr;
  397. if (!func_param_values[1]) {
  398. aot_set_last_error("llvm create const failed.");
  399. return false;
  400. }
  401. /* call aot_check_app_addr_and_convert() function */
  402. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  403. func_param_values, 5, "res"))) {
  404. aot_set_last_error("llvm build call failed.");
  405. return false;
  406. }
  407. /* Check whether exception was thrown when executing the function */
  408. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  409. && !check_call_return(comp_ctx, func_ctx, res)) {
  410. return false;
  411. }
  412. if (!(*p_native_addr_converted =
  413. LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, native_addr_ptr,
  414. "native_addr"))) {
  415. aot_set_last_error("llvm build load failed.");
  416. return false;
  417. }
  418. return true;
  419. }
  420. static void
  421. aot_estimate_and_record_stack_usage_for_function_call(
  422. const AOTCompContext *comp_ctx, AOTFuncContext *caller_func_ctx,
  423. const AOTFuncType *callee_func_type)
  424. {
  425. unsigned int size;
  426. if (!(comp_ctx->enable_stack_bound_check
  427. || comp_ctx->enable_stack_estimation)) {
  428. return;
  429. }
  430. size =
  431. aot_estimate_stack_usage_for_function_call(comp_ctx, callee_func_type);
  432. /*
  433. * only record the max value, assuming that LLVM emits machine code
  434. * which rewinds the stack before making the next call in the
  435. * function.
  436. */
  437. if (caller_func_ctx->stack_consumption_for_func_call < size) {
  438. caller_func_ctx->stack_consumption_for_func_call = size;
  439. }
  440. }
  441. bool
  442. aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  443. uint32 func_idx, bool tail_call)
  444. {
  445. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  446. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  447. uint32 func_count = comp_ctx->func_ctx_count, param_cell_num = 0;
  448. uint32 ext_ret_cell_num = 0, cell_num = 0;
  449. AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
  450. AOTFuncType *func_type;
  451. LLVMTypeRef *param_types = NULL, ret_type;
  452. LLVMTypeRef ext_ret_ptr_type;
  453. LLVMValueRef *param_values = NULL, value_ret = NULL, func;
  454. LLVMValueRef import_func_idx, res;
  455. LLVMValueRef ext_ret, ext_ret_ptr, ext_ret_idx;
  456. int32 i, j = 0, param_count, result_count, ext_ret_count;
  457. uint64 total_size;
  458. uint8 wasm_ret_type;
  459. uint8 *ext_ret_types = NULL;
  460. const char *signature = NULL;
  461. bool ret = false;
  462. char buf[32];
  463. #if WASM_ENABLE_THREAD_MGR != 0
  464. /* Insert suspend check point */
  465. if (comp_ctx->enable_thread_mgr) {
  466. if (!check_suspend_flags(comp_ctx, func_ctx))
  467. return false;
  468. }
  469. #endif
  470. /* Check function index */
  471. if (func_idx >= import_func_count + func_count) {
  472. aot_set_last_error("Function index out of range.");
  473. return false;
  474. }
  475. /* Get function type */
  476. if (func_idx < import_func_count) {
  477. func_type = import_funcs[func_idx].func_type;
  478. signature = import_funcs[func_idx].signature;
  479. }
  480. else {
  481. func_type =
  482. func_ctxes[func_idx - import_func_count]->aot_func->func_type;
  483. }
  484. aot_estimate_and_record_stack_usage_for_function_call(comp_ctx, func_ctx,
  485. func_type);
  486. /* Get param cell number */
  487. param_cell_num = func_type->param_cell_num;
  488. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  489. if (comp_ctx->enable_aux_stack_frame) {
  490. LLVMValueRef func_idx_const;
  491. if (!(func_idx_const = I32_CONST(func_idx))) {
  492. aot_set_last_error("llvm build const failed.");
  493. return false;
  494. }
  495. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx_const))
  496. return false;
  497. }
  498. #endif
  499. /* Allocate memory for parameters.
  500. * Parameters layout:
  501. * - exec env
  502. * - wasm function's parameters
  503. * - extra results'(except the first one) addresses
  504. */
  505. param_count = (int32)func_type->param_count;
  506. result_count = (int32)func_type->result_count;
  507. ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  508. total_size =
  509. sizeof(LLVMValueRef) * (uint64)(param_count + 1 + ext_ret_count);
  510. if (total_size >= UINT32_MAX
  511. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  512. aot_set_last_error("allocate memory failed.");
  513. return false;
  514. }
  515. /* First parameter is exec env */
  516. param_values[j++] = func_ctx->exec_env;
  517. /* Pop parameters from stack */
  518. for (i = param_count - 1; i >= 0; i--)
  519. POP(param_values[i + j], func_type->types[i]);
  520. /* Set parameters for multiple return values, the first return value
  521. is returned by function return value, and the other return values
  522. are returned by function parameters with pointer types */
  523. if (ext_ret_count > 0) {
  524. ext_ret_types = func_type->types + param_count + 1;
  525. ext_ret_cell_num = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  526. if (ext_ret_cell_num > 64) {
  527. aot_set_last_error("prepare extra results's return "
  528. "address arguments failed: "
  529. "maximum 64 parameter cell number supported.");
  530. goto fail;
  531. }
  532. for (i = 0; i < ext_ret_count; i++) {
  533. if (!(ext_ret_idx = I32_CONST(cell_num))
  534. || !(ext_ret_ptr_type =
  535. LLVMPointerType(TO_LLVM_TYPE(ext_ret_types[i]), 0))) {
  536. aot_set_last_error("llvm add const or pointer type failed.");
  537. goto fail;
  538. }
  539. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i);
  540. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(
  541. comp_ctx->builder, I32_TYPE, func_ctx->argv_buf,
  542. &ext_ret_idx, 1, buf))) {
  543. aot_set_last_error("llvm build GEP failed.");
  544. goto fail;
  545. }
  546. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i);
  547. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  548. ext_ret_ptr_type, buf))) {
  549. aot_set_last_error("llvm build bit cast failed.");
  550. goto fail;
  551. }
  552. param_values[param_count + 1 + i] = ext_ret_ptr;
  553. cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
  554. }
  555. }
  556. if (func_idx < import_func_count) {
  557. if (!(import_func_idx = I32_CONST(func_idx))) {
  558. aot_set_last_error("llvm build inbounds gep failed.");
  559. goto fail;
  560. }
  561. /* Initialize parameter types of the LLVM function */
  562. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  563. if (total_size >= UINT32_MAX
  564. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  565. aot_set_last_error("allocate memory failed.");
  566. goto fail;
  567. }
  568. j = 0;
  569. param_types[j++] = comp_ctx->exec_env_type;
  570. for (i = 0; i < param_count; i++, j++) {
  571. param_types[j] = TO_LLVM_TYPE(func_type->types[i]);
  572. /* If the signature can be gotten, e.g. the signature of the builtin
  573. native libraries, just check the app offset and buf size, and
  574. then convert app offset to native addr and call the native func
  575. directly, no need to call aot_invoke_native to call it */
  576. if (signature) {
  577. LLVMValueRef native_addr, native_addr_size;
  578. if (signature[i + 1] == '*' || signature[i + 1] == '$') {
  579. param_types[j] = INT8_PTR_TYPE;
  580. }
  581. if (signature[i + 1] == '*') {
  582. if (signature[i + 2] == '~')
  583. native_addr_size = param_values[i + 2];
  584. else
  585. native_addr_size = I32_ONE;
  586. if (!check_app_addr_and_convert(
  587. comp_ctx, func_ctx, false, param_values[j],
  588. native_addr_size, &native_addr)) {
  589. goto fail;
  590. }
  591. param_values[j] = native_addr;
  592. }
  593. else if (signature[i + 1] == '$') {
  594. native_addr_size = I32_ZERO;
  595. if (!check_app_addr_and_convert(
  596. comp_ctx, func_ctx, true, param_values[j],
  597. native_addr_size, &native_addr)) {
  598. goto fail;
  599. }
  600. param_values[j] = native_addr;
  601. }
  602. }
  603. }
  604. if (func_type->result_count) {
  605. wasm_ret_type = func_type->types[func_type->param_count];
  606. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  607. }
  608. else {
  609. wasm_ret_type = VALUE_TYPE_VOID;
  610. ret_type = VOID_TYPE;
  611. }
  612. if (!signature) {
  613. /* call aot_invoke_native() */
  614. if (!call_aot_invoke_native_func(
  615. comp_ctx, func_ctx, import_func_idx, func_type,
  616. param_types + 1, param_values + 1, param_count,
  617. param_cell_num, ret_type, wasm_ret_type, &value_ret, &res))
  618. goto fail;
  619. /* Check whether there was exception thrown when executing
  620. the function */
  621. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  622. && !check_call_return(comp_ctx, func_ctx, res))
  623. goto fail;
  624. }
  625. else { /* call native func directly */
  626. LLVMTypeRef native_func_type, func_ptr_type;
  627. LLVMValueRef func_ptr;
  628. if (!(native_func_type = LLVMFunctionType(
  629. ret_type, param_types, param_count + 1, false))) {
  630. aot_set_last_error("llvm add function type failed.");
  631. goto fail;
  632. }
  633. if (!(func_ptr_type = LLVMPointerType(native_func_type, 0))) {
  634. aot_set_last_error("create LLVM function type failed.");
  635. goto fail;
  636. }
  637. /* Load function pointer */
  638. if (!(func_ptr = LLVMBuildInBoundsGEP2(
  639. comp_ctx->builder, OPQ_PTR_TYPE, func_ctx->func_ptrs,
  640. &import_func_idx, 1, "native_func_ptr_tmp"))) {
  641. aot_set_last_error("llvm build inbounds gep failed.");
  642. goto fail;
  643. }
  644. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE,
  645. func_ptr, "native_func_ptr"))) {
  646. aot_set_last_error("llvm build load failed.");
  647. goto fail;
  648. }
  649. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  650. func_ptr_type, "native_func"))) {
  651. aot_set_last_error("llvm bit cast failed.");
  652. goto fail;
  653. }
  654. /* Call the function */
  655. if (!(value_ret = LLVMBuildCall2(
  656. comp_ctx->builder, native_func_type, func, param_values,
  657. (uint32)param_count + 1 + ext_ret_count,
  658. (func_type->result_count > 0 ? "call" : "")))) {
  659. aot_set_last_error("LLVM build call failed.");
  660. goto fail;
  661. }
  662. /* Check whether there was exception thrown when executing
  663. the function */
  664. if (!check_exception_thrown(comp_ctx, func_ctx)) {
  665. goto fail;
  666. }
  667. }
  668. }
  669. else {
  670. #if LLVM_VERSION_MAJOR >= 14
  671. LLVMTypeRef llvm_func_type;
  672. #endif
  673. if (comp_ctx->is_indirect_mode) {
  674. LLVMTypeRef func_ptr_type;
  675. if (!(func_ptr_type = LLVMPointerType(
  676. func_ctxes[func_idx - import_func_count]->func_type,
  677. 0))) {
  678. aot_set_last_error("construct func ptr type failed.");
  679. goto fail;
  680. }
  681. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->func_ptrs,
  682. func_ptr_type, func_idx))) {
  683. goto fail;
  684. }
  685. }
  686. else {
  687. if (func_ctxes[func_idx - import_func_count] == func_ctx) {
  688. /* recursive call */
  689. func = func_ctx->precheck_func;
  690. }
  691. else {
  692. if (!comp_ctx->is_jit_mode) {
  693. func =
  694. func_ctxes[func_idx - import_func_count]->precheck_func;
  695. }
  696. else {
  697. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  698. func =
  699. func_ctxes[func_idx - import_func_count]->precheck_func;
  700. #else
  701. /* JIT tier-up, load func ptr from func_ptrs[func_idx] */
  702. LLVMValueRef func_ptr, func_idx_const;
  703. LLVMTypeRef func_ptr_type;
  704. if (!(func_idx_const = I32_CONST(func_idx))) {
  705. aot_set_last_error("llvm build const failed.");
  706. goto fail;
  707. }
  708. if (!(func_ptr = LLVMBuildInBoundsGEP2(
  709. comp_ctx->builder, OPQ_PTR_TYPE,
  710. func_ctx->func_ptrs, &func_idx_const, 1,
  711. "func_ptr_tmp"))) {
  712. aot_set_last_error("llvm build inbounds gep failed.");
  713. goto fail;
  714. }
  715. if (!(func_ptr =
  716. LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE,
  717. func_ptr, "func_ptr"))) {
  718. aot_set_last_error("llvm build load failed.");
  719. goto fail;
  720. }
  721. if (!(func_ptr_type = LLVMPointerType(
  722. func_ctxes[func_idx - import_func_count]
  723. ->func_type,
  724. 0))) {
  725. aot_set_last_error("construct func ptr type failed.");
  726. goto fail;
  727. }
  728. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  729. func_ptr_type,
  730. "indirect_func"))) {
  731. aot_set_last_error("llvm build bit cast failed.");
  732. goto fail;
  733. }
  734. #endif /* end of !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0) */
  735. }
  736. }
  737. }
  738. #if LLVM_VERSION_MAJOR >= 14
  739. llvm_func_type = func_ctxes[func_idx - import_func_count]->func_type;
  740. #endif
  741. /* Call the function */
  742. if (!(value_ret = LLVMBuildCall2(
  743. comp_ctx->builder, llvm_func_type, func, param_values,
  744. (uint32)param_count + 1 + ext_ret_count,
  745. (func_type->result_count > 0 ? "call" : "")))) {
  746. aot_set_last_error("LLVM build call failed.");
  747. goto fail;
  748. }
  749. /* Set calling convention for the call with the func's calling
  750. convention */
  751. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  752. if (tail_call)
  753. LLVMSetTailCall(value_ret, true);
  754. /* Check whether there was exception thrown when executing
  755. the function */
  756. if (!tail_call
  757. && (comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  758. && !check_exception_thrown(comp_ctx, func_ctx))
  759. goto fail;
  760. }
  761. if (func_type->result_count > 0) {
  762. /* Push the first result to stack */
  763. PUSH(value_ret, func_type->types[func_type->param_count]);
  764. /* Load extra result from its address and push to stack */
  765. for (i = 0; i < ext_ret_count; i++) {
  766. snprintf(buf, sizeof(buf), "func%d_ext_ret%d", func_idx, i);
  767. if (!(ext_ret = LLVMBuildLoad2(
  768. comp_ctx->builder, TO_LLVM_TYPE(ext_ret_types[i]),
  769. param_values[1 + param_count + i], buf))) {
  770. aot_set_last_error("llvm build load failed.");
  771. goto fail;
  772. }
  773. PUSH(ext_ret, ext_ret_types[i]);
  774. }
  775. }
  776. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  777. if (comp_ctx->enable_aux_stack_frame) {
  778. if (!call_aot_free_frame_func(comp_ctx, func_ctx))
  779. goto fail;
  780. }
  781. #endif
  782. #if WASM_ENABLE_THREAD_MGR != 0
  783. /* Insert suspend check point */
  784. if (comp_ctx->enable_thread_mgr) {
  785. if (!check_suspend_flags(comp_ctx, func_ctx))
  786. goto fail;
  787. }
  788. #endif
  789. ret = true;
  790. fail:
  791. if (param_types)
  792. wasm_runtime_free(param_types);
  793. if (param_values)
  794. wasm_runtime_free(param_values);
  795. return ret;
  796. }
  797. static bool
  798. call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  799. AOTFuncType *aot_func_type,
  800. LLVMValueRef func_type_idx, LLVMValueRef table_idx,
  801. LLVMValueRef table_elem_idx,
  802. LLVMTypeRef *param_types,
  803. LLVMValueRef *param_values, uint32 param_count,
  804. uint32 param_cell_num, uint32 result_count,
  805. uint8 *wasm_ret_types, LLVMValueRef *value_rets,
  806. LLVMValueRef *p_res)
  807. {
  808. LLVMTypeRef func_type, func_ptr_type, func_param_types[6];
  809. LLVMTypeRef ret_type, ret_ptr_type, elem_ptr_type;
  810. LLVMValueRef func, ret_idx, ret_ptr, elem_idx, elem_ptr;
  811. LLVMValueRef func_param_values[6], res = NULL;
  812. char buf[32], *func_name = "aot_call_indirect";
  813. uint32 i, cell_num = 0, ret_cell_num, argv_cell_num;
  814. /* prepare function type of aot_call_indirect */
  815. func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
  816. func_param_types[1] = I32_TYPE; /* table_idx */
  817. func_param_types[2] = I32_TYPE; /* table_elem_idx */
  818. func_param_types[3] = I32_TYPE; /* argc */
  819. func_param_types[4] = INT32_PTR_TYPE; /* argv */
  820. if (!(func_type =
  821. LLVMFunctionType(INT8_TYPE, func_param_types, 5, false))) {
  822. aot_set_last_error("llvm add function type failed.");
  823. return false;
  824. }
  825. /* prepare function pointer */
  826. if (comp_ctx->is_jit_mode) {
  827. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  828. aot_set_last_error("create LLVM function type failed.");
  829. return false;
  830. }
  831. /* JIT mode, call the function directly */
  832. if (!(func = I64_CONST((uint64)(uintptr_t)llvm_jit_call_indirect))
  833. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  834. aot_set_last_error("create LLVM value failed.");
  835. return false;
  836. }
  837. }
  838. else if (comp_ctx->is_indirect_mode) {
  839. int32 func_index;
  840. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  841. aot_set_last_error("create LLVM function type failed.");
  842. return false;
  843. }
  844. func_index = aot_get_native_symbol_index(comp_ctx, func_name);
  845. if (func_index < 0) {
  846. return false;
  847. }
  848. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  849. func_ptr_type, func_index))) {
  850. return false;
  851. }
  852. }
  853. else {
  854. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  855. && !(func =
  856. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  857. aot_set_last_error("add LLVM function failed.");
  858. return false;
  859. }
  860. }
  861. ret_cell_num = wasm_get_cell_num(wasm_ret_types, result_count);
  862. argv_cell_num =
  863. param_cell_num > ret_cell_num ? param_cell_num : ret_cell_num;
  864. if (argv_cell_num > 64) {
  865. aot_set_last_error("prepare native arguments failed: "
  866. "maximum 64 parameter cell number supported.");
  867. return false;
  868. }
  869. /* prepare frame_lp */
  870. for (i = 0; i < param_count; i++) {
  871. if (!(elem_idx = I32_CONST(cell_num))
  872. || !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
  873. aot_set_last_error("llvm add const or pointer type failed.");
  874. return false;
  875. }
  876. snprintf(buf, sizeof(buf), "%s%d", "elem", i);
  877. if (!(elem_ptr =
  878. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  879. func_ctx->argv_buf, &elem_idx, 1, buf))
  880. || !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
  881. elem_ptr_type, buf))) {
  882. aot_set_last_error("llvm build bit cast failed.");
  883. return false;
  884. }
  885. if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
  886. elem_ptr))) {
  887. aot_set_last_error("llvm build store failed.");
  888. return false;
  889. }
  890. LLVMSetAlignment(res, 1);
  891. cell_num += wasm_value_type_cell_num(aot_func_type->types[i]);
  892. }
  893. func_param_values[0] = func_ctx->exec_env;
  894. func_param_values[1] = table_idx;
  895. func_param_values[2] = table_elem_idx;
  896. func_param_values[3] = I32_CONST(param_cell_num);
  897. func_param_values[4] = func_ctx->argv_buf;
  898. if (!func_param_values[3]) {
  899. aot_set_last_error("llvm create const failed.");
  900. return false;
  901. }
  902. /* call aot_call_indirect() function */
  903. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  904. func_param_values, 5, "res"))) {
  905. aot_set_last_error("llvm build call failed.");
  906. return false;
  907. }
  908. /* get function result values */
  909. cell_num = 0;
  910. for (i = 0; i < result_count; i++) {
  911. ret_type = TO_LLVM_TYPE(wasm_ret_types[i]);
  912. if (!(ret_idx = I32_CONST(cell_num))
  913. || !(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
  914. aot_set_last_error("llvm add const or pointer type failed.");
  915. return false;
  916. }
  917. snprintf(buf, sizeof(buf), "argv_ret%d", i);
  918. if (!(ret_ptr =
  919. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  920. func_ctx->argv_buf, &ret_idx, 1, buf))
  921. || !(ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ret_ptr,
  922. ret_ptr_type, buf))) {
  923. aot_set_last_error("llvm build GEP or bit cast failed.");
  924. return false;
  925. }
  926. snprintf(buf, sizeof(buf), "ret%d", i);
  927. if (!(value_rets[i] =
  928. LLVMBuildLoad2(comp_ctx->builder, ret_type, ret_ptr, buf))) {
  929. aot_set_last_error("llvm build load failed.");
  930. return false;
  931. }
  932. cell_num += wasm_value_type_cell_num(wasm_ret_types[i]);
  933. }
  934. *p_res = res;
  935. return true;
  936. }
  937. bool
  938. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  939. uint32 type_idx, uint32 tbl_idx)
  940. {
  941. AOTFuncType *func_type;
  942. LLVMValueRef tbl_idx_value, elem_idx, table_elem, func_idx;
  943. LLVMValueRef ftype_idx_ptr, ftype_idx, ftype_idx_const;
  944. LLVMValueRef cmp_elem_idx, cmp_func_idx, cmp_ftype_idx;
  945. LLVMValueRef func, func_ptr, table_size_const;
  946. LLVMValueRef ext_ret_offset, ext_ret_ptr, ext_ret, res;
  947. LLVMValueRef *param_values = NULL, *value_rets = NULL;
  948. LLVMValueRef *result_phis = NULL, value_ret, import_func_count;
  949. LLVMTypeRef *param_types = NULL, ret_type;
  950. LLVMTypeRef llvm_func_type, llvm_func_ptr_type;
  951. LLVMTypeRef ext_ret_ptr_type;
  952. LLVMBasicBlockRef check_elem_idx_succ, check_ftype_idx_succ;
  953. LLVMBasicBlockRef check_func_idx_succ, block_return, block_curr;
  954. LLVMBasicBlockRef block_call_import, block_call_non_import;
  955. LLVMValueRef offset;
  956. uint32 total_param_count, func_param_count, func_result_count;
  957. uint32 ext_cell_num, param_cell_num, i, j;
  958. uint8 wasm_ret_type, *wasm_ret_types;
  959. uint64 total_size;
  960. char buf[32];
  961. bool ret = false;
  962. /* Check function type index */
  963. if (type_idx >= comp_ctx->comp_data->func_type_count) {
  964. aot_set_last_error("function type index out of range");
  965. return false;
  966. }
  967. /* Find the equivalent function type whose type index is the smallest:
  968. the callee function's type index is also converted to the smallest
  969. one in wasm loader, so we can just check whether the two type indexes
  970. are equal (the type index of call_indirect opcode and callee func),
  971. we don't need to check whether the whole function types are equal,
  972. including param types and result types. */
  973. type_idx = wasm_get_smallest_type_idx(comp_ctx->comp_data->func_types,
  974. comp_ctx->comp_data->func_type_count,
  975. type_idx);
  976. ftype_idx_const = I32_CONST(type_idx);
  977. CHECK_LLVM_CONST(ftype_idx_const);
  978. func_type = comp_ctx->comp_data->func_types[type_idx];
  979. aot_estimate_and_record_stack_usage_for_function_call(comp_ctx, func_ctx,
  980. func_type);
  981. func_param_count = func_type->param_count;
  982. func_result_count = func_type->result_count;
  983. POP_I32(elem_idx);
  984. /* get the cur size of the table instance */
  985. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  986. + offsetof(AOTTableInstance, cur_size)))) {
  987. HANDLE_FAILURE("LLVMConstInt");
  988. goto fail;
  989. }
  990. if (!(table_size_const = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  991. func_ctx->aot_inst, &offset,
  992. 1, "cur_size_i8p"))) {
  993. HANDLE_FAILURE("LLVMBuildGEP");
  994. goto fail;
  995. }
  996. if (!(table_size_const =
  997. LLVMBuildBitCast(comp_ctx->builder, table_size_const,
  998. INT32_PTR_TYPE, "cur_siuze_i32p"))) {
  999. HANDLE_FAILURE("LLVMBuildBitCast");
  1000. goto fail;
  1001. }
  1002. if (!(table_size_const = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE,
  1003. table_size_const, "cur_size"))) {
  1004. HANDLE_FAILURE("LLVMBuildLoad");
  1005. goto fail;
  1006. }
  1007. /* Check if (uint32)elem index >= table size */
  1008. if (!(cmp_elem_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGE, elem_idx,
  1009. table_size_const, "cmp_elem_idx"))) {
  1010. aot_set_last_error("llvm build icmp failed.");
  1011. goto fail;
  1012. }
  1013. /* Throw exception if elem index >= table size */
  1014. if (!(check_elem_idx_succ = LLVMAppendBasicBlockInContext(
  1015. comp_ctx->context, func_ctx->func, "check_elem_idx_succ"))) {
  1016. aot_set_last_error("llvm add basic block failed.");
  1017. goto fail;
  1018. }
  1019. LLVMMoveBasicBlockAfter(check_elem_idx_succ,
  1020. LLVMGetInsertBlock(comp_ctx->builder));
  1021. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNDEFINED_ELEMENT, true,
  1022. cmp_elem_idx, check_elem_idx_succ)))
  1023. goto fail;
  1024. /* load data as i32* */
  1025. if (!(offset = I32_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)
  1026. + offsetof(AOTTableInstance, elems)))) {
  1027. HANDLE_FAILURE("LLVMConstInt");
  1028. goto fail;
  1029. }
  1030. if (!(table_elem = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1031. func_ctx->aot_inst, &offset, 1,
  1032. "table_elem_i8p"))) {
  1033. aot_set_last_error("llvm build add failed.");
  1034. goto fail;
  1035. }
  1036. if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem,
  1037. INT32_PTR_TYPE, "table_elem_i32p"))) {
  1038. HANDLE_FAILURE("LLVMBuildBitCast");
  1039. goto fail;
  1040. }
  1041. /* Load function index */
  1042. if (!(table_elem =
  1043. LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE, table_elem,
  1044. &elem_idx, 1, "table_elem"))) {
  1045. HANDLE_FAILURE("LLVMBuildNUWAdd");
  1046. goto fail;
  1047. }
  1048. if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, table_elem,
  1049. "func_idx"))) {
  1050. aot_set_last_error("llvm build load failed.");
  1051. goto fail;
  1052. }
  1053. /* Check if func_idx == -1 */
  1054. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, func_idx,
  1055. I32_NEG_ONE, "cmp_func_idx"))) {
  1056. aot_set_last_error("llvm build icmp failed.");
  1057. goto fail;
  1058. }
  1059. /* Throw exception if func_idx == -1 */
  1060. if (!(check_func_idx_succ = LLVMAppendBasicBlockInContext(
  1061. comp_ctx->context, func_ctx->func, "check_func_idx_succ"))) {
  1062. aot_set_last_error("llvm add basic block failed.");
  1063. goto fail;
  1064. }
  1065. LLVMMoveBasicBlockAfter(check_func_idx_succ,
  1066. LLVMGetInsertBlock(comp_ctx->builder));
  1067. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNINITIALIZED_ELEMENT,
  1068. true, cmp_func_idx, check_func_idx_succ)))
  1069. goto fail;
  1070. /* Load function type index */
  1071. if (!(ftype_idx_ptr = LLVMBuildInBoundsGEP2(
  1072. comp_ctx->builder, I32_TYPE, func_ctx->func_type_indexes,
  1073. &func_idx, 1, "ftype_idx_ptr"))) {
  1074. aot_set_last_error("llvm build inbounds gep failed.");
  1075. goto fail;
  1076. }
  1077. if (!(ftype_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, ftype_idx_ptr,
  1078. "ftype_idx"))) {
  1079. aot_set_last_error("llvm build load failed.");
  1080. goto fail;
  1081. }
  1082. /* Check if function type index not equal */
  1083. if (!(cmp_ftype_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, ftype_idx,
  1084. ftype_idx_const, "cmp_ftype_idx"))) {
  1085. aot_set_last_error("llvm build icmp failed.");
  1086. goto fail;
  1087. }
  1088. /* Throw exception if ftype_idx != ftype_idx_const */
  1089. if (!(check_ftype_idx_succ = LLVMAppendBasicBlockInContext(
  1090. comp_ctx->context, func_ctx->func, "check_ftype_idx_succ"))) {
  1091. aot_set_last_error("llvm add basic block failed.");
  1092. goto fail;
  1093. }
  1094. LLVMMoveBasicBlockAfter(check_ftype_idx_succ,
  1095. LLVMGetInsertBlock(comp_ctx->builder));
  1096. if (!(aot_emit_exception(comp_ctx, func_ctx,
  1097. EXCE_INVALID_FUNCTION_TYPE_INDEX, true,
  1098. cmp_ftype_idx, check_ftype_idx_succ)))
  1099. goto fail;
  1100. /* Initialize parameter types of the LLVM function */
  1101. total_param_count = 1 + func_param_count;
  1102. /* Extra function results' addresses (except the first one) are
  1103. appended to aot function parameters. */
  1104. if (func_result_count > 1)
  1105. total_param_count += func_result_count - 1;
  1106. total_size = sizeof(LLVMTypeRef) * (uint64)total_param_count;
  1107. if (total_size >= UINT32_MAX
  1108. || !(param_types = wasm_runtime_malloc((uint32)total_size))) {
  1109. aot_set_last_error("allocate memory failed.");
  1110. goto fail;
  1111. }
  1112. /* Prepare param types */
  1113. j = 0;
  1114. param_types[j++] = comp_ctx->exec_env_type;
  1115. for (i = 0; i < func_param_count; i++)
  1116. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  1117. for (i = 1; i < func_result_count; i++, j++) {
  1118. param_types[j] = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1119. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  1120. aot_set_last_error("llvm get pointer type failed.");
  1121. goto fail;
  1122. }
  1123. }
  1124. /* Resolve return type of the LLVM function */
  1125. if (func_result_count) {
  1126. wasm_ret_type = func_type->types[func_param_count];
  1127. ret_type = TO_LLVM_TYPE(wasm_ret_type);
  1128. }
  1129. else {
  1130. wasm_ret_type = VALUE_TYPE_VOID;
  1131. ret_type = VOID_TYPE;
  1132. }
  1133. /* Allocate memory for parameters */
  1134. total_size = sizeof(LLVMValueRef) * (uint64)total_param_count;
  1135. if (total_size >= UINT32_MAX
  1136. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  1137. aot_set_last_error("allocate memory failed.");
  1138. goto fail;
  1139. }
  1140. /* First parameter is exec env */
  1141. j = 0;
  1142. param_values[j++] = func_ctx->exec_env;
  1143. /* Pop parameters from stack */
  1144. for (i = func_param_count - 1; (int32)i >= 0; i--)
  1145. POP(param_values[i + j], func_type->types[i]);
  1146. /* Prepare extra parameters */
  1147. ext_cell_num = 0;
  1148. for (i = 1; i < func_result_count; i++) {
  1149. ext_ret_offset = I32_CONST(ext_cell_num);
  1150. CHECK_LLVM_CONST(ext_ret_offset);
  1151. snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i - 1);
  1152. if (!(ext_ret_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
  1153. func_ctx->argv_buf,
  1154. &ext_ret_offset, 1, buf))) {
  1155. aot_set_last_error("llvm build GEP failed.");
  1156. goto fail;
  1157. }
  1158. ext_ret_ptr_type = param_types[func_param_count + i];
  1159. snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i - 1);
  1160. if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ext_ret_ptr,
  1161. ext_ret_ptr_type, buf))) {
  1162. aot_set_last_error("llvm build bit cast failed.");
  1163. goto fail;
  1164. }
  1165. param_values[func_param_count + i] = ext_ret_ptr;
  1166. ext_cell_num +=
  1167. wasm_value_type_cell_num(func_type->types[func_param_count + i]);
  1168. }
  1169. if (ext_cell_num > 64) {
  1170. aot_set_last_error("prepare call-indirect arguments failed: "
  1171. "maximum 64 extra cell number supported.");
  1172. goto fail;
  1173. }
  1174. #if WASM_ENABLE_THREAD_MGR != 0
  1175. /* Insert suspend check point */
  1176. if (comp_ctx->enable_thread_mgr) {
  1177. if (!check_suspend_flags(comp_ctx, func_ctx))
  1178. goto fail;
  1179. }
  1180. #endif
  1181. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1182. if (comp_ctx->enable_aux_stack_frame) {
  1183. if (!call_aot_alloc_frame_func(comp_ctx, func_ctx, func_idx))
  1184. goto fail;
  1185. }
  1186. #endif
  1187. /* Add basic blocks */
  1188. block_call_import = LLVMAppendBasicBlockInContext(
  1189. comp_ctx->context, func_ctx->func, "call_import");
  1190. block_call_non_import = LLVMAppendBasicBlockInContext(
  1191. comp_ctx->context, func_ctx->func, "call_non_import");
  1192. block_return = LLVMAppendBasicBlockInContext(comp_ctx->context,
  1193. func_ctx->func, "func_return");
  1194. if (!block_call_import || !block_call_non_import || !block_return) {
  1195. aot_set_last_error("llvm add basic block failed.");
  1196. goto fail;
  1197. }
  1198. LLVMMoveBasicBlockAfter(block_call_import,
  1199. LLVMGetInsertBlock(comp_ctx->builder));
  1200. LLVMMoveBasicBlockAfter(block_call_non_import, block_call_import);
  1201. LLVMMoveBasicBlockAfter(block_return, block_call_non_import);
  1202. import_func_count = I32_CONST(comp_ctx->comp_data->import_func_count);
  1203. CHECK_LLVM_CONST(import_func_count);
  1204. /* Check if func_idx < import_func_count */
  1205. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntULT, func_idx,
  1206. import_func_count, "cmp_func_idx"))) {
  1207. aot_set_last_error("llvm build icmp failed.");
  1208. goto fail;
  1209. }
  1210. /* If func_idx < import_func_count, jump to call import block,
  1211. else jump to call non-import block */
  1212. if (!LLVMBuildCondBr(comp_ctx->builder, cmp_func_idx, block_call_import,
  1213. block_call_non_import)) {
  1214. aot_set_last_error("llvm build cond br failed.");
  1215. goto fail;
  1216. }
  1217. /* Add result phis for return block */
  1218. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  1219. if (func_result_count > 0) {
  1220. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1221. if (total_size >= UINT32_MAX
  1222. || !(result_phis = wasm_runtime_malloc((uint32)total_size))) {
  1223. aot_set_last_error("allocate memory failed.");
  1224. goto fail;
  1225. }
  1226. memset(result_phis, 0, (uint32)total_size);
  1227. for (i = 0; i < func_result_count; i++) {
  1228. LLVMTypeRef tmp_type =
  1229. TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1230. if (!(result_phis[i] =
  1231. LLVMBuildPhi(comp_ctx->builder, tmp_type, "phi"))) {
  1232. aot_set_last_error("llvm build phi failed.");
  1233. goto fail;
  1234. }
  1235. }
  1236. }
  1237. /* Translate call import block */
  1238. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_import);
  1239. /* Allocate memory for result values */
  1240. if (func_result_count > 0) {
  1241. total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
  1242. if (total_size >= UINT32_MAX
  1243. || !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
  1244. aot_set_last_error("allocate memory failed.");
  1245. goto fail;
  1246. }
  1247. memset(value_rets, 0, (uint32)total_size);
  1248. }
  1249. param_cell_num = func_type->param_cell_num;
  1250. wasm_ret_types = func_type->types + func_type->param_count;
  1251. tbl_idx_value = I32_CONST(tbl_idx);
  1252. if (!tbl_idx_value) {
  1253. aot_set_last_error("llvm create const failed.");
  1254. goto fail;
  1255. }
  1256. if (!call_aot_call_indirect_func(
  1257. comp_ctx, func_ctx, func_type, ftype_idx, tbl_idx_value, elem_idx,
  1258. param_types + 1, param_values + 1, func_param_count, param_cell_num,
  1259. func_result_count, wasm_ret_types, value_rets, &res))
  1260. goto fail;
  1261. /* Check whether exception was thrown when executing the function */
  1262. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1263. && !check_call_return(comp_ctx, func_ctx, res))
  1264. goto fail;
  1265. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1266. for (i = 0; i < func_result_count; i++) {
  1267. LLVMAddIncoming(result_phis[i], &value_rets[i], &block_curr, 1);
  1268. }
  1269. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1270. aot_set_last_error("llvm build br failed.");
  1271. goto fail;
  1272. }
  1273. /* Translate call non-import block */
  1274. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_non_import);
  1275. /* Load function pointer */
  1276. if (!(func_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, OPQ_PTR_TYPE,
  1277. func_ctx->func_ptrs, &func_idx, 1,
  1278. "func_ptr_tmp"))) {
  1279. aot_set_last_error("llvm build inbounds gep failed.");
  1280. goto fail;
  1281. }
  1282. if (!(func_ptr = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, func_ptr,
  1283. "func_ptr"))) {
  1284. aot_set_last_error("llvm build load failed.");
  1285. goto fail;
  1286. }
  1287. if (!(llvm_func_type =
  1288. LLVMFunctionType(ret_type, param_types, total_param_count, false))
  1289. || !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
  1290. aot_set_last_error("llvm add function type failed.");
  1291. goto fail;
  1292. }
  1293. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
  1294. llvm_func_ptr_type, "indirect_func"))) {
  1295. aot_set_last_error("llvm build bit cast failed.");
  1296. goto fail;
  1297. }
  1298. if (!(value_ret = LLVMBuildCall2(comp_ctx->builder, llvm_func_type, func,
  1299. param_values, total_param_count,
  1300. func_result_count > 0 ? "ret" : ""))) {
  1301. aot_set_last_error("llvm build call failed.");
  1302. goto fail;
  1303. }
  1304. /* Check whether exception was thrown when executing the function */
  1305. if ((comp_ctx->enable_bound_check || is_win_platform(comp_ctx))
  1306. && !check_exception_thrown(comp_ctx, func_ctx))
  1307. goto fail;
  1308. if (func_result_count > 0) {
  1309. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1310. /* Push the first result to stack */
  1311. LLVMAddIncoming(result_phis[0], &value_ret, &block_curr, 1);
  1312. /* Load extra result from its address and push to stack */
  1313. for (i = 1; i < func_result_count; i++) {
  1314. ret_type = TO_LLVM_TYPE(func_type->types[func_param_count + i]);
  1315. snprintf(buf, sizeof(buf), "ext_ret%d", i - 1);
  1316. if (!(ext_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
  1317. param_values[func_param_count + i],
  1318. buf))) {
  1319. aot_set_last_error("llvm build load failed.");
  1320. goto fail;
  1321. }
  1322. LLVMAddIncoming(result_phis[i], &ext_ret, &block_curr, 1);
  1323. }
  1324. }
  1325. if (!LLVMBuildBr(comp_ctx->builder, block_return)) {
  1326. aot_set_last_error("llvm build br failed.");
  1327. goto fail;
  1328. }
  1329. /* Translate function return block */
  1330. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_return);
  1331. for (i = 0; i < func_result_count; i++) {
  1332. PUSH(result_phis[i], func_type->types[func_param_count + i]);
  1333. }
  1334. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
  1335. if (comp_ctx->enable_aux_stack_frame) {
  1336. if (!call_aot_free_frame_func(comp_ctx, func_ctx))
  1337. goto fail;
  1338. }
  1339. #endif
  1340. #if WASM_ENABLE_THREAD_MGR != 0
  1341. /* Insert suspend check point */
  1342. if (comp_ctx->enable_thread_mgr) {
  1343. if (!check_suspend_flags(comp_ctx, func_ctx))
  1344. goto fail;
  1345. }
  1346. #endif
  1347. ret = true;
  1348. fail:
  1349. if (param_values)
  1350. wasm_runtime_free(param_values);
  1351. if (param_types)
  1352. wasm_runtime_free(param_types);
  1353. if (value_rets)
  1354. wasm_runtime_free(value_rets);
  1355. if (result_phis)
  1356. wasm_runtime_free(result_phis);
  1357. return ret;
  1358. }
  1359. bool
  1360. aot_compile_op_ref_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1361. {
  1362. PUSH_I32(REF_NULL);
  1363. return true;
  1364. fail:
  1365. return false;
  1366. }
  1367. bool
  1368. aot_compile_op_ref_is_null(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1369. {
  1370. LLVMValueRef lhs, res;
  1371. POP_I32(lhs);
  1372. if (!(res = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, lhs, REF_NULL,
  1373. "cmp_w_null"))) {
  1374. HANDLE_FAILURE("LLVMBuildICmp");
  1375. goto fail;
  1376. }
  1377. if (!(res = LLVMBuildZExt(comp_ctx->builder, res, I32_TYPE, "r_i"))) {
  1378. HANDLE_FAILURE("LLVMBuildZExt");
  1379. goto fail;
  1380. }
  1381. PUSH_I32(res);
  1382. return true;
  1383. fail:
  1384. return false;
  1385. }
  1386. bool
  1387. aot_compile_op_ref_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1388. uint32 func_idx)
  1389. {
  1390. LLVMValueRef ref_idx;
  1391. if (!(ref_idx = I32_CONST(func_idx))) {
  1392. HANDLE_FAILURE("LLVMConstInt");
  1393. goto fail;
  1394. }
  1395. PUSH_I32(ref_idx);
  1396. return true;
  1397. fail:
  1398. return false;
  1399. }