aot_emit_function.c 57 KB

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