]> git.lizzy.rs Git - rust.git/blob - src/codegen_i128.rs
Sync from rust 8df945c4717ffaf923b57bf30c473df6fc98bc85
[rust.git] / 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 is_signed = type_sign(lhs.layout().ty);
23
24     match bin_op {
25         BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
26             assert!(!checked);
27             None
28         }
29         BinOp::Add | BinOp::Sub if !checked => None,
30         BinOp::Mul if !checked => {
31             let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
32             if fx.tcx.sess.target.is_like_windows {
33                 let ret_place = CPlace::new_stack_slot(fx, lhs.layout());
34                 let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
35                 let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
36                 assert!(lhs_extra.is_none());
37                 assert!(rhs_extra.is_none());
38                 let args =
39                     [ret_place.to_ptr().get_addr(fx), lhs_ptr.get_addr(fx), rhs_ptr.get_addr(fx)];
40                 fx.lib_call(
41                     "__multi3",
42                     vec![
43                         AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
44                         AbiParam::new(pointer_ty(fx.tcx)),
45                         AbiParam::new(pointer_ty(fx.tcx)),
46                     ],
47                     vec![],
48                     &args,
49                 );
50                 Some(ret_place.to_cvalue(fx))
51             } else {
52                 Some(fx.easy_call("__multi3", &[lhs, rhs], val_ty))
53             }
54         }
55         BinOp::Add | BinOp::Sub | BinOp::Mul => {
56             assert!(checked);
57             let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
58             let out_place = CPlace::new_stack_slot(fx, fx.layout_of(out_ty));
59             let (param_types, args) = if fx.tcx.sess.target.is_like_windows {
60                 let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
61                 let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
62                 assert!(lhs_extra.is_none());
63                 assert!(rhs_extra.is_none());
64                 (
65                     vec![
66                         AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
67                         AbiParam::new(pointer_ty(fx.tcx)),
68                         AbiParam::new(pointer_ty(fx.tcx)),
69                     ],
70                     [out_place.to_ptr().get_addr(fx), lhs_ptr.get_addr(fx), rhs_ptr.get_addr(fx)],
71                 )
72             } else {
73                 (
74                     vec![
75                         AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
76                         AbiParam::new(types::I128),
77                         AbiParam::new(types::I128),
78                     ],
79                     [out_place.to_ptr().get_addr(fx), lhs.load_scalar(fx), rhs.load_scalar(fx)],
80                 )
81             };
82             let name = match (bin_op, is_signed) {
83                 (BinOp::Add, false) => "__rust_u128_addo",
84                 (BinOp::Add, true) => "__rust_i128_addo",
85                 (BinOp::Sub, false) => "__rust_u128_subo",
86                 (BinOp::Sub, true) => "__rust_i128_subo",
87                 (BinOp::Mul, false) => "__rust_u128_mulo",
88                 (BinOp::Mul, true) => "__rust_i128_mulo",
89                 _ => unreachable!(),
90             };
91             fx.lib_call(name, param_types, vec![], &args);
92             Some(out_place.to_cvalue(fx))
93         }
94         BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
95         BinOp::Div | BinOp::Rem => {
96             assert!(!checked);
97             let name = match (bin_op, is_signed) {
98                 (BinOp::Div, false) => "__udivti3",
99                 (BinOp::Div, true) => "__divti3",
100                 (BinOp::Rem, false) => "__umodti3",
101                 (BinOp::Rem, true) => "__modti3",
102                 _ => unreachable!(),
103             };
104             if fx.tcx.sess.target.is_like_windows {
105                 let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
106                 let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
107                 assert!(lhs_extra.is_none());
108                 assert!(rhs_extra.is_none());
109                 let args = [lhs_ptr.get_addr(fx), rhs_ptr.get_addr(fx)];
110                 let ret = fx.lib_call(
111                     name,
112                     vec![AbiParam::new(pointer_ty(fx.tcx)), AbiParam::new(pointer_ty(fx.tcx))],
113                     vec![AbiParam::new(types::I64X2)],
114                     &args,
115                 )[0];
116                 // FIXME use bitcast instead of store to get from i64x2 to i128
117                 let ret_place = CPlace::new_stack_slot(fx, lhs.layout());
118                 ret_place.to_ptr().store(fx, ret, MemFlags::trusted());
119                 Some(ret_place.to_cvalue(fx))
120             } else {
121                 Some(fx.easy_call(name, &[lhs, rhs], lhs.layout().ty))
122             }
123         }
124         BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
125             assert!(!checked);
126             None
127         }
128         BinOp::Shl | BinOp::Shr => None,
129     }
130 }