]> git.lizzy.rs Git - rust.git/commitdiff
Implement lt, le, ge and gt for fat pointers
authorbjorn3 <bjorn3@users.noreply.github.com>
Sat, 4 May 2019 16:40:49 +0000 (18:40 +0200)
committerbjorn3 <bjorn3@users.noreply.github.com>
Sun, 16 Jun 2019 15:59:31 +0000 (17:59 +0200)
src/base.rs

index 2b4cd42c7579fe41e3bb1417f61294c7690eedef..3f5b003a24de83e42d99ab2c0c8e9691ec84f6f2 100644 (file)
@@ -1052,6 +1052,7 @@ fn trans_ptr_binop<'a, 'tcx: 'a>(
     } else {
         let (lhs_ptr, lhs_extra) = lhs.load_scalar_pair(fx);
         let (rhs_ptr, rhs_extra) = rhs.load_scalar_pair(fx);
+
         let res = match bin_op {
             BinOp::Eq => {
                 let ptr_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_ptr, rhs_ptr);
@@ -1063,10 +1064,28 @@ fn trans_ptr_binop<'a, 'tcx: 'a>(
                 let extra_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_extra, rhs_extra);
                 fx.bcx.ins().bor(ptr_ne, extra_ne)
             }
-            _ => unimplemented!(
-                "trans_ptr_binop({:?}, <fat ptr>, <fat ptr>) not implemented",
-                bin_op
-            ),
+            BinOp::Lt | BinOp::Le | BinOp::Ge | BinOp::Gt => {
+                let ptr_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_ptr, rhs_ptr);
+
+                let ptr_cmp = fx.bcx.ins().icmp(match bin_op {
+                    BinOp::Lt => IntCC::UnsignedLessThan,
+                    BinOp::Le => IntCC::UnsignedLessThanOrEqual,
+                    BinOp::Ge => IntCC::UnsignedGreaterThanOrEqual,
+                    BinOp::Gt => IntCC::UnsignedGreaterThan,
+                    _ => unreachable!(),
+                }, lhs_ptr, rhs_ptr);
+
+                let extra_cmp = fx.bcx.ins().icmp(match bin_op {
+                    BinOp::Lt => IntCC::UnsignedLessThan,
+                    BinOp::Le => IntCC::UnsignedLessThanOrEqual,
+                    BinOp::Ge => IntCC::UnsignedGreaterThanOrEqual,
+                    BinOp::Gt => IntCC::UnsignedGreaterThan,
+                    _ => unreachable!(),
+                }, lhs_extra, rhs_extra);
+
+                fx.bcx.ins().select(ptr_eq, extra_cmp, ptr_cmp)
+            }
+            _ => panic!("bin_op {:?} on ptr", bin_op),
         };
 
         assert_eq!(fx.tcx.types.bool, ret_ty);