aot_emit_conversion.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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 (!comp_ctx->is_indirect_mode) {
  311. if (sign) {
  312. min_value = F32_CONST(-2147483904.0f);
  313. max_value = F32_CONST(2147483648.0f);
  314. }
  315. else {
  316. min_value = F32_CONST(-1.0f);
  317. max_value = F32_CONST(4294967296.0f);
  318. }
  319. }
  320. else {
  321. WASMValue wasm_value;
  322. if (sign) {
  323. wasm_value.f32 = -2147483904.0f;
  324. min_value = aot_load_const_from_table(
  325. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
  326. wasm_value.f32 = 2147483648.0f;
  327. max_value = aot_load_const_from_table(
  328. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
  329. }
  330. else {
  331. wasm_value.f32 = -1.0f;
  332. min_value = aot_load_const_from_table(
  333. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
  334. wasm_value.f32 = 4294967296.0f;
  335. max_value = aot_load_const_from_table(
  336. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
  337. }
  338. }
  339. CHECK_LLVM_CONST(min_value);
  340. CHECK_LLVM_CONST(max_value);
  341. if (!saturating)
  342. return trunc_float_to_int(
  343. comp_ctx, func_ctx, value, F32_TYPE, I32_TYPE, min_value, max_value,
  344. sign ? "i32_trunc_f32_s" : "i32_trunc_f32_u", sign);
  345. else
  346. return trunc_sat_float_to_int(
  347. comp_ctx, func_ctx, value, F32_TYPE, I32_TYPE, min_value, max_value,
  348. sign ? "i32_trunc_sat_f32_s" : "i32_trunc_sat_f32_u", sign);
  349. fail:
  350. return false;
  351. }
  352. bool
  353. aot_compile_op_i32_trunc_f64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  354. bool sign, bool saturating)
  355. {
  356. LLVMValueRef value;
  357. LLVMValueRef min_value, max_value;
  358. POP_F64(value);
  359. if (!comp_ctx->is_indirect_mode) {
  360. if (sign) {
  361. min_value = F64_CONST(-2147483649.0);
  362. max_value = F64_CONST(2147483648.0);
  363. }
  364. else {
  365. min_value = F64_CONST(-1.0);
  366. max_value = F64_CONST(4294967296.0);
  367. }
  368. }
  369. else {
  370. WASMValue wasm_value;
  371. if (sign) {
  372. wasm_value.f64 = -2147483649.0;
  373. min_value = aot_load_const_from_table(
  374. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
  375. wasm_value.f64 = 2147483648.0;
  376. max_value = aot_load_const_from_table(
  377. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
  378. }
  379. else {
  380. wasm_value.f64 = -1.0;
  381. min_value = aot_load_const_from_table(
  382. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
  383. wasm_value.f64 = 4294967296.0;
  384. max_value = aot_load_const_from_table(
  385. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
  386. }
  387. }
  388. CHECK_LLVM_CONST(min_value);
  389. CHECK_LLVM_CONST(max_value);
  390. if (!saturating)
  391. return trunc_float_to_int(
  392. comp_ctx, func_ctx, value, F64_TYPE, I32_TYPE, min_value, max_value,
  393. sign ? "i32_trunc_f64_s" : "i32_trunc_f64_u", sign);
  394. else
  395. return trunc_sat_float_to_int(
  396. comp_ctx, func_ctx, value, F64_TYPE, I32_TYPE, min_value, max_value,
  397. sign ? "i32_trunc_sat_f64_s" : "i32_trunc_sat_f64_u", sign);
  398. fail:
  399. return false;
  400. }
  401. bool
  402. aot_compile_op_i64_extend_i32(AOTCompContext *comp_ctx,
  403. AOTFuncContext *func_ctx, bool sign)
  404. {
  405. LLVMValueRef value, res;
  406. POP_I32(value);
  407. if (sign)
  408. res = LLVMBuildSExt(comp_ctx->builder, value, I64_TYPE,
  409. "i64_extend_i32_s");
  410. else
  411. res = LLVMBuildZExt(comp_ctx->builder, value, I64_TYPE,
  412. "i64_extend_i32_u");
  413. if (!res) {
  414. aot_set_last_error("llvm build conversion failed.");
  415. return false;
  416. }
  417. PUSH_I64(res);
  418. return true;
  419. fail:
  420. return false;
  421. }
  422. bool
  423. aot_compile_op_i64_extend_i64(AOTCompContext *comp_ctx,
  424. AOTFuncContext *func_ctx, int8 bitwidth)
  425. {
  426. LLVMValueRef value, res, cast_value = NULL;
  427. POP_I64(value);
  428. if (bitwidth == 8) {
  429. cast_value = LLVMBuildIntCast2(comp_ctx->builder, value, INT8_TYPE,
  430. true, "i8_intcast_i64");
  431. }
  432. else if (bitwidth == 16) {
  433. cast_value = LLVMBuildIntCast2(comp_ctx->builder, value, INT16_TYPE,
  434. true, "i16_intcast_i64");
  435. }
  436. else if (bitwidth == 32) {
  437. cast_value = LLVMBuildIntCast2(comp_ctx->builder, value, I32_TYPE, true,
  438. "i32_intcast_i64");
  439. }
  440. if (!cast_value) {
  441. aot_set_last_error("llvm build conversion failed.");
  442. return false;
  443. }
  444. res = LLVMBuildSExt(comp_ctx->builder, cast_value, I64_TYPE,
  445. "i64_extend_i64_s");
  446. if (!res) {
  447. aot_set_last_error("llvm build conversion failed.");
  448. return false;
  449. }
  450. PUSH_I64(res);
  451. return true;
  452. fail:
  453. return false;
  454. }
  455. bool
  456. aot_compile_op_i32_extend_i32(AOTCompContext *comp_ctx,
  457. AOTFuncContext *func_ctx, int8 bitwidth)
  458. {
  459. LLVMValueRef value, res, cast_value = NULL;
  460. POP_I32(value);
  461. if (bitwidth == 8) {
  462. cast_value = LLVMBuildIntCast2(comp_ctx->builder, value, INT8_TYPE,
  463. true, "i8_intcast_i32");
  464. }
  465. else if (bitwidth == 16) {
  466. cast_value = LLVMBuildIntCast2(comp_ctx->builder, value, INT16_TYPE,
  467. true, "i16_intcast_i32");
  468. }
  469. if (!cast_value) {
  470. aot_set_last_error("llvm build conversion failed.");
  471. return false;
  472. }
  473. res = LLVMBuildSExt(comp_ctx->builder, cast_value, I32_TYPE,
  474. "i32_extend_i32_s");
  475. if (!res) {
  476. aot_set_last_error("llvm build conversion failed.");
  477. return false;
  478. }
  479. PUSH_I32(res);
  480. return true;
  481. fail:
  482. return false;
  483. }
  484. bool
  485. aot_compile_op_i64_trunc_f32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  486. bool sign, bool saturating)
  487. {
  488. LLVMValueRef value;
  489. LLVMValueRef min_value, max_value;
  490. POP_F32(value);
  491. if (!comp_ctx->is_indirect_mode) {
  492. if (sign) {
  493. min_value = F32_CONST(-9223373136366403584.0f);
  494. max_value = F32_CONST(9223372036854775808.0f);
  495. }
  496. else {
  497. min_value = F32_CONST(-1.0f);
  498. max_value = F32_CONST(18446744073709551616.0f);
  499. }
  500. }
  501. else {
  502. WASMValue wasm_value;
  503. if (sign) {
  504. wasm_value.f32 = -9223373136366403584.0f;
  505. min_value = aot_load_const_from_table(
  506. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
  507. wasm_value.f32 = 9223372036854775808.0f;
  508. max_value = aot_load_const_from_table(
  509. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
  510. }
  511. else {
  512. wasm_value.f32 = -1.0f;
  513. min_value = aot_load_const_from_table(
  514. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
  515. wasm_value.f32 = 18446744073709551616.0f;
  516. max_value = aot_load_const_from_table(
  517. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
  518. }
  519. }
  520. CHECK_LLVM_CONST(min_value);
  521. CHECK_LLVM_CONST(max_value);
  522. if (!saturating)
  523. return trunc_float_to_int(
  524. comp_ctx, func_ctx, value, F32_TYPE, I64_TYPE, min_value, max_value,
  525. sign ? "i64_trunc_f32_s" : "i64_trunc_f32_u", sign);
  526. else
  527. return trunc_sat_float_to_int(
  528. comp_ctx, func_ctx, value, F32_TYPE, I64_TYPE, min_value, max_value,
  529. sign ? "i64_trunc_sat_f32_s" : "i64_trunc_sat_f32_u", sign);
  530. fail:
  531. return false;
  532. }
  533. bool
  534. aot_compile_op_i64_trunc_f64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  535. bool sign, bool saturating)
  536. {
  537. LLVMValueRef value;
  538. LLVMValueRef min_value, max_value;
  539. POP_F64(value);
  540. if (!comp_ctx->is_indirect_mode) {
  541. if (sign) {
  542. min_value = F64_CONST(-9223372036854777856.0);
  543. max_value = F64_CONST(9223372036854775808.0);
  544. }
  545. else {
  546. min_value = F64_CONST(-1.0);
  547. max_value = F64_CONST(18446744073709551616.0);
  548. }
  549. }
  550. else {
  551. WASMValue wasm_value;
  552. if (sign) {
  553. wasm_value.f64 = -9223372036854777856.0;
  554. min_value = aot_load_const_from_table(
  555. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
  556. wasm_value.f64 = 9223372036854775808.0;
  557. max_value = aot_load_const_from_table(
  558. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
  559. }
  560. else {
  561. wasm_value.f64 = -1.0;
  562. min_value = aot_load_const_from_table(
  563. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
  564. wasm_value.f64 = 18446744073709551616.0;
  565. max_value = aot_load_const_from_table(
  566. comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
  567. }
  568. }
  569. CHECK_LLVM_CONST(min_value);
  570. CHECK_LLVM_CONST(max_value);
  571. if (!saturating)
  572. return trunc_float_to_int(
  573. comp_ctx, func_ctx, value, F64_TYPE, I64_TYPE, min_value, max_value,
  574. sign ? "i64_trunc_f64_s" : "i64_trunc_f64_u", sign);
  575. else
  576. return trunc_sat_float_to_int(
  577. comp_ctx, func_ctx, value, F64_TYPE, I64_TYPE, min_value, max_value,
  578. sign ? "i64_trunc_sat_f64_s" : "i64_trunc_sat_f64_u", sign);
  579. fail:
  580. return false;
  581. }
  582. bool
  583. aot_compile_op_f32_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 ? "f32_convert_i32_s" : "f32_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 ? "f32_convert_i32_s"
  595. : "f32_convert_i32_u",
  596. F32_TYPE, param_types, 1, value);
  597. }
  598. else {
  599. if (sign)
  600. res = LLVMBuildSIToFP(comp_ctx->builder, value, F32_TYPE,
  601. "f32_convert_i32_s");
  602. else
  603. res = LLVMBuildUIToFP(comp_ctx->builder, value, F32_TYPE,
  604. "f32_convert_i32_u");
  605. }
  606. if (!res) {
  607. aot_set_last_error("llvm build conversion failed.");
  608. return false;
  609. }
  610. PUSH_F32(res);
  611. return true;
  612. fail:
  613. return false;
  614. }
  615. bool
  616. aot_compile_op_f32_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 ? "f32_convert_i64_s" : "f32_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 ? "f32_convert_i64_s"
  628. : "f32_convert_i64_u",
  629. F32_TYPE, param_types, 1, value);
  630. }
  631. else {
  632. if (sign)
  633. res = LLVMBuildSIToFP(comp_ctx->builder, value, F32_TYPE,
  634. "f32_convert_i64_s");
  635. else
  636. res = LLVMBuildUIToFP(comp_ctx->builder, value, F32_TYPE,
  637. "f32_convert_i64_u");
  638. }
  639. if (!res) {
  640. aot_set_last_error("llvm build conversion failed.");
  641. return false;
  642. }
  643. PUSH_F32(res);
  644. return true;
  645. fail:
  646. return false;
  647. }
  648. bool
  649. aot_compile_op_f32_demote_f64(AOTCompContext *comp_ctx,
  650. AOTFuncContext *func_ctx)
  651. {
  652. LLVMValueRef value, res;
  653. POP_F64(value);
  654. if (comp_ctx->disable_llvm_intrinsics
  655. && aot_intrinsic_check_capability(comp_ctx, "f32_demote_f64")) {
  656. LLVMTypeRef param_types[1];
  657. param_types[0] = F64_TYPE;
  658. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx, "f32_demote_f64",
  659. F32_TYPE, param_types, 1, value);
  660. }
  661. else {
  662. res = LLVMBuildFPTrunc(comp_ctx->builder, value, F32_TYPE,
  663. "f32_demote_f64");
  664. }
  665. if (!res) {
  666. aot_set_last_error("llvm build conversion failed.");
  667. return false;
  668. }
  669. PUSH_F32(res);
  670. return true;
  671. fail:
  672. return false;
  673. }
  674. bool
  675. aot_compile_op_f64_convert_i32(AOTCompContext *comp_ctx,
  676. AOTFuncContext *func_ctx, bool sign)
  677. {
  678. LLVMValueRef value, res;
  679. POP_I32(value);
  680. if (comp_ctx->disable_llvm_intrinsics
  681. && aot_intrinsic_check_capability(
  682. comp_ctx, sign ? "f64_convert_i32_s" : "f64_convert_i32_u")) {
  683. LLVMTypeRef param_types[1];
  684. param_types[0] = I32_TYPE;
  685. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx,
  686. sign ? "f64_convert_i32_s"
  687. : "f64_convert_i32_u",
  688. F64_TYPE, param_types, 1, value);
  689. }
  690. else {
  691. if (sign)
  692. res = LLVMBuildSIToFP(comp_ctx->builder, value, F64_TYPE,
  693. "f64_convert_i32_s");
  694. else
  695. res = LLVMBuildUIToFP(comp_ctx->builder, value, F64_TYPE,
  696. "f64_convert_i32_u");
  697. }
  698. if (!res) {
  699. aot_set_last_error("llvm build conversion failed.");
  700. return false;
  701. }
  702. PUSH_F64(res);
  703. return true;
  704. fail:
  705. return false;
  706. }
  707. bool
  708. aot_compile_op_f64_convert_i64(AOTCompContext *comp_ctx,
  709. AOTFuncContext *func_ctx, bool sign)
  710. {
  711. LLVMValueRef value, res;
  712. POP_I64(value);
  713. if (comp_ctx->disable_llvm_intrinsics
  714. && aot_intrinsic_check_capability(
  715. comp_ctx, sign ? "f64_convert_i64_s" : "f64_convert_i64_u")) {
  716. LLVMTypeRef param_types[1];
  717. param_types[0] = I64_TYPE;
  718. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx,
  719. sign ? "f64_convert_i64_s"
  720. : "f64_convert_i64_u",
  721. F64_TYPE, param_types, 1, value);
  722. }
  723. else {
  724. if (sign)
  725. res = LLVMBuildSIToFP(comp_ctx->builder, value, F64_TYPE,
  726. "f64_convert_i64_s");
  727. else
  728. res = LLVMBuildUIToFP(comp_ctx->builder, value, F64_TYPE,
  729. "f64_convert_i64_u");
  730. }
  731. if (!res) {
  732. aot_set_last_error("llvm build conversion failed.");
  733. return false;
  734. }
  735. PUSH_F64(res);
  736. return true;
  737. fail:
  738. return false;
  739. }
  740. bool
  741. aot_compile_op_f64_promote_f32(AOTCompContext *comp_ctx,
  742. AOTFuncContext *func_ctx)
  743. {
  744. LLVMValueRef value, res;
  745. POP_F32(value);
  746. if (comp_ctx->disable_llvm_intrinsics
  747. && aot_intrinsic_check_capability(comp_ctx, "f64_promote_f32")) {
  748. LLVMTypeRef param_types[1];
  749. param_types[0] = F32_TYPE;
  750. res = aot_call_llvm_intrinsic(comp_ctx, func_ctx, "f64_promote_f32",
  751. F64_TYPE, param_types, 1, value);
  752. }
  753. else {
  754. res = LLVMBuildFPExt(comp_ctx->builder, value, F64_TYPE,
  755. "f64_promote_f32");
  756. }
  757. if (!res) {
  758. aot_set_last_error("llvm build conversion failed.");
  759. return false;
  760. }
  761. PUSH_F64(res);
  762. /* Avoid the promote being optimized away */
  763. PUSH_F64(F64_CONST(1.0));
  764. return aot_compile_op_f64_arithmetic(comp_ctx, func_ctx, FLOAT_MUL);
  765. fail:
  766. return false;
  767. }
  768. bool
  769. aot_compile_op_i64_reinterpret_f64(AOTCompContext *comp_ctx,
  770. AOTFuncContext *func_ctx)
  771. {
  772. LLVMValueRef value;
  773. POP_F64(value);
  774. if (!(value =
  775. LLVMBuildBitCast(comp_ctx->builder, value, I64_TYPE, "i64"))) {
  776. aot_set_last_error("llvm build fp to si failed.");
  777. return false;
  778. }
  779. PUSH_I64(value);
  780. return true;
  781. fail:
  782. return false;
  783. }
  784. bool
  785. aot_compile_op_i32_reinterpret_f32(AOTCompContext *comp_ctx,
  786. AOTFuncContext *func_ctx)
  787. {
  788. LLVMValueRef value;
  789. POP_F32(value);
  790. if (!(value =
  791. LLVMBuildBitCast(comp_ctx->builder, value, I32_TYPE, "i32"))) {
  792. aot_set_last_error("llvm build fp to si failed.");
  793. return false;
  794. }
  795. PUSH_I32(value);
  796. return true;
  797. fail:
  798. return false;
  799. }
  800. bool
  801. aot_compile_op_f64_reinterpret_i64(AOTCompContext *comp_ctx,
  802. AOTFuncContext *func_ctx)
  803. {
  804. LLVMValueRef value;
  805. POP_I64(value);
  806. if (!(value =
  807. LLVMBuildBitCast(comp_ctx->builder, value, F64_TYPE, "f64"))) {
  808. aot_set_last_error("llvm build si to fp failed.");
  809. return false;
  810. }
  811. PUSH_F64(value);
  812. return true;
  813. fail:
  814. return false;
  815. }
  816. bool
  817. aot_compile_op_f32_reinterpret_i32(AOTCompContext *comp_ctx,
  818. AOTFuncContext *func_ctx)
  819. {
  820. LLVMValueRef value;
  821. POP_I32(value);
  822. if (!(value =
  823. LLVMBuildBitCast(comp_ctx->builder, value, F32_TYPE, "f32"))) {
  824. aot_set_last_error("llvm build si to fp failed.");
  825. return false;
  826. }
  827. PUSH_F32(value);
  828. return true;
  829. fail:
  830. return false;
  831. }