]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/codegen_i128.rs
Auto merge of #82680 - jturner314:div_euclid-docs, r=JohnTitor
[rust.git] / compiler / rustc_codegen_cranelift / src / codegen_i128.rs
1 //! Replaces 128-bit operators with lang item calls where necessary
2
3 use cranelift_codegen::ir::ArgumentPurpose;
4
5 use crate::prelude::*;
6
7 pub(crate) fn maybe_codegen<'tcx>(
8     fx: &mut FunctionCx<'_, '_, 'tcx>,
9     bin_op: BinOp,
10     checked: bool,
11     lhs: CValue<'tcx>,
12     rhs: CValue<'tcx>,
13 ) -> Option<CValue<'tcx>> {
14     if lhs.layout().ty != fx.tcx.types.u128
15         && lhs.layout().ty != fx.tcx.types.i128
16         && rhs.layout().ty != fx.tcx.types.u128
17         && rhs.layout().ty != fx.tcx.types.i128
18     {
19         return None;
20     }
21
22     let lhs_val = lhs.load_scalar(fx);
23     let rhs_val = rhs.load_scalar(fx);
24
25     let is_signed = type_sign(lhs.layout().ty);
26
27     match bin_op {
28         BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
29             assert!(!checked);
30             None
31         }
32         BinOp::Add | BinOp::Sub if !checked => None,
33         BinOp::Mul if !checked => {
34             let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
35             Some(fx.easy_call("__multi3", &[lhs, rhs], val_ty))
36         }
37         BinOp::Add | BinOp::Sub | BinOp::Mul => {
38             assert!(checked);
39             let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
40             let out_place = CPlace::new_stack_slot(fx, fx.layout_of(out_ty));
41             let param_types = vec![
42                 AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
43                 AbiParam::new(types::I128),
44                 AbiParam::new(types::I128),
45             ];
46             let args = [out_place.to_ptr().get_addr(fx), lhs.load_scalar(fx), rhs.load_scalar(fx)];
47             let name = match (bin_op, is_signed) {
48                 (BinOp::Add, false) => "__rust_u128_addo",
49                 (BinOp::Add, true) => "__rust_i128_addo",
50                 (BinOp::Sub, false) => "__rust_u128_subo",
51                 (BinOp::Sub, true) => "__rust_i128_subo",
52                 (BinOp::Mul, false) => "__rust_u128_mulo",
53                 (BinOp::Mul, true) => "__rust_i128_mulo",
54                 _ => unreachable!(),
55             };
56             fx.lib_call(name, param_types, vec![], &args);
57             Some(out_place.to_cvalue(fx))
58         }
59         BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
60         BinOp::Div => {
61             assert!(!checked);
62             if is_signed {
63                 Some(fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128))
64             } else {
65                 Some(fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128))
66             }
67         }
68         BinOp::Rem => {
69             assert!(!checked);
70             if is_signed {
71                 Some(fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128))
72             } else {
73                 Some(fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128))
74             }
75         }
76         BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
77             assert!(!checked);
78             None
79         }
80         BinOp::Shl | BinOp::Shr => {
81             let is_overflow = if checked {
82                 // rhs >= 128
83
84                 // FIXME support non 128bit rhs
85                 /*let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
86                 let rhs_msb_gt_0 = fx.bcx.ins().icmp_imm(IntCC::NotEqual, rhs_msb, 0);
87                 let rhs_lsb_ge_128 = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThan, rhs_lsb, 127);
88                 let is_overflow = fx.bcx.ins().bor(rhs_msb_gt_0, rhs_lsb_ge_128);*/
89                 let is_overflow = fx.bcx.ins().bconst(types::B1, false);
90
91                 Some(fx.bcx.ins().bint(types::I8, is_overflow))
92             } else {
93                 None
94             };
95
96             let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
97             let val = match bin_op {
98                 BinOp::Shl => fx.bcx.ins().ishl(lhs_val, truncated_rhs),
99                 BinOp::Shr => {
100                     if is_signed {
101                         fx.bcx.ins().sshr(lhs_val, truncated_rhs)
102                     } else {
103                         fx.bcx.ins().ushr(lhs_val, truncated_rhs)
104                     }
105                 }
106                 _ => unreachable!(),
107             };
108             if let Some(is_overflow) = is_overflow {
109                 let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
110                 Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
111             } else {
112                 Some(CValue::by_val(val, lhs.layout()))
113             }
114         }
115     }
116 }