about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOliver Schneider <github35764891676564198441@oli-obk.de>2018-05-23 19:39:49 +0200
committerOliver Schneider <github35764891676564198441@oli-obk.de>2018-05-24 20:49:37 +0200
commitc420531304c1f02561bae5498d80447097ad5e75 (patch)
tree67f96f5afa39db64b82f90b62702e60151822a72
parentcc60a22b1034140505866bbf757095a23b631327 (diff)
downloadrust-c420531304c1f02561bae5498d80447097ad5e75.tar.gz
rust-c420531304c1f02561bae5498d80447097ad5e75.zip
Replace `ScalarKind` with `Primitive`
-rw-r--r--src/librustc/mir/interpret/mod.rs2
-rw-r--r--src/librustc/mir/interpret/value.rs66
-rw-r--r--src/librustc/ty/layout.rs12
-rw-r--r--src/librustc_codegen_llvm/type_of.rs5
-rw-r--r--src/librustc_mir/interpret/eval_context.rs81
-rw-r--r--src/librustc_mir/interpret/operator.rs4
-rw-r--r--src/librustc_target/abi/call/mips64.rs6
-rw-r--r--src/librustc_target/abi/call/mod.rs3
-rw-r--r--src/librustc_target/abi/call/s390x.rs7
-rw-r--r--src/librustc_target/abi/call/x86.rs7
-rw-r--r--src/librustc_target/abi/call/x86_64.rs3
-rw-r--r--src/librustc_target/abi/mod.rs63
-rw-r--r--src/libsyntax/ast.rs37
13 files changed, 83 insertions, 213 deletions
diff --git a/src/librustc/mir/interpret/mod.rs b/src/librustc/mir/interpret/mod.rs
index a44729f2c59..b41652469ae 100644
--- a/src/librustc/mir/interpret/mod.rs
+++ b/src/librustc/mir/interpret/mod.rs
@@ -10,7 +10,7 @@ mod value;
 
 pub use self::error::{EvalError, EvalResult, EvalErrorKind, AssertMessage};
 
-pub use self::value::{Scalar, ScalarKind, Value, ConstValue};
+pub use self::value::{Scalar, Value, ConstValue};
 
 use std::fmt;
 use mir;
diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs
index 2bda474a0a6..6f82b38e2eb 100644
--- a/src/librustc/mir/interpret/value.rs
+++ b/src/librustc/mir/interpret/value.rs
@@ -202,16 +202,6 @@ pub enum Scalar {
     Ptr(Pointer),
 }
 
-#[derive(Clone, Copy, Debug, PartialEq)]
-pub enum ScalarKind {
-    I8, I16, I32, I64, I128,
-    U8, U16, U32, U64, U128,
-    F32, F64,
-    Ptr, FnPtr,
-    Bool,
-    Char,
-}
-
 impl<'tcx> Scalar {
     pub fn undef() -> Self {
         Scalar::Bits { bits: 0, defined: 0 }
@@ -264,59 +254,3 @@ impl<'tcx> Scalar {
         }
     }
 }
-
-impl ScalarKind {
-    pub fn is_int(self) -> bool {
-        use self::ScalarKind::*;
-        match self {
-            I8 | I16 | I32 | I64 | I128 | U8 | U16 | U32 | U64 | U128 => true,
-            _ => false,
-        }
-    }
-
-    pub fn is_signed_int(self) -> bool {
-        use self::ScalarKind::*;
-        match self {
-            I8 | I16 | I32 | I64 | I128 => true,
-            _ => false,
-        }
-    }
-
-    pub fn is_float(self) -> bool {
-        use self::ScalarKind::*;
-        match self {
-            F32 | F64 => true,
-            _ => false,
-        }
-    }
-
-    pub fn from_uint_size(size: Size) -> Self {
-        match size.bytes() {
-            1 => ScalarKind::U8,
-            2 => ScalarKind::U16,
-            4 => ScalarKind::U32,
-            8 => ScalarKind::U64,
-            16 => ScalarKind::U128,
-            _ => bug!("can't make uint with size {}", size.bytes()),
-        }
-    }
-
-    pub fn from_int_size(size: Size) -> Self {
-        match size.bytes() {
-            1 => ScalarKind::I8,
-            2 => ScalarKind::I16,
-            4 => ScalarKind::I32,
-            8 => ScalarKind::I64,
-            16 => ScalarKind::I128,
-            _ => bug!("can't make int with size {}", size.bytes()),
-        }
-    }
-
-    pub fn is_ptr(self) -> bool {
-        use self::ScalarKind::*;
-        match self {
-            Ptr | FnPtr => true,
-            _ => false,
-        }
-    }
-}
diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs
index b3598bcdc04..98df4b150fa 100644
--- a/src/librustc/ty/layout.rs
+++ b/src/librustc/ty/layout.rs
@@ -11,7 +11,7 @@
 use session::{self, DataTypeKind};
 use ty::{self, Ty, TyCtxt, TypeFoldable, ReprOptions};
 
-use syntax::ast::{self, FloatTy, IntTy, UintTy};
+use syntax::ast::{self, IntTy, UintTy};
 use syntax::attr;
 use syntax_pos::DUMMY_SP;
 
@@ -130,8 +130,8 @@ impl PrimitiveExt for Primitive {
     fn to_ty<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
         match *self {
             Int(i, signed) => i.to_ty(tcx, signed),
-            F32 => tcx.types.f32,
-            F64 => tcx.types.f64,
+            Float(FloatTy::F32) => tcx.types.f32,
+            Float(FloatTy::F64) => tcx.types.f64,
             Pointer => tcx.mk_mut_ptr(tcx.mk_nil()),
         }
     }
@@ -488,8 +488,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
             ty::TyUint(ity) => {
                 scalar(Int(Integer::from_attr(dl, attr::UnsignedInt(ity)), false))
             }
-            ty::TyFloat(FloatTy::F32) => scalar(F32),
-            ty::TyFloat(FloatTy::F64) => scalar(F64),
+            ty::TyFloat(fty) => scalar(Float(fty)),
             ty::TyFnPtr(_) => {
                 let mut ptr = scalar_unit(Pointer);
                 ptr.valid_range = 1..=*ptr.valid_range.end();
@@ -1908,8 +1907,7 @@ impl_stable_hash_for!(enum ::ty::layout::Integer {
 
 impl_stable_hash_for!(enum ::ty::layout::Primitive {
     Int(integer, signed),
-    F32,
-    F64,
+    Float(fty),
     Pointer
 });
 
diff --git a/src/librustc_codegen_llvm/type_of.rs b/src/librustc_codegen_llvm/type_of.rs
index c993d7db107..88b75ff9c09 100644
--- a/src/librustc_codegen_llvm/type_of.rs
+++ b/src/librustc_codegen_llvm/type_of.rs
@@ -15,6 +15,7 @@ use rustc::hir;
 use rustc::ty::{self, Ty, TypeFoldable};
 use rustc::ty::layout::{self, Align, LayoutOf, Size, TyLayout};
 use rustc_target::spec::PanicStrategy;
+use rustc_target::abi::FloatTy;
 use mono_item::DefPathBasedNames;
 use type_::Type;
 
@@ -324,8 +325,8 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> {
                                scalar: &layout::Scalar, offset: Size) -> Type {
         match scalar.value {
             layout::Int(i, _) => Type::from_integer(cx, i),
-            layout::F32 => Type::f32(cx),
-            layout::F64 => Type::f64(cx),
+            layout::Float(FloatTy::F32) => Type::f32(cx),
+            layout::Float(FloatTy::F64) => Type::f64(cx),
             layout::Pointer => {
                 // If we know the alignment, pick something better than i8.
                 let pointee = if let Some(pointee) = self.pointee_info_at(cx, offset) {
diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs
index 01170f07636..5949df697d7 100644
--- a/src/librustc_mir/interpret/eval_context.rs
+++ b/src/librustc_mir/interpret/eval_context.rs
@@ -14,7 +14,7 @@ use rustc::middle::const_val::FrameInfo;
 use syntax::codemap::{self, Span};
 use syntax::ast::Mutability;
 use rustc::mir::interpret::{
-    GlobalId, Value, Scalar, ScalarKind,
+    GlobalId, Value, Scalar,
     EvalError, EvalResult, EvalErrorKind, Pointer, ConstValue,
 };
 use std::mem;
@@ -230,13 +230,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
 
     pub fn str_to_value(&mut self, s: &str) -> EvalResult<'tcx, Value> {
         let ptr = self.memory.allocate_bytes(s.as_bytes());
-        Ok(Value::ScalarPair(
-            Scalar::Ptr(ptr),
-            Scalar::Bits {
-                bits: s.len() as u128,
-                defined: self.tcx.data_layout.pointer_size.bits() as u8,
-            },
-        ))
+        Ok(Scalar::Ptr(ptr).to_value_with_len(s.len() as u64, self.tcx.tcx))
     }
 
     pub fn const_value_to_value(
@@ -1271,72 +1265,11 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
         }
     }
 
-    pub fn ty_to_scalar_kind(&self, ty: Ty<'tcx>) -> EvalResult<'tcx, ScalarKind> {
-        use syntax::ast::FloatTy;
-
-        let kind = match ty.sty {
-            ty::TyBool => ScalarKind::Bool,
-            ty::TyChar => ScalarKind::Char,
-
-            ty::TyInt(int_ty) => {
-                use syntax::ast::IntTy::*;
-                let size = match int_ty {
-                    I8 => Size::from_bytes(1),
-                    I16 => Size::from_bytes(2),
-                    I32 => Size::from_bytes(4),
-                    I64 => Size::from_bytes(8),
-                    I128 => Size::from_bytes(16),
-                    Isize => self.memory.pointer_size(),
-                };
-                ScalarKind::from_int_size(size)
-            }
-
-            ty::TyUint(uint_ty) => {
-                use syntax::ast::UintTy::*;
-                let size = match uint_ty {
-                    U8 => Size::from_bytes(1),
-                    U16 => Size::from_bytes(2),
-                    U32 => Size::from_bytes(4),
-                    U64 => Size::from_bytes(8),
-                    U128 => Size::from_bytes(16),
-                    Usize => self.memory.pointer_size(),
-                };
-                ScalarKind::from_uint_size(size)
-            }
-
-            ty::TyFloat(FloatTy::F32) => ScalarKind::F32,
-            ty::TyFloat(FloatTy::F64) => ScalarKind::F64,
-
-            ty::TyFnPtr(_) => ScalarKind::FnPtr,
-
-            ty::TyRef(_, ty, _) |
-            ty::TyRawPtr(ty::TypeAndMut { ty, .. }) if self.type_is_sized(ty) => {
-                ScalarKind::Ptr
-            }
-
-            ty::TyAdt(def, _) if def.is_box() => ScalarKind::Ptr,
-
-            ty::TyAdt(..) => {
-                match self.layout_of(ty)?.abi {
-                    layout::Abi::Scalar(ref scalar) => {
-                        use rustc::ty::layout::Primitive::*;
-                        match scalar.value {
-                            Int(i, false) => ScalarKind::from_uint_size(i.size()),
-                            Int(i, true) => ScalarKind::from_int_size(i.size()),
-                            F32 => ScalarKind::F32,
-                            F64 => ScalarKind::F64,
-                            Pointer => ScalarKind::Ptr,
-                        }
-                    }
-
-                    _ => return err!(TypeNotPrimitive(ty)),
-                }
-            }
-
-            _ => return err!(TypeNotPrimitive(ty)),
-        };
-
-        Ok(kind)
+    pub fn ty_to_primitive(&self, ty: Ty<'tcx>) -> EvalResult<'tcx, layout::Primitive> {
+        match self.layout_of(ty)?.abi {
+            layout::Abi::Scalar(ref scalar) => Ok(scalar.value),
+            _ => err!(TypeNotPrimitive(ty)),
+        }
     }
 
     fn ensure_valid_value(&self, val: Scalar, ty: Ty<'tcx>) -> EvalResult<'tcx> {
diff --git a/src/librustc_mir/interpret/operator.rs b/src/librustc_mir/interpret/operator.rs
index c8af638a54d..a257117de1d 100644
--- a/src/librustc_mir/interpret/operator.rs
+++ b/src/librustc_mir/interpret/operator.rs
@@ -68,8 +68,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
     ) -> EvalResult<'tcx, (Scalar, bool)> {
         use rustc::mir::BinOp::*;
 
-        let left_kind = self.ty_to_scalar_kind(left_ty)?;
-        let right_kind = self.ty_to_scalar_kind(right_ty)?;
+        let left_kind = self.ty_to_primitive(left_ty)?;
+        let right_kind = self.ty_to_primitive(right_ty)?;
         trace!("Running binary op {:?}: {:?} ({:?}), {:?} ({:?})", bin_op, left, left_kind, right, right_kind);
 
         // I: Handle operations that support pointers
diff --git a/src/librustc_target/abi/call/mips64.rs b/src/librustc_target/abi/call/mips64.rs
index 93fe9cb723c..3734e563d58 100644
--- a/src/librustc_target/abi/call/mips64.rs
+++ b/src/librustc_target/abi/call/mips64.rs
@@ -33,8 +33,8 @@ fn float_reg<'a, Ty, C>(cx: C, ret: &ArgType<'a, Ty>, i: usize) -> Option<Reg>
 {
     match ret.layout.field(cx, i).abi {
         abi::Abi::Scalar(ref scalar) => match scalar.value {
-            abi::F32 => Some(Reg::f32()),
-            abi::F64 => Some(Reg::f64()),
+            abi::Float(abi::FloatTy::F32) => Some(Reg::f32()),
+            abi::Float(abi::FloatTy::F64) => Some(Reg::f64()),
             _ => None
         },
         _ => None
@@ -117,7 +117,7 @@ fn classify_arg_ty<'a, Ty, C>(cx: C, arg: &mut ArgType<'a, Ty>)
 
                 // We only care about aligned doubles
                 if let abi::Abi::Scalar(ref scalar) = field.abi {
-                    if let abi::F64 = scalar.value {
+                    if let abi::Float(abi::FloatTy::F64) = scalar.value {
                         if offset.is_abi_aligned(dl.f64_align) {
                             // Insert enough integers to cover [last_offset, offset)
                             assert!(last_offset.is_abi_aligned(dl.f64_align));
diff --git a/src/librustc_target/abi/call/mod.rs b/src/librustc_target/abi/call/mod.rs
index d5f2501efc5..fcae9ea22bb 100644
--- a/src/librustc_target/abi/call/mod.rs
+++ b/src/librustc_target/abi/call/mod.rs
@@ -256,8 +256,7 @@ impl<'a, Ty> TyLayout<'a, Ty> {
                 let kind = match scalar.value {
                     abi::Int(..) |
                     abi::Pointer => RegKind::Integer,
-                    abi::F32 |
-                    abi::F64 => RegKind::Float
+                    abi::Float(_) => RegKind::Float,
                 };
                 Some(Reg {
                     kind,
diff --git a/src/librustc_target/abi/call/s390x.rs b/src/librustc_target/abi/call/s390x.rs
index 3002a3c46c8..37be6ea41c6 100644
--- a/src/librustc_target/abi/call/s390x.rs
+++ b/src/librustc_target/abi/call/s390x.rs
@@ -29,12 +29,7 @@ fn is_single_fp_element<'a, Ty, C>(cx: C, layout: TyLayout<'a, Ty>) -> bool
           C: LayoutOf<Ty = Ty, TyLayout = TyLayout<'a, Ty>> + HasDataLayout
 {
     match layout.abi {
-        abi::Abi::Scalar(ref scalar) => {
-            match scalar.value {
-                abi::F32 | abi::F64 => true,
-                _ => false
-            }
-        }
+        abi::Abi::Scalar(ref scalar) => scalar.value.is_float(),
         abi::Abi::Aggregate { .. } => {
             if layout.fields.count() == 1 && layout.fields.offset(0).bytes() == 0 {
                 is_single_fp_element(cx, layout.field(cx, 0))
diff --git a/src/librustc_target/abi/call/x86.rs b/src/librustc_target/abi/call/x86.rs
index e803b96b21c..0c0040de9df 100644
--- a/src/librustc_target/abi/call/x86.rs
+++ b/src/librustc_target/abi/call/x86.rs
@@ -23,12 +23,7 @@ fn is_single_fp_element<'a, Ty, C>(cx: C, layout: TyLayout<'a, Ty>) -> bool
           C: LayoutOf<Ty = Ty, TyLayout = TyLayout<'a, Ty>> + HasDataLayout
 {
     match layout.abi {
-        abi::Abi::Scalar(ref scalar) => {
-            match scalar.value {
-                abi::F32 | abi::F64 => true,
-                _ => false
-            }
-        }
+        abi::Abi::Scalar(ref scalar) => scalar.value.is_float(),
         abi::Abi::Aggregate { .. } => {
             if layout.fields.count() == 1 && layout.fields.offset(0).bytes() == 0 {
                 is_single_fp_element(cx, layout.field(cx, 0))
diff --git a/src/librustc_target/abi/call/x86_64.rs b/src/librustc_target/abi/call/x86_64.rs
index e7d44020f50..a443255b970 100644
--- a/src/librustc_target/abi/call/x86_64.rs
+++ b/src/librustc_target/abi/call/x86_64.rs
@@ -55,8 +55,7 @@ fn classify_arg<'a, Ty, C>(cx: C, arg: &ArgType<'a, Ty>)
                 match scalar.value {
                     abi::Int(..) |
                     abi::Pointer => Class::Int,
-                    abi::F32 |
-                    abi::F64 => Class::Sse
+                    abi::Float(_) => Class::Sse
                 }
             }
 
diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs
index 64fca7e0880..7ff04df6233 100644
--- a/src/librustc_target/abi/mod.rs
+++ b/src/librustc_target/abi/mod.rs
@@ -13,7 +13,7 @@ pub use self::Primitive::*;
 
 use spec::Target;
 
-use std::cmp;
+use std::{cmp, fmt};
 use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive};
 
 pub mod call;
@@ -488,6 +488,42 @@ impl Integer {
     }
 }
 
+
+#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy,
+         PartialOrd, Ord)]
+pub enum FloatTy {
+    F32,
+    F64,
+}
+
+impl fmt::Debug for FloatTy {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        fmt::Display::fmt(self, f)
+    }
+}
+
+impl fmt::Display for FloatTy {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "{}", self.ty_to_string())
+    }
+}
+
+impl FloatTy {
+    pub fn ty_to_string(&self) -> &'static str {
+        match *self {
+            FloatTy::F32 => "f32",
+            FloatTy::F64 => "f64",
+        }
+    }
+
+    pub fn bit_width(&self) -> usize {
+        match *self {
+            FloatTy::F32 => 32,
+            FloatTy::F64 => 64,
+        }
+    }
+}
+
 /// Fundamental unit of memory access and layout.
 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
 pub enum Primitive {
@@ -499,8 +535,7 @@ pub enum Primitive {
     /// a negative integer passed by zero-extension will appear positive in
     /// the callee, and most operations on it will produce the wrong values.
     Int(Integer, bool),
-    F32,
-    F64,
+    Float(FloatTy),
     Pointer
 }
 
@@ -510,8 +545,8 @@ impl<'a, 'tcx> Primitive {
 
         match self {
             Int(i, _) => i.size(),
-            F32 => Size::from_bits(32),
-            F64 => Size::from_bits(64),
+            Float(FloatTy::F32) => Size::from_bits(32),
+            Float(FloatTy::F64) => Size::from_bits(64),
             Pointer => dl.pointer_size
         }
     }
@@ -521,11 +556,25 @@ impl<'a, 'tcx> Primitive {
 
         match self {
             Int(i, _) => i.align(dl),
-            F32 => dl.f32_align,
-            F64 => dl.f64_align,
+            Float(FloatTy::F32) => dl.f32_align,
+            Float(FloatTy::F64) => dl.f64_align,
             Pointer => dl.pointer_align
         }
     }
+
+    pub fn is_float(self) -> bool {
+        match self {
+            Float(_) => true,
+            _ => false
+        }
+    }
+
+    pub fn is_int(self) -> bool {
+        match self {
+            Int(..) => true,
+            _ => false,
+        }
+    }
 }
 
 /// Information about one scalar component of a Rust type.
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index 6ae1729295f..c9843de547f 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -33,6 +33,8 @@ use std::fmt;
 use rustc_data_structures::sync::Lrc;
 use std::u32;
 
+pub use rustc_target::abi::FloatTy;
+
 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
 pub struct Label {
     pub ident: Ident,
@@ -1519,41 +1521,6 @@ impl fmt::Display for UintTy {
     }
 }
 
-#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy,
-         PartialOrd, Ord)]
-pub enum FloatTy {
-    F32,
-    F64,
-}
-
-impl fmt::Debug for FloatTy {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        fmt::Display::fmt(self, f)
-    }
-}
-
-impl fmt::Display for FloatTy {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "{}", self.ty_to_string())
-    }
-}
-
-impl FloatTy {
-    pub fn ty_to_string(&self) -> &'static str {
-        match *self {
-            FloatTy::F32 => "f32",
-            FloatTy::F64 => "f64",
-        }
-    }
-
-    pub fn bit_width(&self) -> usize {
-        match *self {
-            FloatTy::F32 => 32,
-            FloatTy::F64 => 64,
-        }
-    }
-}
-
 // Bind a type to an associated type: `A=Foo`.
 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
 pub struct TypeBinding {