aot_emit_conversion.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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_conversion.h"
  6. #include "aot_emit_exception.h"
  7. #include "aot_emit_numberic.h"
  8. #include "../aot/aot_intrinsic.h"
  9. #include "../aot/aot_runtime.h"
  10. static bool
  11. trunc_float_to_int(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  12. LLVMValueRef operand, LLVMTypeRef src_type,
  13. LLVMTypeRef dest_type, LLVMValueRef min_value,
  14. LLVMValueRef max_value, char *name, bool sign)
  15. {
  16. LLVMBasicBlockRef check_nan_succ, check_overflow_succ;
  17. LLVMValueRef is_less, is_greater, res;
  18. if (comp_ctx->disable_llvm_intrinsics
  19. && aot_intrinsic_check_capability(
  20. comp_ctx, src_type == F32_TYPE ? "f32_cmp" : "f64_cmp")) {
  21. LLVMTypeRef param_types[3];
  22. LLVMValueRef opcond = LLVMConstInt(I32_TYPE, FLOAT_UNO, true);
  23. param_types[0] = I32_TYPE;
  24. param_types[1] = src_type;
  25. param_types[2] = src_type;
  26. res = aot_call_llvm_intrinsic(
  27. comp_ctx, func_ctx, src_type == F32_TYPE ? "f32_cmp" : "f64_cmp",
  28. I32_TYPE, param_types, 3, opcond, operand, operand);
  29. if (!res) {
  30. goto fail;
  31. }
  32. res = LLVMBuildIntCast(comp_ctx->builder, res, INT1_TYPE, "bit_cast");
  33. }
  34. else {
  35. res = LLVMBuildFCmp(comp_ctx->builder, LLVMRealUNO, operand, operand,
  36. "fcmp_is_nan");
  37. }
  38. if (!res) {
  39. aot_set_last_error("llvm build fcmp failed.");
  40. goto fail;
  41. }
  42. if (!(check_nan_succ = LLVMAppendBasicBlockInContext(
  43. comp_ctx->context, func_ctx->func, "check_nan_succ"))) {
  44. aot_set_last_error("llvm add basic block failed.");
  45. goto fail;
  46. }
  47. LLVMMoveBasicBlockAfter(check_nan_succ,
  48. LLVMGetInsertBlock(comp_ctx->builder));
  49. if (!(aot_emit_exception(comp_ctx, func_ctx,
  50. EXCE_INVALID_CONVERSION_TO_INTEGER, true, res,
  51. check_nan_succ)))
  52. goto fail;
  53. if (comp_ctx->disable_llvm_intrinsics
  54. && aot_intrinsic_check_capability(
  55. comp_ctx, src_type == F32_TYPE ? "f32_cmp" : "f64_cmp")) {
  56. LLVMTypeRef param_types[3];
  57. LLVMValueRef opcond = LLVMConstInt(I32_TYPE, FLOAT_LE, true);
  58. param_types[0] = I32_TYPE;
  59. param_types[1] = src_type;
  60. param_types[2] = src_type;
  61. is_less = aot_call_llvm_intrinsic(
  62. comp_ctx, func_ctx, src_type == F32_TYPE ? "f32_cmp" : "f64_cmp",
  63. I32_TYPE, param_types, 3, opcond, operand, min_value);
  64. if (!is_less) {
  65. goto fail;
  66. }
  67. is_less =
  68. LLVMBuildIntCast(comp_ctx->builder, is_less, INT1_TYPE, "bit_cast");
  69. }
  70. else {
  71. is_less = LLVMBuildFCmp(comp_ctx->builder, LLVMRealOLE, operand,
  72. min_value, "fcmp_min_value");
  73. }
  74. if (!is_less) {
  75. aot_set_last_error("llvm build fcmp failed.");
  76. goto fail;
  77. }
  78. if (comp_ctx->disable_llvm_intrinsics
  79. && aot_intrinsic_check_capability(
  80. comp_ctx, src_type == F32_TYPE ? "f32_cmp" : "f64_cmp")) {
  81. LLVMTypeRef param_types[3];
  82. LLVMValueRef opcond = LLVMConstInt(I32_TYPE, FLOAT_GE, true);
  83. param_types[0] = I32_TYPE;
  84. param_types[1] = src_type;
  85. param_types[2] = src_type;
  86. is_greater = aot_call_llvm_intrinsic(
  87. comp_ctx, func_ctx, src_type == F32_TYPE ? "f32_cmp" : "f64_cmp",
  88. I32_TYPE, param_types, 3, opcond, operand, max_value);
  89. if (!is_greater) {
  90. goto fail;
  91. }
  92. is_greater = LLVMBuildIntCast(comp_ctx->builder, is_greater, INT1_TYPE,
  93. "bit_cast");
  94. }
  95. else {
  96. is_greater = LLVMBuildFCmp(comp_ctx->builder, LLVMRealOGE, operand,
  97. max_value, "fcmp_min_value");
  98. }
  99. if (!is_greater) {
  100. aot_set_last_error("llvm build fcmp failed.");
  101. goto fail;
  102. }
  103. if (!(res = LLVMBuildOr(comp_ctx->builder, is_less, is_greater,
  104. "is_overflow"))) {
  105. aot_set_last_error("llvm build logic and failed.");
  106. goto fail;
  107. }
  108. /* Check if float value out of range */
  109. if (!(check_overflow_succ = LLVMAppendBasicBlockInContext(
  110. comp_ctx->context, func_ctx->func, "check_overflow_succ"))) {
  111. aot_set_last_error("llvm add basic block failed.");
  112. goto fail;
  113. }
  114. LLVMMoveBasicBlockAfter(check_overflow_succ,
  115. LLVMGetInsertBlock(comp_ctx->builder));
  116. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INTEGER_OVERFLOW, true,
  117. res, check_overflow_succ)))
  118. goto fail;
  119. if (comp_ctx->disable_llvm_intrinsics
  120. && aot_intrinsic_check_capability(comp_ctx, name)) {
  121. LLVMTypeRef param_types[1];
  122. param_types[0] = src_type;
  123. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx, name, dest_type,
  124. param_types, 1, operand);
  125. }
  126. else {
  127. if (sign)
  128. res = LLVMBuildFPToSI(comp_ctx->builder, operand, dest_type, name);
  129. else
  130. res = LLVMBuildFPToUI(comp_ctx->builder, operand, dest_type, name);
  131. }
  132. if (!res) {
  133. aot_set_last_error("llvm build conversion failed.");
  134. return false;
  135. }
  136. if (dest_type == I32_TYPE)
  137. PUSH_I32(res);
  138. else if (dest_type == I64_TYPE)
  139. PUSH_I64(res);
  140. return true;
  141. fail:
  142. return false;
  143. }
  144. #define ADD_BASIC_BLOCK(block, name) \
  145. do { \
  146. if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
  147. func_ctx->func, name))) { \
  148. aot_set_last_error("llvm add basic block failed."); \
  149. goto fail; \
  150. } \
  151. \
  152. LLVMMoveBasicBlockAfter(block, LLVMGetInsertBlock(comp_ctx->builder)); \
  153. } while (0)
  154. static bool
  155. trunc_sat_float_to_int(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  156. LLVMValueRef operand, LLVMTypeRef src_type,
  157. LLVMTypeRef dest_type, LLVMValueRef min_value,
  158. LLVMValueRef max_value, char *name, bool sign)
  159. {
  160. LLVMBasicBlockRef check_nan_succ, check_less_succ, check_greater_succ;
  161. LLVMBasicBlockRef is_nan_block, is_less_block, is_greater_block, res_block;
  162. LLVMValueRef is_less, is_greater, res, phi;
  163. LLVMValueRef zero = (dest_type == I32_TYPE) ? I32_ZERO : I64_ZERO;
  164. LLVMValueRef vmin, vmax;
  165. if (!(res = LLVMBuildFCmp(comp_ctx->builder, LLVMRealUNO, operand, operand,
  166. "fcmp_is_nan"))) {
  167. aot_set_last_error("llvm build fcmp failed.");
  168. goto fail;
  169. }
  170. ADD_BASIC_BLOCK(check_nan_succ, "check_nan_succ");
  171. ADD_BASIC_BLOCK(is_nan_block, "is_nan_block");
  172. ADD_BASIC_BLOCK(check_less_succ, "check_less_succ");
  173. ADD_BASIC_BLOCK(is_less_block, "is_less_block");
  174. ADD_BASIC_BLOCK(check_greater_succ, "check_greater_succ");
  175. ADD_BASIC_BLOCK(is_greater_block, "is_greater_block");
  176. ADD_BASIC_BLOCK(res_block, "res_block");
  177. if (!LLVMBuildCondBr(comp_ctx->builder, res, is_nan_block,
  178. check_nan_succ)) {
  179. aot_set_last_error("llvm build cond br failed.");
  180. goto fail;
  181. }
  182. /* Start to translate is_nan block */
  183. LLVMPositionBuilderAtEnd(comp_ctx->builder, is_nan_block);
  184. if (!LLVMBuildBr(comp_ctx->builder, res_block)) {
  185. aot_set_last_error("llvm build br failed.");
  186. goto fail;
  187. }
  188. /* Start to translate check_nan_succ block */
  189. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_nan_succ);
  190. if (!(is_less = LLVMBuildFCmp(comp_ctx->builder, LLVMRealOLE, operand,
  191. min_value, "fcmp_min_value"))) {
  192. aot_set_last_error("llvm build fcmp failed.");
  193. goto fail;
  194. }
  195. if (!LLVMBuildCondBr(comp_ctx->builder, is_less, is_less_block,
  196. check_less_succ)) {
  197. aot_set_last_error("llvm build cond br failed.");
  198. goto fail;
  199. }
  200. /* Start to translate is_less block */
  201. LLVMPositionBuilderAtEnd(comp_ctx->builder, is_less_block);
  202. if (!LLVMBuildBr(comp_ctx->builder, res_block)) {
  203. aot_set_last_error("llvm build br failed.");
  204. goto fail;
  205. }
  206. /* Start to translate check_less_succ block */
  207. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_less_succ);
  208. if (!(is_greater = LLVMBuildFCmp(comp_ctx->builder, LLVMRealOGE, operand,
  209. max_value, "fcmp_max_value"))) {
  210. aot_set_last_error("llvm build fcmp failed.");
  211. goto fail;
  212. }
  213. if (!LLVMBuildCondBr(comp_ctx->builder, is_greater, is_greater_block,
  214. check_greater_succ)) {
  215. aot_set_last_error("llvm build cond br failed.");
  216. goto fail;
  217. }
  218. /* Start to translate is_greater block */
  219. LLVMPositionBuilderAtEnd(comp_ctx->builder, is_greater_block);
  220. if (!LLVMBuildBr(comp_ctx->builder, res_block)) {
  221. aot_set_last_error("llvm build br failed.");
  222. goto fail;
  223. }
  224. /* Start to translate check_greater_succ block */
  225. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_greater_succ);
  226. if (comp_ctx->disable_llvm_intrinsics
  227. && aot_intrinsic_check_capability(comp_ctx, name)) {
  228. LLVMTypeRef param_types[1];
  229. param_types[0] = src_type;
  230. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx, name, dest_type,
  231. param_types, 1, operand);
  232. }
  233. else {
  234. if (sign)
  235. res = LLVMBuildFPToSI(comp_ctx->builder, operand, dest_type, name);
  236. else
  237. res = LLVMBuildFPToUI(comp_ctx->builder, operand, dest_type, name);
  238. }
  239. if (!res) {
  240. aot_set_last_error("llvm build conversion failed.");
  241. return false;
  242. }
  243. if (!LLVMBuildBr(comp_ctx->builder, res_block)) {
  244. aot_set_last_error("llvm build br failed.");
  245. goto fail;
  246. }
  247. /* Start to translate res_block */
  248. LLVMPositionBuilderAtEnd(comp_ctx->builder, res_block);
  249. /* Create result phi */
  250. if (!(phi = LLVMBuildPhi(comp_ctx->builder, dest_type,
  251. "trunc_sat_result_phi"))) {
  252. aot_set_last_error("llvm build phi failed.");
  253. return false;
  254. }
  255. /* Add phi incoming values */
  256. if (dest_type == I32_TYPE) {
  257. if (sign) {
  258. vmin = I32_CONST(INT32_MIN);
  259. vmax = I32_CONST(INT32_MAX);
  260. }
  261. else {
  262. vmin = I32_CONST(0);
  263. vmax = I32_CONST(UINT32_MAX);
  264. }
  265. }
  266. else if (dest_type == I64_TYPE) {
  267. if (sign) {
  268. vmin = I64_CONST(INT64_MIN);
  269. vmax = I64_CONST(INT64_MAX);
  270. }
  271. else {
  272. vmin = I64_CONST(0);
  273. vmax = I64_CONST(UINT64_MAX);
  274. }
  275. }
  276. LLVMAddIncoming(phi, &zero, &is_nan_block, 1);
  277. LLVMAddIncoming(phi, &vmin, &is_less_block, 1);
  278. LLVMAddIncoming(phi, &vmax, &is_greater_block, 1);
  279. LLVMAddIncoming(phi, &res, &check_greater_succ, 1);
  280. if (dest_type == I32_TYPE)
  281. PUSH_I32(phi);
  282. else if (dest_type == I64_TYPE)
  283. PUSH_I64(phi);
  284. return true;
  285. fail:
  286. return false;
  287. }
  288. bool
  289. aot_compile_op_i32_wrap_i64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  290. {
  291. LLVMValueRef value, res;
  292. POP_I64(value);
  293. if (!(res = LLVMBuildTrunc(comp_ctx->builder, value, I32_TYPE,
  294. "i32_wrap_i64"))) {
  295. aot_set_last_error("llvm build conversion failed.");
  296. return false;
  297. }
  298. PUSH_I32(res);
  299. return true;
  300. fail:
  301. return false;
  302. }
  303. bool
  304. aot_compile_op_i32_trunc_f32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  305. bool sign, bool saturating)
  306. {
  307. LLVMValueRef value;
  308. LLVMValueRef min_value, max_value;
  309. POP_F32(value);
  310. if (sign) {
  311. min_value = F32_CONST(-2147483904.0f);
  312. max_value = F32_CONST(2147483648.0f);
  313. }
  314. else {
  315. min_value = F32_CONST(-1.0f);
  316. max_value = F32_CONST(4294967296.0f);
  317. }
  318. if (!saturating)
  319. return trunc_float_to_int(
  320. comp_ctx, func_ctx, value, F32_TYPE, I32_TYPE, min_value, max_value,
  321. sign ? "i32_trunc_f32_s" : "i32_trunc_f32_u", sign);
  322. else
  323. return trunc_sat_float_to_int(
  324. comp_ctx, func_ctx, value, F32_TYPE, I32_TYPE, min_value, max_value,
  325. sign ? "i32_trunc_sat_f32_s" : "i32_trunc_sat_f32_u", sign);
  326. fail:
  327. return false;
  328. }
  329. bool
  330. aot_compile_op_i32_trunc_f64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  331. bool sign, bool saturating)
  332. {
  333. LLVMValueRef value;
  334. LLVMValueRef min_value, max_value;
  335. POP_F64(value);
  336. if (sign) {
  337. min_value = F64_CONST(-2147483649.0);
  338. max_value = F64_CONST(2147483648.0);
  339. }
  340. else {
  341. min_value = F64_CONST(-1.0);
  342. max_value = F64_CONST(4294967296.0);
  343. }
  344. if (!saturating)
  345. return trunc_float_to_int(
  346. comp_ctx, func_ctx, value, F64_TYPE, I32_TYPE, min_value, max_value,
  347. sign ? "i32_trunc_f64_s" : "i32_trunc_f64_u", sign);
  348. else
  349. return trunc_sat_float_to_int(
  350. comp_ctx, func_ctx, value, F64_TYPE, I32_TYPE, min_value, max_value,
  351. sign ? "i32_trunc_sat_f64_s" : "i32_trunc_sat_f64_u", sign);
  352. fail:
  353. return false;
  354. }
  355. bool
  356. aot_compile_op_i64_extend_i32(AOTCompContext *comp_ctx,
  357. AOTFuncContext *func_ctx, bool sign)
  358. {
  359. LLVMValueRef value, res;
  360. POP_I32(value);
  361. if (sign)
  362. res = LLVMBuildSExt(comp_ctx->builder, value, I64_TYPE,
  363. "i64_extend_i32_s");
  364. else
  365. res = LLVMBuildZExt(comp_ctx->builder, value, I64_TYPE,
  366. "i64_extend_i32_u");
  367. if (!res) {
  368. aot_set_last_error("llvm build conversion failed.");
  369. return false;
  370. }
  371. PUSH_I64(res);
  372. return true;
  373. fail:
  374. return false;
  375. }
  376. bool
  377. aot_compile_op_i64_extend_i64(AOTCompContext *comp_ctx,
  378. AOTFuncContext *func_ctx, int8 bitwidth)
  379. {
  380. LLVMValueRef value, res, cast_value = NULL;
  381. POP_I64(value);
  382. if (bitwidth == 8) {
  383. cast_value = LLVMBuildIntCast2(comp_ctx->builder, value, INT8_TYPE,
  384. true, "i8_intcast_i64");
  385. }
  386. else if (bitwidth == 16) {
  387. cast_value = LLVMBuildIntCast2(comp_ctx->builder, value, INT16_TYPE,
  388. true, "i16_intcast_i64");
  389. }
  390. else if (bitwidth == 32) {
  391. cast_value = LLVMBuildIntCast2(comp_ctx->builder, value, I32_TYPE, true,
  392. "i32_intcast_i64");
  393. }
  394. if (!cast_value) {
  395. aot_set_last_error("llvm build conversion failed.");
  396. return false;
  397. }
  398. res = LLVMBuildSExt(comp_ctx->builder, cast_value, I64_TYPE,
  399. "i64_extend_i64_s");
  400. if (!res) {
  401. aot_set_last_error("llvm build conversion failed.");
  402. return false;
  403. }
  404. PUSH_I64(res);
  405. return true;
  406. fail:
  407. return false;
  408. }
  409. bool
  410. aot_compile_op_i32_extend_i32(AOTCompContext *comp_ctx,
  411. AOTFuncContext *func_ctx, int8 bitwidth)
  412. {
  413. LLVMValueRef value, res, cast_value = NULL;
  414. POP_I32(value);
  415. if (bitwidth == 8) {
  416. cast_value = LLVMBuildIntCast2(comp_ctx->builder, value, INT8_TYPE,
  417. true, "i8_intcast_i32");
  418. }
  419. else if (bitwidth == 16) {
  420. cast_value = LLVMBuildIntCast2(comp_ctx->builder, value, INT16_TYPE,
  421. true, "i16_intcast_i32");
  422. }
  423. if (!cast_value) {
  424. aot_set_last_error("llvm build conversion failed.");
  425. return false;
  426. }
  427. res = LLVMBuildSExt(comp_ctx->builder, cast_value, I32_TYPE,
  428. "i32_extend_i32_s");
  429. if (!res) {
  430. aot_set_last_error("llvm build conversion failed.");
  431. return false;
  432. }
  433. PUSH_I32(res);
  434. return true;
  435. fail:
  436. return false;
  437. }
  438. bool
  439. aot_compile_op_i64_trunc_f32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  440. bool sign, bool saturating)
  441. {
  442. LLVMValueRef value;
  443. LLVMValueRef min_value, max_value;
  444. POP_F32(value);
  445. if (sign) {
  446. min_value = F32_CONST(-9223373136366403584.0f);
  447. max_value = F32_CONST(9223372036854775808.0f);
  448. }
  449. else {
  450. min_value = F32_CONST(-1.0f);
  451. max_value = F32_CONST(18446744073709551616.0f);
  452. }
  453. if (!saturating)
  454. return trunc_float_to_int(
  455. comp_ctx, func_ctx, value, F32_TYPE, I64_TYPE, min_value, max_value,
  456. sign ? "i64_trunc_f32_s" : "i64_trunc_f32_u", sign);
  457. else
  458. return trunc_sat_float_to_int(
  459. comp_ctx, func_ctx, value, F32_TYPE, I64_TYPE, min_value, max_value,
  460. sign ? "i64_trunc_sat_f32_s" : "i64_trunc_sat_f32_u", sign);
  461. fail:
  462. return false;
  463. }
  464. bool
  465. aot_compile_op_i64_trunc_f64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  466. bool sign, bool saturating)
  467. {
  468. LLVMValueRef value;
  469. LLVMValueRef min_value, max_value;
  470. POP_F64(value);
  471. if (sign) {
  472. min_value = F64_CONST(-9223372036854777856.0);
  473. max_value = F64_CONST(9223372036854775808.0);
  474. }
  475. else {
  476. min_value = F64_CONST(-1.0);
  477. max_value = F64_CONST(18446744073709551616.0);
  478. }
  479. if (!saturating)
  480. return trunc_float_to_int(
  481. comp_ctx, func_ctx, value, F64_TYPE, I64_TYPE, min_value, max_value,
  482. sign ? "i64_trunc_f64_s" : "i64_trunc_f64_u", sign);
  483. else
  484. return trunc_sat_float_to_int(
  485. comp_ctx, func_ctx, value, F64_TYPE, I64_TYPE, min_value, max_value,
  486. sign ? "i64_trunc_sat_f64_s" : "i64_trunc_sat_f64_u", sign);
  487. fail:
  488. return false;
  489. }
  490. bool
  491. aot_compile_op_f32_convert_i32(AOTCompContext *comp_ctx,
  492. AOTFuncContext *func_ctx, bool sign)
  493. {
  494. LLVMValueRef value, res;
  495. POP_I32(value);
  496. if (comp_ctx->disable_llvm_intrinsics
  497. && aot_intrinsic_check_capability(
  498. comp_ctx, sign ? "f32_convert_i32_s" : "f32_convert_i32_u")) {
  499. LLVMTypeRef param_types[1];
  500. param_types[0] = I32_TYPE;
  501. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx,
  502. sign ? "f32_convert_i32_s"
  503. : "f32_convert_i32_u",
  504. F32_TYPE, param_types, 1, value);
  505. }
  506. else {
  507. if (sign)
  508. res = LLVMBuildSIToFP(comp_ctx->builder, value, F32_TYPE,
  509. "f32_convert_i32_s");
  510. else
  511. res = LLVMBuildUIToFP(comp_ctx->builder, value, F32_TYPE,
  512. "f32_convert_i32_u");
  513. }
  514. if (!res) {
  515. aot_set_last_error("llvm build conversion failed.");
  516. return false;
  517. }
  518. PUSH_F32(res);
  519. return true;
  520. fail:
  521. return false;
  522. }
  523. bool
  524. aot_compile_op_f32_convert_i64(AOTCompContext *comp_ctx,
  525. AOTFuncContext *func_ctx, bool sign)
  526. {
  527. LLVMValueRef value, res;
  528. POP_I64(value);
  529. if (comp_ctx->disable_llvm_intrinsics
  530. && aot_intrinsic_check_capability(
  531. comp_ctx, sign ? "f32_convert_i64_s" : "f32_convert_i64_u")) {
  532. LLVMTypeRef param_types[1];
  533. param_types[0] = I64_TYPE;
  534. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx,
  535. sign ? "f32_convert_i64_s"
  536. : "f32_convert_i64_u",
  537. F32_TYPE, param_types, 1, value);
  538. }
  539. else {
  540. if (sign)
  541. res = LLVMBuildSIToFP(comp_ctx->builder, value, F32_TYPE,
  542. "f32_convert_i64_s");
  543. else
  544. res = LLVMBuildUIToFP(comp_ctx->builder, value, F32_TYPE,
  545. "f32_convert_i64_u");
  546. }
  547. if (!res) {
  548. aot_set_last_error("llvm build conversion failed.");
  549. return false;
  550. }
  551. PUSH_F32(res);
  552. return true;
  553. fail:
  554. return false;
  555. }
  556. bool
  557. aot_compile_op_f32_demote_f64(AOTCompContext *comp_ctx,
  558. AOTFuncContext *func_ctx)
  559. {
  560. LLVMValueRef value, res;
  561. POP_F64(value);
  562. if (comp_ctx->disable_llvm_intrinsics
  563. && aot_intrinsic_check_capability(comp_ctx, "f32_demote_f64")) {
  564. LLVMTypeRef param_types[1];
  565. param_types[0] = F64_TYPE;
  566. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx, "f32_demote_f64",
  567. F32_TYPE, param_types, 1, value);
  568. }
  569. else {
  570. res = LLVMBuildFPTrunc(comp_ctx->builder, value, F32_TYPE,
  571. "f32_demote_f64");
  572. }
  573. if (!res) {
  574. aot_set_last_error("llvm build conversion failed.");
  575. return false;
  576. }
  577. PUSH_F32(res);
  578. return true;
  579. fail:
  580. return false;
  581. }
  582. bool
  583. aot_compile_op_f64_convert_i32(AOTCompContext *comp_ctx,
  584. AOTFuncContext *func_ctx, bool sign)
  585. {
  586. LLVMValueRef value, res;
  587. POP_I32(value);
  588. if (comp_ctx->disable_llvm_intrinsics
  589. && aot_intrinsic_check_capability(
  590. comp_ctx, sign ? "f64_convert_i32_s" : "f64_convert_i32_u")) {
  591. LLVMTypeRef param_types[1];
  592. param_types[0] = I32_TYPE;
  593. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx,
  594. sign ? "f64_convert_i32_s"
  595. : "f64_convert_i32_u",
  596. F64_TYPE, param_types, 1, value);
  597. }
  598. else {
  599. if (sign)
  600. res = LLVMBuildSIToFP(comp_ctx->builder, value, F64_TYPE,
  601. "f64_convert_i32_s");
  602. else
  603. res = LLVMBuildUIToFP(comp_ctx->builder, value, F64_TYPE,
  604. "f64_convert_i32_u");
  605. }
  606. if (!res) {
  607. aot_set_last_error("llvm build conversion failed.");
  608. return false;
  609. }
  610. PUSH_F64(res);
  611. return true;
  612. fail:
  613. return false;
  614. }
  615. bool
  616. aot_compile_op_f64_convert_i64(AOTCompContext *comp_ctx,
  617. AOTFuncContext *func_ctx, bool sign)
  618. {
  619. LLVMValueRef value, res;
  620. POP_I64(value);
  621. if (comp_ctx->disable_llvm_intrinsics
  622. && aot_intrinsic_check_capability(
  623. comp_ctx, sign ? "f64_convert_i64_s" : "f64_convert_i64_u")) {
  624. LLVMTypeRef param_types[1];
  625. param_types[0] = I64_TYPE;
  626. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx,
  627. sign ? "f64_convert_i64_s"
  628. : "f64_convert_i64_u",
  629. F64_TYPE, param_types, 1, value);
  630. }
  631. else {
  632. if (sign)
  633. res = LLVMBuildSIToFP(comp_ctx->builder, value, F64_TYPE,
  634. "f64_convert_i64_s");
  635. else
  636. res = LLVMBuildUIToFP(comp_ctx->builder, value, F64_TYPE,
  637. "f64_convert_i64_u");
  638. }
  639. if (!res) {
  640. aot_set_last_error("llvm build conversion failed.");
  641. return false;
  642. }
  643. PUSH_F64(res);
  644. return true;
  645. fail:
  646. return false;
  647. }
  648. bool
  649. aot_compile_op_f64_promote_f32(AOTCompContext *comp_ctx,
  650. AOTFuncContext *func_ctx)
  651. {
  652. LLVMValueRef value, res;
  653. POP_F32(value);
  654. if (comp_ctx->disable_llvm_intrinsics
  655. && aot_intrinsic_check_capability(comp_ctx, "f64_promote_f32")) {
  656. LLVMTypeRef param_types[1];
  657. param_types[0] = F32_TYPE;
  658. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx, "f64_promote_f32",
  659. F64_TYPE, param_types, 1, value);
  660. }
  661. else {
  662. res = LLVMBuildFPExt(comp_ctx->builder, value, F64_TYPE,
  663. "f64_promote_f32");
  664. }
  665. if (!res) {
  666. aot_set_last_error("llvm build conversion failed.");
  667. return false;
  668. }
  669. PUSH_F64(res);
  670. /* Avoid the promote being optimized away */
  671. PUSH_F64(F64_CONST(1.0));
  672. return aot_compile_op_f64_arithmetic(comp_ctx, func_ctx, FLOAT_MUL);
  673. fail:
  674. return false;
  675. }
  676. bool
  677. aot_compile_op_i64_reinterpret_f64(AOTCompContext *comp_ctx,
  678. AOTFuncContext *func_ctx)
  679. {
  680. LLVMValueRef value;
  681. POP_F64(value);
  682. if (!(value =
  683. LLVMBuildBitCast(comp_ctx->builder, value, I64_TYPE, "i64"))) {
  684. aot_set_last_error("llvm build fp to si failed.");
  685. return false;
  686. }
  687. PUSH_I64(value);
  688. return true;
  689. fail:
  690. return false;
  691. }
  692. bool
  693. aot_compile_op_i32_reinterpret_f32(AOTCompContext *comp_ctx,
  694. AOTFuncContext *func_ctx)
  695. {
  696. LLVMValueRef value;
  697. POP_F32(value);
  698. if (!(value =
  699. LLVMBuildBitCast(comp_ctx->builder, value, I32_TYPE, "i32"))) {
  700. aot_set_last_error("llvm build fp to si failed.");
  701. return false;
  702. }
  703. PUSH_I32(value);
  704. return true;
  705. fail:
  706. return false;
  707. }
  708. bool
  709. aot_compile_op_f64_reinterpret_i64(AOTCompContext *comp_ctx,
  710. AOTFuncContext *func_ctx)
  711. {
  712. LLVMValueRef value;
  713. POP_I64(value);
  714. if (!(value =
  715. LLVMBuildBitCast(comp_ctx->builder, value, F64_TYPE, "f64"))) {
  716. aot_set_last_error("llvm build si to fp failed.");
  717. return false;
  718. }
  719. PUSH_F64(value);
  720. return true;
  721. fail:
  722. return false;
  723. }
  724. bool
  725. aot_compile_op_f32_reinterpret_i32(AOTCompContext *comp_ctx,
  726. AOTFuncContext *func_ctx)
  727. {
  728. LLVMValueRef value;
  729. POP_I32(value);
  730. if (!(value =
  731. LLVMBuildBitCast(comp_ctx->builder, value, F32_TYPE, "f32"))) {
  732. aot_set_last_error("llvm build si to fp failed.");
  733. return false;
  734. }
  735. PUSH_F32(value);
  736. return true;
  737. fail:
  738. return false;
  739. }