]> git.lizzy.rs Git - rust.git/blob - src/codegen_i128.rs
Merge pull request #627 from bjorn3/wip_i128
[rust.git] / src / codegen_i128.rs
1 //! Replaces 128-bit operators with lang item calls
2
3 use crate::prelude::*;
4
5 pub fn maybe_codegen<'a, 'tcx>(
6     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
7     bin_op: BinOp,
8     checked: bool,
9     is_signed: bool,
10     lhs: CValue<'tcx>,
11     rhs: CValue<'tcx>,
12     out_ty: Ty<'tcx>,
13 ) -> Option<CValue<'tcx>> {
14     if lhs.layout().ty != fx.tcx.types.u128 && lhs.layout().ty != fx.tcx.types.i128 {
15         return None;
16     }
17
18     let lhs_val = lhs.load_scalar(fx);
19     let rhs_val = rhs.load_scalar(fx);
20
21     match bin_op {
22         BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
23             assert!(!checked);
24             return None;
25         }
26         BinOp::Add | BinOp::Sub if !checked => return None,
27         BinOp::Add => {
28             return Some(if is_signed {
29                 fx.easy_call("__rust_i128_addo", &[lhs, rhs], out_ty)
30             } else {
31                 fx.easy_call("__rust_u128_addo", &[lhs, rhs], out_ty)
32             })
33         }
34         BinOp::Sub => {
35             return Some(if is_signed {
36                 fx.easy_call("__rust_i128_subo", &[lhs, rhs], out_ty)
37             } else {
38                 fx.easy_call("__rust_u128_subo", &[lhs, rhs], out_ty)
39             })
40         }
41         BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
42         BinOp::Mul => {
43             let res = if checked {
44                 if is_signed {
45                     fx.easy_call("__rust_i128_mulo", &[lhs, rhs], out_ty)
46                 } else {
47                     fx.easy_call("__rust_u128_mulo", &[lhs, rhs], out_ty)
48                 }
49             } else {
50                 let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
51                 fx.easy_call("__multi3", &[lhs, rhs], val_ty)
52             };
53             return Some(res);
54         }
55         BinOp::Div => {
56             assert!(!checked);
57             if is_signed {
58                 Some(fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128))
59             } else {
60                 Some(fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128))
61             }
62         }
63         BinOp::Rem => {
64             assert!(!checked);
65             if is_signed {
66                 Some(fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128))
67             } else {
68                 Some(fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128))
69             }
70         }
71         BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
72             assert!(!checked);
73             let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
74             let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
75
76             let res = match bin_op {
77                 BinOp::Eq => {
78                     let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb);
79                     let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
80                     fx.bcx.ins().band(lsb_eq, msb_eq)
81                 }
82                 BinOp::Ne => {
83                     let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb);
84                     let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb);
85                     fx.bcx.ins().bor(lsb_ne, msb_ne)
86                 }
87                 _ => {
88                     // if msb_eq {
89                     //     lhs_cc
90                     // } else {
91                     //     msb_cc
92                     // }
93                     let cc = match (bin_op, is_signed) {
94                         (BinOp::Ge, false) => IntCC::UnsignedGreaterThanOrEqual,
95                         (BinOp::Gt, false) => IntCC::UnsignedGreaterThan,
96                         (BinOp::Lt, false) => IntCC::UnsignedLessThan,
97                         (BinOp::Le, false) => IntCC::UnsignedLessThanOrEqual,
98
99                         (BinOp::Ge, true) => IntCC::SignedGreaterThanOrEqual,
100                         (BinOp::Gt, true) => IntCC::SignedGreaterThan,
101                         (BinOp::Lt, true) => IntCC::SignedLessThan,
102                         (BinOp::Le, true) => IntCC::SignedLessThanOrEqual,
103                         _ => unreachable!(),
104                     };
105
106                     let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
107                     let lsb_cc = fx.bcx.ins().icmp(cc, lhs_lsb, rhs_lsb);
108                     let msb_cc = fx.bcx.ins().icmp(cc, lhs_msb, rhs_msb);
109
110                     fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
111                 }
112             };
113
114             let res = fx.bcx.ins().bint(types::I8, res);
115             let res = CValue::by_val(res, fx.layout_of(fx.tcx.types.bool));
116             return Some(res);
117         }
118         BinOp::Shl | BinOp::Shr => {
119             let is_overflow = if checked {
120                 // rhs >= 128
121
122                 // FIXME support non 128bit rhs
123                 /*let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
124                 let rhs_msb_gt_0 = fx.bcx.ins().icmp_imm(IntCC::NotEqual, rhs_msb, 0);
125                 let rhs_lsb_ge_128 = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThan, rhs_lsb, 127);
126                 let is_overflow = fx.bcx.ins().bor(rhs_msb_gt_0, rhs_lsb_ge_128);*/
127                 let is_overflow = fx.bcx.ins().bconst(types::B1, false);
128
129                 Some(fx.bcx.ins().bint(types::I8, is_overflow))
130             } else {
131                 None
132             };
133
134             // Optimize `val >> 64`, because compiler_builtins uses it to deconstruct an 128bit
135             // integer into its lsb and msb.
136             // https://github.com/rust-lang-nursery/compiler-builtins/blob/79a6a1603d5672cbb9187ff41ff4d9b5048ac1cb/src/int/mod.rs#L217
137             if let Some(64) = resolve_value_imm(fx.bcx.func, rhs_val) {
138                 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
139                 let all_zeros = fx.bcx.ins().iconst(types::I64, 0);
140                 let val = match (bin_op, is_signed) {
141                     (BinOp::Shr, false) => {
142                         let val = fx.bcx.ins().iconcat(lhs_msb, all_zeros);
143                         Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.u128)))
144                     }
145                     (BinOp::Shr, true) => {
146                         let sign = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, lhs_msb, 0);
147                         let all_ones = fx.bcx.ins().iconst(types::I64, u64::max_value() as i64);
148                         let all_sign_bits = fx.bcx.ins().select(sign, all_zeros, all_ones);
149
150                         let val = fx.bcx.ins().iconcat(lhs_msb, all_sign_bits);
151                         Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.i128)))
152                     }
153                     (BinOp::Shl, _) => {
154                         let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
155                         let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb);
156                         Some(CValue::by_val(val, fx.layout_of(val_ty)))
157                     }
158                     _ => None
159                 };
160                 if let Some(val) = val {
161                     if let Some(is_overflow) = is_overflow {
162                         let val = val.load_scalar(fx);
163                         return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
164                     } else {
165                         return Some(val);
166                     }
167                 }
168             }
169
170             let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
171             let truncated_rhs = CValue::by_val(truncated_rhs, fx.layout_of(fx.tcx.types.u32));
172             let val = match (bin_op, is_signed) {
173                 (BinOp::Shl, false) => {
174                     fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
175                 }
176                 (BinOp::Shl, true) => {
177                     fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
178                 }
179                 (BinOp::Shr, false) => {
180                     fx.easy_call("__lshrti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
181                 }
182                 (BinOp::Shr, true) => {
183                     fx.easy_call("__ashrti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
184                 }
185                 (_, _) => unreachable!(),
186             };
187             if let Some(is_overflow) = is_overflow {
188                 let val = val.load_scalar(fx);
189                 Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
190             } else {
191                 Some(val)
192             }
193         }
194     }
195 }