]> git.lizzy.rs Git - rust.git/blobdiff - src/shims/intrinsics.rs
Auto merge of #2245 - saethlin:color-always, r=RalfJung
[rust.git] / src / shims / intrinsics.rs
index 9101cbcf05ff4e7887a3c88a52c3d66da21f227c..8d4da31fd0d777de75d0f4811b7199b5cbef2c3e 100644 (file)
@@ -1,4 +1,3 @@
-use std::convert::TryInto;
 use std::iter;
 
 use log::trace;
@@ -23,19 +22,20 @@ fn call_intrinsic(
         &mut self,
         instance: ty::Instance<'tcx>,
         args: &[OpTy<'tcx, Tag>],
-        ret: Option<(&PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
+        dest: &PlaceTy<'tcx, Tag>,
+        ret: Option<mir::BasicBlock>,
         _unwind: StackPopUnwind,
     ) -> InterpResult<'tcx> {
         let this = self.eval_context_mut();
 
-        if this.emulate_intrinsic(instance, args, ret)? {
+        if this.emulate_intrinsic(instance, args, dest, ret)? {
             return Ok(());
         }
 
         // All supported intrinsics have a return place.
         let intrinsic_name = this.tcx.item_name(instance.def_id());
         let intrinsic_name = intrinsic_name.as_str();
-        let (dest, ret) = match ret {
+        let ret = match ret {
             None => throw_unsup_format!("unimplemented (diverging) intrinsic: {}", intrinsic_name),
             Some(p) => p,
         };
@@ -44,13 +44,13 @@ fn call_intrinsic(
         match intrinsic_name {
             // Miri overwriting CTFE intrinsics.
             "ptr_guaranteed_eq" => {
-                let &[ref left, ref right] = check_arg_count(args)?;
+                let [left, right] = check_arg_count(args)?;
                 let left = this.read_immediate(left)?;
                 let right = this.read_immediate(right)?;
                 this.binop_ignore_overflow(mir::BinOp::Eq, &left, &right, dest)?;
             }
             "ptr_guaranteed_ne" => {
-                let &[ref left, ref right] = check_arg_count(args)?;
+                let [left, right] = check_arg_count(args)?;
                 let left = this.read_immediate(left)?;
                 let right = this.read_immediate(right)?;
                 this.binop_ignore_overflow(mir::BinOp::Ne, &left, &right, dest)?;
@@ -66,18 +66,18 @@ fn call_intrinsic(
 
             // Raw memory accesses
             "volatile_load" => {
-                let &[ref place] = check_arg_count(args)?;
+                let [place] = check_arg_count(args)?;
                 let place = this.deref_operand(place)?;
                 this.copy_op(&place.into(), dest)?;
             }
             "volatile_store" => {
-                let &[ref place, ref dest] = check_arg_count(args)?;
+                let [place, dest] = check_arg_count(args)?;
                 let place = this.deref_operand(place)?;
                 this.copy_op(dest, &place.into())?;
             }
 
             "write_bytes" | "volatile_set_memory" => {
-                let &[ref ptr, ref val_byte, ref count] = check_arg_count(args)?;
+                let [ptr, val_byte, count] = check_arg_count(args)?;
                 let ty = instance.substs.type_at(0);
                 let ty_layout = this.layout_of(ty)?;
                 let val_byte = this.read_scalar(val_byte)?.to_u8()?;
@@ -96,13 +96,13 @@ fn call_intrinsic(
 
             // Floating-point operations
             "fabsf32" => {
-                let &[ref f] = check_arg_count(args)?;
+                let [f] = check_arg_count(args)?;
                 let f = this.read_scalar(f)?.to_f32()?;
                 // Can be implemented in soft-floats.
                 this.write_scalar(Scalar::from_f32(f.abs()), dest)?;
             }
             "fabsf64" => {
-                let &[ref f] = check_arg_count(args)?;
+                let [f] = check_arg_count(args)?;
                 let f = this.read_scalar(f)?.to_f64()?;
                 // Can be implemented in soft-floats.
                 this.write_scalar(Scalar::from_f64(f.abs()), dest)?;
@@ -121,7 +121,7 @@ fn call_intrinsic(
             | "truncf32"
             | "roundf32"
             => {
-                let &[ref f] = check_arg_count(args)?;
+                let [f] = check_arg_count(args)?;
                 // FIXME: Using host floats.
                 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
                 let f = match intrinsic_name {
@@ -156,7 +156,7 @@ fn call_intrinsic(
             | "truncf64"
             | "roundf64"
             => {
-                let &[ref f] = check_arg_count(args)?;
+                let [f] = check_arg_count(args)?;
                 // FIXME: Using host floats.
                 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
                 let f = match intrinsic_name {
@@ -184,7 +184,7 @@ fn call_intrinsic(
             | "fdiv_fast"
             | "frem_fast"
             => {
-                let &[ref a, ref b] = check_arg_count(args)?;
+                let [a, b] = check_arg_count(args)?;
                 let a = this.read_immediate(a)?;
                 let b = this.read_immediate(b)?;
                 let op = match intrinsic_name {
@@ -229,7 +229,7 @@ fn call_intrinsic(
             | "maxnumf32"
             | "copysignf32"
             => {
-                let &[ref a, ref b] = check_arg_count(args)?;
+                let [a, b] = check_arg_count(args)?;
                 let a = this.read_scalar(a)?.to_f32()?;
                 let b = this.read_scalar(b)?.to_f32()?;
                 let res = match intrinsic_name {
@@ -246,7 +246,7 @@ fn call_intrinsic(
             | "maxnumf64"
             | "copysignf64"
             => {
-                let &[ref a, ref b] = check_arg_count(args)?;
+                let [a, b] = check_arg_count(args)?;
                 let a = this.read_scalar(a)?.to_f64()?;
                 let b = this.read_scalar(b)?.to_f64()?;
                 let res = match intrinsic_name {
@@ -259,7 +259,7 @@ fn call_intrinsic(
             }
 
             "powf32" => {
-                let &[ref f, ref f2] = check_arg_count(args)?;
+                let [f, f2] = check_arg_count(args)?;
                 // FIXME: Using host floats.
                 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
                 let f2 = f32::from_bits(this.read_scalar(f2)?.to_u32()?);
@@ -267,7 +267,7 @@ fn call_intrinsic(
             }
 
             "powf64" => {
-                let &[ref f, ref f2] = check_arg_count(args)?;
+                let [f, f2] = check_arg_count(args)?;
                 // FIXME: Using host floats.
                 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
                 let f2 = f64::from_bits(this.read_scalar(f2)?.to_u64()?);
@@ -275,7 +275,7 @@ fn call_intrinsic(
             }
 
             "fmaf32" => {
-                let &[ref a, ref b, ref c] = check_arg_count(args)?;
+                let [a, b, c] = check_arg_count(args)?;
                 let a = this.read_scalar(a)?.to_f32()?;
                 let b = this.read_scalar(b)?.to_f32()?;
                 let c = this.read_scalar(c)?.to_f32()?;
@@ -284,7 +284,7 @@ fn call_intrinsic(
             }
 
             "fmaf64" => {
-                let &[ref a, ref b, ref c] = check_arg_count(args)?;
+                let [a, b, c] = check_arg_count(args)?;
                 let a = this.read_scalar(a)?.to_f64()?;
                 let b = this.read_scalar(b)?.to_f64()?;
                 let c = this.read_scalar(c)?.to_f64()?;
@@ -293,7 +293,7 @@ fn call_intrinsic(
             }
 
             "powif32" => {
-                let &[ref f, ref i] = check_arg_count(args)?;
+                let [f, i] = check_arg_count(args)?;
                 // FIXME: Using host floats.
                 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
                 let i = this.read_scalar(i)?.to_i32()?;
@@ -301,7 +301,7 @@ fn call_intrinsic(
             }
 
             "powif64" => {
-                let &[ref f, ref i] = check_arg_count(args)?;
+                let [f, i] = check_arg_count(args)?;
                 // FIXME: Using host floats.
                 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
                 let i = this.read_scalar(i)?.to_i32()?;
@@ -309,7 +309,7 @@ fn call_intrinsic(
             }
 
             "float_to_int_unchecked" => {
-                let &[ref val] = check_arg_count(args)?;
+                let [val] = check_arg_count(args)?;
                 let val = this.read_immediate(val)?;
 
                 let res = match val.layout.ty.kind() {
@@ -336,7 +336,7 @@ fn call_intrinsic(
             | "simd_round"
             | "simd_trunc"
             | "simd_fsqrt" => {
-                let &[ref op] = check_arg_count(args)?;
+                let [op] = check_arg_count(args)?;
                 let (op, op_len) = this.operand_to_simd(op)?;
                 let (dest, dest_len) = this.place_to_simd(dest)?;
 
@@ -438,10 +438,11 @@ enum Op {
             | "simd_fmax"
             | "simd_fmin"
             | "simd_saturating_add"
-            | "simd_saturating_sub" => {
+            | "simd_saturating_sub"
+            | "simd_arith_offset" => {
                 use mir::BinOp;
 
-                let &[ref left, ref right] = check_arg_count(args)?;
+                let [left, right] = check_arg_count(args)?;
                 let (left, left_len) = this.operand_to_simd(left)?;
                 let (right, right_len) = this.operand_to_simd(right)?;
                 let (dest, dest_len) = this.place_to_simd(dest)?;
@@ -454,6 +455,7 @@ enum Op {
                     SaturatingOp(BinOp),
                     FMax,
                     FMin,
+                    WrappingOffset,
                 }
                 let which = match intrinsic_name {
                     "simd_add" => Op::MirOp(BinOp::Add),
@@ -476,6 +478,7 @@ enum Op {
                     "simd_fmin" => Op::FMin,
                     "simd_saturating_add" => Op::SaturatingOp(BinOp::Add),
                     "simd_saturating_sub" => Op::SaturatingOp(BinOp::Sub),
+                    "simd_arith_offset" => Op::WrappingOffset,
                     _ => unreachable!(),
                 };
 
@@ -505,21 +508,31 @@ enum Op {
                                 val
                             }
                         }
+                        Op::SaturatingOp(mir_op) => {
+                            this.saturating_arith(mir_op, &left, &right)?
+                        }
+                        Op::WrappingOffset => {
+                            let ptr = this.scalar_to_ptr(left.to_scalar()?)?;
+                            let offset_count = right.to_scalar()?.to_machine_isize(this)?;
+                            let pointee_ty = left.layout.ty.builtin_deref(true).unwrap().ty;
+
+                            let pointee_size = i64::try_from(this.layout_of(pointee_ty)?.size.bytes()).unwrap();
+                            let offset_bytes = offset_count.wrapping_mul(pointee_size);
+                            let offset_ptr = ptr.wrapping_signed_offset(offset_bytes, this);
+                            Scalar::from_maybe_pointer(offset_ptr, this)
+                        }
                         Op::FMax => {
                             fmax_op(&left, &right)?
                         }
                         Op::FMin => {
                             fmin_op(&left, &right)?
                         }
-                        Op::SaturatingOp(mir_op) => {
-                            this.saturating_arith(mir_op, &left, &right)?
-                        }
                     };
                     this.write_scalar(val, &dest.into())?;
                 }
             }
             "simd_fma" => {
-                let &[ref a, ref b, ref c] = check_arg_count(args)?;
+                let [a, b, c] = check_arg_count(args)?;
                 let (a, a_len) = this.operand_to_simd(a)?;
                 let (b, b_len) = this.operand_to_simd(b)?;
                 let (c, c_len) = this.operand_to_simd(c)?;
@@ -558,7 +571,7 @@ enum Op {
             | "simd_reduce_min" => {
                 use mir::BinOp;
 
-                let &[ref op] = check_arg_count(args)?;
+                let [op] = check_arg_count(args)?;
                 let (op, op_len) = this.operand_to_simd(op)?;
 
                 let imm_from_bool =
@@ -630,7 +643,7 @@ enum Op {
             | "simd_reduce_mul_ordered" => {
                 use mir::BinOp;
 
-                let &[ref op, ref init] = check_arg_count(args)?;
+                let [op, init] = check_arg_count(args)?;
                 let (op, op_len) = this.operand_to_simd(op)?;
                 let init = this.read_immediate(init)?;
 
@@ -648,7 +661,7 @@ enum Op {
                 this.write_immediate(*res, dest)?;
             }
             "simd_select" => {
-                let &[ref mask, ref yes, ref no] = check_arg_count(args)?;
+                let [mask, yes, no] = check_arg_count(args)?;
                 let (mask, mask_len) = this.operand_to_simd(mask)?;
                 let (yes, yes_len) = this.operand_to_simd(yes)?;
                 let (no, no_len) = this.operand_to_simd(no)?;
@@ -669,7 +682,7 @@ enum Op {
                 }
             }
             "simd_select_bitmask" => {
-                let &[ref mask, ref yes, ref no] = check_arg_count(args)?;
+                let [mask, yes, no] = check_arg_count(args)?;
                 let (yes, yes_len) = this.operand_to_simd(yes)?;
                 let (no, no_len) = this.operand_to_simd(no)?;
                 let (dest, dest_len) = this.place_to_simd(dest)?;
@@ -709,7 +722,7 @@ enum Op {
             }
             #[rustfmt::skip]
             "simd_cast" | "simd_as" => {
-                let &[ref op] = check_arg_count(args)?;
+                let [op] = check_arg_count(args)?;
                 let (op, op_len) = this.operand_to_simd(op)?;
                 let (dest, dest_len) = this.place_to_simd(dest)?;
 
@@ -747,7 +760,7 @@ enum Op {
                 }
             }
             "simd_shuffle" => {
-                let &[ref left, ref right, ref index] = check_arg_count(args)?;
+                let [left, right, index] = check_arg_count(args)?;
                 let (left, left_len) = this.operand_to_simd(left)?;
                 let (right, right_len) = this.operand_to_simd(right)?;
                 let (dest, dest_len) = this.place_to_simd(dest)?;
@@ -763,7 +776,7 @@ enum Op {
 
                 for i in 0..dest_len {
                     let src_index: u64 = this
-                        .read_immediate(&this.operand_index(&index, i)?.into())?
+                        .read_immediate(&this.operand_index(index, i)?)?
                         .to_scalar()?
                         .to_u32()?
                         .into();
@@ -786,7 +799,7 @@ enum Op {
                 }
             }
             "simd_gather" => {
-                let &[ref passthru, ref ptrs, ref mask] = check_arg_count(args)?;
+                let [passthru, ptrs, mask] = check_arg_count(args)?;
                 let (passthru, passthru_len) = this.operand_to_simd(passthru)?;
                 let (ptrs, ptrs_len) = this.operand_to_simd(ptrs)?;
                 let (mask, mask_len) = this.operand_to_simd(mask)?;
@@ -812,7 +825,7 @@ enum Op {
                 }
             }
             "simd_scatter" => {
-                let &[ref value, ref ptrs, ref mask] = check_arg_count(args)?;
+                let [value, ptrs, mask] = check_arg_count(args)?;
                 let (value, value_len) = this.operand_to_simd(value)?;
                 let (ptrs, ptrs_len) = this.operand_to_simd(ptrs)?;
                 let (mask, mask_len) = this.operand_to_simd(mask)?;
@@ -832,7 +845,7 @@ enum Op {
                 }
             }
             "simd_bitmask" => {
-                let &[ref op] = check_arg_count(args)?;
+                let [op] = check_arg_count(args)?;
                 let (op, op_len) = this.operand_to_simd(op)?;
                 let bitmask_len = op_len.max(8);
 
@@ -1051,14 +1064,14 @@ enum Op {
 
             // Other
             "exact_div" => {
-                let &[ref num, ref denom] = check_arg_count(args)?;
+                let [num, denom] = check_arg_count(args)?;
                 this.exact_div(&this.read_immediate(num)?, &this.read_immediate(denom)?, dest)?;
             }
 
             "try" => return this.handle_try(args, dest, ret),
 
             "breakpoint" => {
-                let &[] = check_arg_count(args)?;
+                let [] = check_arg_count(args)?;
                 // normally this would raise a SIGTRAP, which aborts if no debugger is connected
                 throw_machine_stop!(TerminationInfo::Abort("Trace/breakpoint trap".to_string()))
             }
@@ -1079,7 +1092,7 @@ fn atomic_load(
     ) -> InterpResult<'tcx> {
         let this = self.eval_context_mut();
 
-        let &[ref place] = check_arg_count(args)?;
+        let [place] = check_arg_count(args)?;
         let place = this.deref_operand(place)?;
 
         // make sure it fits into a scalar; otherwise it cannot be atomic
@@ -1107,7 +1120,7 @@ fn atomic_store(
     ) -> InterpResult<'tcx> {
         let this = self.eval_context_mut();
 
-        let &[ref place, ref val] = check_arg_count(args)?;
+        let [place, val] = check_arg_count(args)?;
         let place = this.deref_operand(place)?;
         let val = this.read_scalar(val)?; // make sure it fits into a scalar; otherwise it cannot be atomic
 
@@ -1132,7 +1145,7 @@ fn compiler_fence(
         args: &[OpTy<'tcx, Tag>],
         atomic: AtomicFenceOp,
     ) -> InterpResult<'tcx> {
-        let &[] = check_arg_count(args)?;
+        let [] = check_arg_count(args)?;
         let _ = atomic;
         //FIXME: compiler fences are currently ignored
         Ok(())
@@ -1144,7 +1157,7 @@ fn atomic_fence(
         atomic: AtomicFenceOp,
     ) -> InterpResult<'tcx> {
         let this = self.eval_context_mut();
-        let &[] = check_arg_count(args)?;
+        let [] = check_arg_count(args)?;
         this.validate_atomic_fence(atomic)?;
         Ok(())
     }
@@ -1158,7 +1171,7 @@ fn atomic_op(
     ) -> InterpResult<'tcx> {
         let this = self.eval_context_mut();
 
-        let &[ref place, ref rhs] = check_arg_count(args)?;
+        let [place, rhs] = check_arg_count(args)?;
         let place = this.deref_operand(place)?;
 
         if !place.layout.ty.is_integral() {
@@ -1180,12 +1193,12 @@ fn atomic_op(
         match atomic_op {
             AtomicOp::Min => {
                 let old = this.atomic_min_max_scalar(&place, rhs, true, atomic)?;
-                this.write_immediate(*old, &dest)?; // old value is returned
+                this.write_immediate(*old, dest)?; // old value is returned
                 Ok(())
             }
             AtomicOp::Max => {
                 let old = this.atomic_min_max_scalar(&place, rhs, false, atomic)?;
-                this.write_immediate(*old, &dest)?; // old value is returned
+                this.write_immediate(*old, dest)?; // old value is returned
                 Ok(())
             }
             AtomicOp::MirOp(op, neg) => {
@@ -1204,7 +1217,7 @@ fn atomic_exchange(
     ) -> InterpResult<'tcx> {
         let this = self.eval_context_mut();
 
-        let &[ref place, ref new] = check_arg_count(args)?;
+        let [place, new] = check_arg_count(args)?;
         let place = this.deref_operand(place)?;
         let new = this.read_scalar(new)?;
 
@@ -1234,7 +1247,7 @@ fn atomic_compare_exchange_impl(
     ) -> InterpResult<'tcx> {
         let this = self.eval_context_mut();
 
-        let &[ref place, ref expect_old, ref new] = check_arg_count(args)?;
+        let [place, expect_old, new] = check_arg_count(args)?;
         let place = this.deref_operand(place)?;
         let expect_old = this.read_immediate(expect_old)?; // read as immediate for the sake of `binary_op()`
         let new = this.read_scalar(new)?;
@@ -1376,7 +1389,7 @@ fn bool_to_simd_element(b: bool, size: Size) -> Scalar<Tag> {
     Scalar::from_int(val, size)
 }
 
-fn simd_element_to_bool<'tcx>(elem: ImmTy<'tcx, Tag>) -> InterpResult<'tcx, bool> {
+fn simd_element_to_bool(elem: ImmTy<'_, Tag>) -> InterpResult<'_, bool> {
     let val = elem.to_scalar()?.to_int(elem.layout.size)?;
     Ok(match val {
         0 => false,