]> git.lizzy.rs Git - rust.git/commitdiff
Add intrinsics for unchecked division and modulo
authorNicholas Mazzuca <npmazzuca@gmail.com>
Wed, 29 Apr 2015 01:20:30 +0000 (18:20 -0700)
committerNicholas Mazzuca <npmazzuca@gmail.com>
Wed, 29 Apr 2015 01:20:30 +0000 (18:20 -0700)
The "unchecked_" div and rem functions will give UB in case of rhs == 0, or,
in the signed versions, lhs == INT::min and rhs == -1

src/libcore/intrinsics.rs
src/librustc_trans/trans/context.rs
src/librustc_trans/trans/intrinsic.rs
src/librustc_typeck/check/mod.rs

index 8ed89adec5b69385d208b821dcd39cfb67530497..c6bb1fb1cb164c545cd7896bdb27781119661c53 100644 (file)
@@ -576,3 +576,20 @@ pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
     #[cfg(not(stage0))]
     pub fn discriminant_value<T>(v: &T) -> u64;
 }
+
+#[cfg(not(stage0))]
+extern "rust-intrinsic" {
+    /// Performs an unchecked signed division, which results in undefined behavior,
+    /// in cases where y == 0, or x == int::MIN and y == -1
+    pub fn unchecked_sdiv<T>(x: T, y: T) -> T;
+    /// Performs an unchecked unsigned division, which results in undefined behavior,
+    /// in cases where y == 0
+    pub fn unchecked_udiv<T>(x: T, y: T) -> T;
+
+    /// Returns the remainder of an unchecked signed division, which results in
+    /// undefined behavior, in cases where y == 0, or x == int::MIN and y == -1
+    pub fn unchecked_urem<T>(x: T, y: T) -> T;
+    /// Returns the remainder of an unchecked signed division, which results in
+    /// undefined behavior, in cases where y == 0
+    pub fn unchecked_srem<T>(x: T, y: T) -> T;
+}
index 1506e5b266981fc5bd11097bad1f2769cc02ffd2..41ef566f2fd7f8e916c7af0039a62e28d91dc9c4 100644 (file)
@@ -735,6 +735,7 @@ pub fn check_drop_flag_for_sanity(&self) -> bool {
     }
 }
 
+/// Declare any llvm intrinsics that you might need
 fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
     macro_rules! ifn {
         ($name:expr, fn() -> $ret:expr) => (
index 6bfa80f9c40b360f5dd119494dc7987f60b30680..ed0beee20b04009cd3ed2932133ee44e0ec4b9e4 100644 (file)
@@ -144,6 +144,9 @@ pub fn check_intrinsics(ccx: &CrateContext) {
     ccx.sess().abort_if_errors();
 }
 
+/// Remember to add all intrinsics here, in librustc_typeck/check/mod.rs,
+/// and in libcore/intrinsics.rs; if you need access to any llvm intrinsics,
+/// add them to librustc_trans/trans/context.rs
 pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
                                             node: ast::NodeId,
                                             callee_ty: Ty<'tcx>,
@@ -676,6 +679,11 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
                                     llargs[1],
                                     call_debug_location),
 
+        (_, "unchecked_udiv") => UDiv(bcx, llargs[0], llargs[1], call_debug_location),
+        (_, "unchecked_sdiv") => SDiv(bcx, llargs[0], llargs[1], call_debug_location),
+        (_, "unchecked_urem") => URem(bcx, llargs[0], llargs[1], call_debug_location),
+        (_, "unchecked_srem") => SRem(bcx, llargs[0], llargs[1], call_debug_location),
+
         (_, "overflowing_add") => Add(bcx, llargs[0], llargs[1], call_debug_location),
         (_, "overflowing_sub") => Sub(bcx, llargs[0], llargs[1], call_debug_location),
         (_, "overflowing_mul") => Mul(bcx, llargs[0], llargs[1], call_debug_location),
index f9c78cd36e6ac1d93e3d3093337443363468201f..c285483214026b1135184f2bcf25dd2d26ef1967 100644 (file)
@@ -4882,6 +4882,8 @@ pub fn check_bounds_are_used<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
     }
 }
 
+/// Remember to add all intrinsics here, in librustc_trans/trans/intrinsic.rs,
+/// and in libcore/intrinsics.rs
 pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &ast::ForeignItem) {
     fn param<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, n: u32) -> Ty<'tcx> {
         let name = token::intern(&format!("P{}", n));
@@ -5119,6 +5121,9 @@ fn param<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, n: u32) -> Ty<'tcx> {
                 (0, vec!(tcx.types.u64, tcx.types.u64),
                 ty::mk_tup(tcx, vec!(tcx.types.u64, tcx.types.bool))),
 
+            "unchecked_udiv" | "unchecked_sdiv" | "unchecked_urem" | "unchecked_srem" =>
+                (1, vec![param(ccx, 0), param(ccx, 0)], param(ccx, 0)),
+
             "overflowing_add" | "overflowing_sub" | "overflowing_mul" =>
                 (1, vec![param(ccx, 0), param(ccx, 0)], param(ccx, 0)),