aot_emit_function.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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/aot_runtime.h"
  9. /* Check whether there was exception thrown, if yes, return directly */
  10. static bool
  11. check_exception_thrown(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  12. {
  13. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  14. LLVMBasicBlockRef block_curr, check_exce_succ;
  15. LLVMValueRef value, cmp;
  16. /* Load the first byte of aot_module_inst->cur_exception, and check
  17. whether it is '\0'. If yes, no exception was thrown. */
  18. if (!(value = LLVMBuildLoad(comp_ctx->builder, func_ctx->cur_exception,
  19. "exce_value"))
  20. || !(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  21. value, I8_ZERO, "cmp"))) {
  22. aot_set_last_error("llvm build icmp failed.");
  23. return false;
  24. }
  25. /* Add check exection success block */
  26. if (!(check_exce_succ = LLVMAppendBasicBlockInContext(comp_ctx->context,
  27. func_ctx->func,
  28. "check_exce_succ"))) {
  29. aot_set_last_error("llvm add basic block failed.");
  30. return false;
  31. }
  32. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  33. LLVMMoveBasicBlockAfter(check_exce_succ, block_curr);
  34. /* Create function return block if it isn't created */
  35. if (!func_ctx->func_return_block) {
  36. if (!(func_ctx->func_return_block =
  37. LLVMAppendBasicBlockInContext(comp_ctx->context,
  38. func_ctx->func,
  39. "func_ret"))) {
  40. aot_set_last_error("llvm add basic block failed.");
  41. return false;
  42. }
  43. /* Create return IR */
  44. LLVMPositionBuilderAtEnd(comp_ctx->builder, func_ctx->func_return_block);
  45. if (aot_func_type->result_count) {
  46. switch (aot_func_type->types[aot_func_type->param_count]) {
  47. case VALUE_TYPE_I32:
  48. LLVMBuildRet(comp_ctx->builder, I32_ZERO);
  49. break;
  50. case VALUE_TYPE_I64:
  51. LLVMBuildRet(comp_ctx->builder, I64_ZERO);
  52. break;
  53. case VALUE_TYPE_F32:
  54. LLVMBuildRet(comp_ctx->builder, F32_ZERO);
  55. break;
  56. case VALUE_TYPE_F64:
  57. LLVMBuildRet(comp_ctx->builder, F64_ZERO);
  58. break;
  59. }
  60. }
  61. else {
  62. LLVMBuildRetVoid(comp_ctx->builder);
  63. }
  64. }
  65. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
  66. /* Create condition br */
  67. if (!LLVMBuildCondBr(comp_ctx->builder, cmp,
  68. check_exce_succ, func_ctx->func_return_block)) {
  69. aot_set_last_error("llvm build cond br failed.");
  70. return false;
  71. }
  72. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_exce_succ);
  73. return true;
  74. }
  75. bool
  76. aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  77. uint32 func_idx, uint8 **p_frame_ip)
  78. {
  79. uint32 import_func_count = comp_ctx->comp_data->import_func_count;
  80. AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
  81. uint32 func_count = comp_ctx->func_ctx_count;
  82. AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
  83. AOTFuncType *func_type;
  84. LLVMTypeRef *param_types = NULL, ret_type, f_type, f_ptr_type;
  85. LLVMValueRef *param_values = NULL, value_ret, func, value, cmp;
  86. LLVMBasicBlockRef check_func_ptr_succ;
  87. int32 i, j = 0, param_count;
  88. void *func_ptr;
  89. uint64 total_size;
  90. bool ret = false;
  91. /* Check function index */
  92. if (func_idx >= import_func_count + func_count) {
  93. aot_set_last_error("Function index out of range.");
  94. return false;
  95. }
  96. /* Get function type */
  97. if (func_idx < import_func_count)
  98. func_type = import_funcs[func_idx].func_type;
  99. else
  100. func_type = func_ctxes[func_idx - import_func_count]->
  101. aot_func->func_type;
  102. /* Allocate memory for parameters */
  103. param_count = (int32)func_type->param_count;
  104. total_size = sizeof(LLVMValueRef) * (uint64)(param_count + 1);
  105. if (total_size >= UINT32_MAX
  106. || !(param_values = wasm_malloc((uint32)total_size))) {
  107. aot_set_last_error("Allocate memory failed.");
  108. return false;
  109. }
  110. /* First parameter is exec env */
  111. param_values[j++] = func_ctx->exec_env;
  112. /* Pop parameters from stack */
  113. for (i = param_count - 1; i >= 0; i--)
  114. POP(param_values[i + j], func_type->types[i]);
  115. if (func_idx < import_func_count) {
  116. /* Get function pointer linked */
  117. func_ptr = import_funcs[func_idx].func_ptr_linked;
  118. /* Initialize parameter types of the LLVM function */
  119. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  120. if (total_size >= UINT32_MAX
  121. || !(param_types = wasm_malloc((uint32)total_size))) {
  122. aot_set_last_error("Allocate memory failed.");
  123. goto fail;
  124. }
  125. j = 0;
  126. param_types[j++] = comp_ctx->exec_env_type;
  127. for (i = 0; i < param_count; i++)
  128. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  129. /* Resolve return type of the LLVM function */
  130. if (func_type->result_count)
  131. ret_type = TO_LLVM_TYPE(func_type->types[func_type->param_count]);
  132. else
  133. ret_type = VOID_TYPE;
  134. /* Resolve function prototype */
  135. if (!(f_type = LLVMFunctionType(ret_type, param_types,
  136. (uint32)param_count + 1, false))
  137. || !(f_ptr_type = LLVMPointerType(f_type, 0))) {
  138. aot_set_last_error("create LLVM function type failed.");
  139. goto fail;
  140. }
  141. if (comp_ctx->is_jit_mode) {
  142. if (!func_ptr) {
  143. /* The import function isn't linked, throw exception
  144. when calling it. */
  145. if (!aot_emit_exception(comp_ctx, func_ctx,
  146. EXCE_CALL_UNLINKED_IMPORT_FUNC,
  147. false, NULL, NULL))
  148. goto fail;
  149. ret = aot_handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  150. goto fail;
  151. }
  152. /* JIT mode, call the linked function directly */
  153. if (!(value = I64_CONST((uint64)(uintptr_t)func_ptr))
  154. || !(func = LLVMConstIntToPtr(value, f_ptr_type))) {
  155. aot_set_last_error("create LLVM value failed.");
  156. goto fail;
  157. }
  158. }
  159. else {
  160. /* Load function pointer */
  161. if (!(value = I32_CONST(func_idx))
  162. || !(func_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  163. func_ctx->func_ptrs,
  164. &value, 1, "func_ptr"))) {
  165. aot_set_last_error("llvm build inbounds gep failed.");
  166. goto fail;
  167. }
  168. if (!(func = LLVMBuildLoad(comp_ctx->builder, func_ptr, "func_tmp"))) {
  169. aot_set_last_error("llvm build load failed.");
  170. goto fail;
  171. }
  172. /* Check whether import function is NULL */
  173. if (!(cmp = LLVMBuildIsNull(comp_ctx->builder, func, "is_func_null"))) {
  174. aot_set_last_error("llvm build icmp failed.");
  175. goto fail;
  176. }
  177. /* Throw exception if import function is NULL */
  178. if (!(check_func_ptr_succ =
  179. LLVMAppendBasicBlockInContext(comp_ctx->context,
  180. func_ctx->func,
  181. "check_func_ptr_succ"))) {
  182. aot_set_last_error("llvm add basic block failed.");
  183. goto fail;
  184. }
  185. LLVMMoveBasicBlockAfter(check_func_ptr_succ,
  186. LLVMGetInsertBlock(comp_ctx->builder));
  187. if (!(aot_emit_exception(comp_ctx, func_ctx,
  188. EXCE_CALL_UNLINKED_IMPORT_FUNC,
  189. true, cmp, check_func_ptr_succ)))
  190. goto fail;
  191. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func,
  192. f_ptr_type, "func"))) {
  193. aot_set_last_error("create LLVM value failed.");
  194. goto fail;
  195. }
  196. }
  197. }
  198. else {
  199. func = func_ctxes[func_idx - import_func_count]->func;
  200. }
  201. /* Call the function */
  202. if (!(value_ret = LLVMBuildCall(comp_ctx->builder, func,
  203. param_values, (uint32)param_count + 1,
  204. (func_type->result_count > 0
  205. ? "call" : "")))) {
  206. aot_set_last_error("LLVM build call failed.");
  207. goto fail;
  208. }
  209. /* Set calling convention for the call with the func's calling convention */
  210. LLVMSetInstructionCallConv(value_ret, LLVMGetFunctionCallConv(func));
  211. if (func_type->result_count > 0)
  212. PUSH(value_ret, func_type->types[func_type->param_count]);
  213. /* Check whether there was exception thrown when executing the function */
  214. if (!check_exception_thrown(comp_ctx, func_ctx))
  215. goto fail;
  216. ret = true;
  217. fail:
  218. if (param_types)
  219. wasm_free(param_types);
  220. if (param_values)
  221. wasm_free(param_values);
  222. return ret;
  223. }
  224. bool
  225. aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  226. uint32 type_idx)
  227. {
  228. AOTFuncType *func_type;
  229. LLVMValueRef elem_idx, table_elem, func_idx, ftype_idx_ptr, ftype_idx;
  230. LLVMValueRef cmp_elem_idx, cmp_func_idx, is_ftype_match, is_ftype_mismatch;
  231. LLVMValueRef func, func_ptr, func_const, table_size_const, cmp_func_ptr;
  232. LLVMValueRef *param_values = NULL, param_values_tmp[3], value_ret;
  233. LLVMTypeRef *param_types = NULL, param_types_tmp[3], ret_type,
  234. f_type, f_ptr_type;
  235. LLVMBasicBlockRef check_elem_idx_succ, check_ftype_idx_succ;
  236. LLVMBasicBlockRef check_func_idx_succ, check_func_ptr_succ;
  237. int32 i, j = 0, param_count;
  238. uint64 total_size;
  239. bool ret;
  240. char *func_name = "aot_is_wasm_type_equal";
  241. /* Check function type index */
  242. if (type_idx >= comp_ctx->comp_data->func_type_count) {
  243. aot_set_last_error("type index is overflow");
  244. return false;
  245. }
  246. func_type = comp_ctx->comp_data->func_types[type_idx];
  247. POP_I32(elem_idx);
  248. table_size_const = I32_CONST(comp_ctx->comp_data->table_size);
  249. CHECK_LLVM_CONST(table_size_const);
  250. /* Check if (uint32)elem index >= table size */
  251. if (!(cmp_elem_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGE,
  252. elem_idx, table_size_const,
  253. "cmp_elem_idx"))) {
  254. aot_set_last_error("llvm build icmp failed.");
  255. goto fail;
  256. }
  257. /* Throw exception if elem index >= table size */
  258. if (!(check_elem_idx_succ =
  259. LLVMAppendBasicBlockInContext(comp_ctx->context,
  260. func_ctx->func,
  261. "check_elem_idx_succ"))) {
  262. aot_set_last_error("llvm add basic block failed.");
  263. goto fail;
  264. }
  265. LLVMMoveBasicBlockAfter(check_elem_idx_succ,
  266. LLVMGetInsertBlock(comp_ctx->builder));
  267. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNDEFINED_ELEMENT,
  268. true, cmp_elem_idx, check_elem_idx_succ)))
  269. goto fail;
  270. /* Load function index */
  271. if (!(table_elem = LLVMBuildInBoundsGEP(comp_ctx->builder,
  272. func_ctx->table_base,
  273. &elem_idx, 1, "table_elem"))) {
  274. aot_set_last_error("llvm build add failed.");
  275. goto fail;
  276. }
  277. if (!(func_idx = LLVMBuildLoad(comp_ctx->builder, table_elem, "func_idx"))) {
  278. aot_set_last_error("llvm build load failed.");
  279. goto fail;
  280. }
  281. /* Check if func_idx == -1 */
  282. if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  283. func_idx, I32_NEG_ONE,
  284. "cmp_func_idx"))) {
  285. aot_set_last_error("llvm build icmp failed.");
  286. goto fail;
  287. }
  288. /* Throw exception if func_idx == -1 */
  289. if (!(check_func_idx_succ =
  290. LLVMAppendBasicBlockInContext(comp_ctx->context,
  291. func_ctx->func,
  292. "check_func_idx_succ"))) {
  293. aot_set_last_error("llvm add basic block failed.");
  294. goto fail;
  295. }
  296. LLVMMoveBasicBlockAfter(check_func_idx_succ,
  297. LLVMGetInsertBlock(comp_ctx->builder));
  298. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_UNINITIALIZED_ELEMENT,
  299. true, cmp_func_idx, check_func_idx_succ)))
  300. goto fail;
  301. /* Load function type index */
  302. if (!(ftype_idx_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  303. func_ctx->func_type_indexes,
  304. &func_idx, 1,
  305. "ftype_idx_ptr"))) {
  306. aot_set_last_error("llvm build inbounds gep failed.");
  307. goto fail;
  308. }
  309. if (!(ftype_idx = LLVMBuildLoad(comp_ctx->builder, ftype_idx_ptr,
  310. "ftype_idx"))) {
  311. aot_set_last_error("llvm build load failed.");
  312. goto fail;
  313. }
  314. /* Call aot_is_type_equal() to check whether function type match */
  315. param_types_tmp[0] = INT8_PTR_TYPE;
  316. param_types_tmp[1] = I32_TYPE;
  317. param_types_tmp[2] = I32_TYPE;
  318. ret_type = INT8_TYPE;
  319. /* Create function type */
  320. if (!(f_type = LLVMFunctionType(ret_type, param_types_tmp,
  321. 3, false))) {
  322. aot_set_last_error("create LLVM function type failed.");
  323. goto fail;
  324. }
  325. if (comp_ctx->is_jit_mode) {
  326. /* Create function type */
  327. if (!(f_ptr_type = LLVMPointerType(f_type, 0))) {
  328. aot_set_last_error("create LLVM function type failed.");
  329. goto fail;
  330. }
  331. /* Create LLVM function with const function pointer */
  332. if (!(func_const =
  333. I64_CONST((uint64)(uintptr_t)aot_is_wasm_type_equal))
  334. || !(func = LLVMConstIntToPtr(func_const, f_ptr_type))) {
  335. aot_set_last_error("create LLVM value failed.");
  336. goto fail;
  337. }
  338. }
  339. else {
  340. /* Create LLVM function with external function pointer */
  341. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
  342. && !(func = LLVMAddFunction(comp_ctx->module, func_name, f_type))) {
  343. aot_set_last_error("add LLVM function failed.");
  344. goto fail;
  345. }
  346. }
  347. /* Call the aot_is_type_equal() function */
  348. param_values_tmp[0] = func_ctx->aot_inst;
  349. param_values_tmp[1] = I32_CONST(type_idx);
  350. param_values_tmp[2] = ftype_idx;
  351. CHECK_LLVM_CONST(param_values_tmp[1]);
  352. if (!(is_ftype_match = LLVMBuildCall(comp_ctx->builder, func,
  353. param_values_tmp, 3,
  354. "is_ftype_match"))) {
  355. aot_set_last_error("llvm build icmp failed.");
  356. goto fail;
  357. }
  358. if (!(is_ftype_mismatch = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
  359. is_ftype_match, I8_ZERO,
  360. "is_ftype_mismatch"))) {
  361. aot_set_last_error("llvm build icmp failed.");
  362. goto fail;
  363. }
  364. if (!(check_ftype_idx_succ =
  365. LLVMAppendBasicBlockInContext(comp_ctx->context,
  366. func_ctx->func,
  367. "check_ftype_idx_success"))) {
  368. aot_set_last_error("llvm add basic block failed.");
  369. goto fail;
  370. }
  371. LLVMMoveBasicBlockAfter(check_ftype_idx_succ,
  372. LLVMGetInsertBlock(comp_ctx->builder));
  373. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INVALID_FUNCTION_TYPE_INDEX,
  374. true, is_ftype_mismatch, check_ftype_idx_succ)))
  375. goto fail;
  376. /* Load function pointer */
  377. if (!(func_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->func_ptrs,
  378. &func_idx, 1, "func_ptr"))) {
  379. aot_set_last_error("llvm build inbounds gep failed.");
  380. goto fail;
  381. }
  382. if (!(func = LLVMBuildLoad(comp_ctx->builder, func_ptr, "func_tmp"))) {
  383. aot_set_last_error("llvm build load failed.");
  384. goto fail;
  385. }
  386. /* Check whether import function is NULL */
  387. if (!(cmp_func_ptr = LLVMBuildIsNull(comp_ctx->builder, func, "is_func_null"))) {
  388. aot_set_last_error("llvm build is null failed.");
  389. goto fail;
  390. }
  391. /* Throw exception if import function is NULL */
  392. if (!(check_func_ptr_succ =
  393. LLVMAppendBasicBlockInContext(comp_ctx->context,
  394. func_ctx->func,
  395. "check_func_ptr_succ"))) {
  396. aot_set_last_error("llvm add basic block failed.");
  397. goto fail;
  398. }
  399. LLVMMoveBasicBlockAfter(check_func_ptr_succ,
  400. LLVMGetInsertBlock(comp_ctx->builder));
  401. if (!(aot_emit_exception(comp_ctx, func_ctx,
  402. EXCE_CALL_UNLINKED_IMPORT_FUNC,
  403. true, cmp_func_ptr, check_func_ptr_succ)))
  404. goto fail;
  405. /* Initialize parameter types of the LLVM function */
  406. param_count = (int32)func_type->param_count;
  407. total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
  408. if (total_size >= UINT32_MAX
  409. || !(param_types = wasm_malloc((uint32)total_size))) {
  410. aot_set_last_error("Allocate memory failed.");
  411. goto fail;
  412. }
  413. j = 0;
  414. param_types[j++] = comp_ctx->exec_env_type;
  415. for (i = 0; i < param_count; i++)
  416. param_types[j++] = TO_LLVM_TYPE(func_type->types[i]);
  417. /* Resolve return type of the LLVM function */
  418. if (func_type->result_count)
  419. ret_type = TO_LLVM_TYPE(func_type->types[func_type->param_count]);
  420. else
  421. ret_type = VOID_TYPE;
  422. /* Resolve function prototype */
  423. if (!(f_type = LLVMFunctionType(ret_type, param_types,
  424. (uint32)param_count + 1, false))
  425. || !(f_ptr_type = LLVMPointerType(f_type, 0))) {
  426. aot_set_last_error("create LLVM function type failed.");
  427. goto fail;
  428. }
  429. if (!(func = LLVMBuildBitCast(comp_ctx->builder, func,
  430. f_ptr_type, "func"))) {
  431. aot_set_last_error("create LLVM value failed.");
  432. goto fail;
  433. }
  434. /* Allocate memory for parameters */
  435. total_size = sizeof(LLVMValueRef) * (uint64)(param_count + 1);
  436. if (total_size >= UINT32_MAX
  437. || !(param_values = wasm_malloc((uint32)total_size))) {
  438. aot_set_last_error("Allocate memory failed.");
  439. goto fail;
  440. }
  441. /* First parameter is exec env */
  442. j = 0;
  443. param_values[j++] = func_ctx->exec_env;
  444. /* Pop parameters from stack */
  445. for (i = param_count - 1; i >= 0; i--)
  446. POP(param_values[i + j], func_type->types[i]);
  447. /* Call the function */
  448. if (!(value_ret = LLVMBuildCall(comp_ctx->builder, func,
  449. param_values, (uint32)param_count + 1,
  450. (func_type->result_count > 0
  451. ? "call_indirect" : "")))) {
  452. aot_set_last_error("LLVM build call failed.");
  453. goto fail;
  454. }
  455. if (func_type->result_count > 0)
  456. PUSH(value_ret, func_type->types[func_type->param_count]);
  457. /* Check whether there was exception thrown when executing the function */
  458. if (!check_exception_thrown(comp_ctx, func_ctx))
  459. goto fail;
  460. ret = true;
  461. fail:
  462. if (param_values)
  463. wasm_free(param_values);
  464. if (param_types)
  465. wasm_free(param_types);
  466. return ret;
  467. }