]> git.lizzy.rs Git - rust.git/blobdiff - src/codegen_i128.rs
Add custom driver
[rust.git] / src / codegen_i128.rs
index 6fdd3042c52688255d27816e640ee2b3d4f7e5b0..99bd526ad01a8ddc5d9c0a2af5f9faa3108882dc 100644 (file)
@@ -1,14 +1,13 @@
-//! Replaces 128-bit operators with lang item calls
+//! Replaces 128-bit operators with lang item calls where necessary
 
 use crate::prelude::*;
 
-pub fn maybe_codegen<'a, 'tcx>(
-    fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
+pub(crate) fn maybe_codegen<'tcx>(
+    fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
     bin_op: BinOp,
     checked: bool,
     lhs: CValue<'tcx>,
     rhs: CValue<'tcx>,
-    out_ty: Ty<'tcx>,
 ) -> Option<CValue<'tcx>> {
     if lhs.layout().ty != fx.tcx.types.u128 && lhs.layout().ty != fx.tcx.types.i128 {
         return None;
@@ -26,29 +25,36 @@ pub fn maybe_codegen<'a, 'tcx>(
         }
         BinOp::Add | BinOp::Sub if !checked => return None,
         BinOp::Add => {
+            let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
             return Some(if is_signed {
                 fx.easy_call("__rust_i128_addo", &[lhs, rhs], out_ty)
             } else {
                 fx.easy_call("__rust_u128_addo", &[lhs, rhs], out_ty)
-            })
+            });
         }
         BinOp::Sub => {
+            let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
             return Some(if is_signed {
                 fx.easy_call("__rust_i128_subo", &[lhs, rhs], out_ty)
             } else {
                 fx.easy_call("__rust_u128_subo", &[lhs, rhs], out_ty)
-            })
+            });
         }
         BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
         BinOp::Mul => {
             let res = if checked {
+                let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
                 if is_signed {
                     fx.easy_call("__rust_i128_mulo", &[lhs, rhs], out_ty)
                 } else {
                     fx.easy_call("__rust_u128_mulo", &[lhs, rhs], out_ty)
                 }
             } else {
-                let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
+                let val_ty = if is_signed {
+                    fx.tcx.types.i128
+                } else {
+                    fx.tcx.types.u128
+                };
                 fx.easy_call("__multi3", &[lhs, rhs], val_ty)
             };
             return Some(res);
@@ -71,50 +77,7 @@ pub fn maybe_codegen<'a, 'tcx>(
         }
         BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
             assert!(!checked);
-            let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
-            let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
-
-            let res = match bin_op {
-                BinOp::Eq => {
-                    let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb);
-                    let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
-                    fx.bcx.ins().band(lsb_eq, msb_eq)
-                }
-                BinOp::Ne => {
-                    let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb);
-                    let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb);
-                    fx.bcx.ins().bor(lsb_ne, msb_ne)
-                }
-                _ => {
-                    // if msb_eq {
-                    //     lsb_cc
-                    // } else {
-                    //     msb_cc
-                    // }
-                    let cc = match (bin_op, is_signed) {
-                        (BinOp::Ge, false) => IntCC::UnsignedGreaterThanOrEqual,
-                        (BinOp::Gt, false) => IntCC::UnsignedGreaterThan,
-                        (BinOp::Lt, false) => IntCC::UnsignedLessThan,
-                        (BinOp::Le, false) => IntCC::UnsignedLessThanOrEqual,
-
-                        (BinOp::Ge, true) => IntCC::SignedGreaterThanOrEqual,
-                        (BinOp::Gt, true) => IntCC::SignedGreaterThan,
-                        (BinOp::Lt, true) => IntCC::SignedLessThan,
-                        (BinOp::Le, true) => IntCC::SignedLessThanOrEqual,
-                        _ => unreachable!(),
-                    };
-
-                    let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
-                    let lsb_cc = fx.bcx.ins().icmp(cc, lhs_lsb, rhs_lsb);
-                    let msb_cc = fx.bcx.ins().icmp(cc, lhs_msb, rhs_msb);
-
-                    fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
-                }
-            };
-
-            let res = fx.bcx.ins().bint(types::I8, res);
-            let res = CValue::by_val(res, fx.layout_of(fx.tcx.types.bool));
-            return Some(res);
+            return None;
         }
         BinOp::Shl | BinOp::Shr => {
             let is_overflow = if checked {
@@ -135,7 +98,7 @@ pub fn maybe_codegen<'a, 'tcx>(
             // Optimize `val >> 64`, because compiler_builtins uses it to deconstruct an 128bit
             // integer into its lsb and msb.
             // https://github.com/rust-lang-nursery/compiler-builtins/blob/79a6a1603d5672cbb9187ff41ff4d9b5048ac1cb/src/int/mod.rs#L217
-            if let Some(64) = resolve_value_imm(fx.bcx.func, rhs_val) {
+            if resolve_value_imm(fx.bcx.func, rhs_val) == Some(64) {
                 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
                 let all_zeros = fx.bcx.ins().iconst(types::I64, 0);
                 let val = match (bin_op, is_signed) {
@@ -145,23 +108,28 @@ pub fn maybe_codegen<'a, 'tcx>(
                     }
                     (BinOp::Shr, true) => {
                         let sign = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, lhs_msb, 0);
-                        let all_ones = fx.bcx.ins().iconst(types::I64, u64::max_value() as i64);
+                        let all_ones = fx.bcx.ins().iconst(types::I64, u64::MAX as i64);
                         let all_sign_bits = fx.bcx.ins().select(sign, all_zeros, all_ones);
 
                         let val = fx.bcx.ins().iconcat(lhs_msb, all_sign_bits);
                         Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.i128)))
                     }
                     (BinOp::Shl, _) => {
-                        let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
+                        let val_ty = if is_signed {
+                            fx.tcx.types.i128
+                        } else {
+                            fx.tcx.types.u128
+                        };
                         let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb);
                         Some(CValue::by_val(val, fx.layout_of(val_ty)))
                     }
-                    _ => None
+                    _ => None,
                 };
                 if let Some(val) = val {
                     if let Some(is_overflow) = is_overflow {
+                        let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
                         let val = val.load_scalar(fx);
-                        return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
+                        return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)));
                     } else {
                         return Some(val);
                     }
@@ -186,6 +154,7 @@ pub fn maybe_codegen<'a, 'tcx>(
                 (_, _) => unreachable!(),
             };
             if let Some(is_overflow) = is_overflow {
+                let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
                 let val = val.load_scalar(fx);
                 Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
             } else {