]> git.lizzy.rs Git - rust.git/commitdiff
Allow more types in CValue::const_val
authorbjorn3 <bjorn3@users.noreply.github.com>
Sun, 1 Dec 2019 16:35:40 +0000 (17:35 +0100)
committerbjorn3 <bjorn3@users.noreply.github.com>
Wed, 22 Jan 2020 17:56:36 +0000 (18:56 +0100)
src/value_and_place.rs

index 95913256f24ca16b69fb6a04653534ac3caa1261..f346ff8f05e0bd69365a90a788df7681d3e78c74 100644 (file)
@@ -195,13 +195,20 @@ pub fn unsize_value<'a>(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>, dest:
     }
 
     /// If `ty` is signed, `const_val` must already be sign extended.
-    pub fn const_val<'a>(
+    pub fn const_val(
         fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
         layout: TyLayout<'tcx>,
         const_val: u128,
     ) -> CValue<'tcx> {
         let clif_ty = fx.clif_type(layout.ty).unwrap();
 
+        match layout.ty.kind {
+            ty::TyKind::Bool => {
+                assert!(const_val == 0 || const_val == 1, "Invalid bool 0x{:032X}", const_val);
+            }
+            _ => {}
+        }
+
         let val = match layout.ty.kind {
             ty::TyKind::Uint(UintTy::U128) | ty::TyKind::Int(IntTy::I128) => {
                 let lsb = fx.bcx.ins().iconst(types::I64, const_val as u64 as i64);
@@ -211,21 +218,25 @@ pub fn const_val<'a>(
                     .iconst(types::I64, (const_val >> 64) as u64 as i64);
                 fx.bcx.ins().iconcat(lsb, msb)
             }
-            ty::TyKind::Bool => {
-                assert!(
-                    const_val == 0 || const_val == 1,
-                    "Invalid bool 0x{:032X}",
-                    const_val
-                );
-                fx.bcx.ins().iconst(types::I8, const_val as i64)
+            ty::TyKind::Bool | ty::TyKind::Char | ty::TyKind::Uint(_) | ty::TyKind::Ref(..)
+            | ty::TyKind::RawPtr(..) => {
+                fx
+                    .bcx
+                    .ins()
+                    .iconst(clif_ty, u64::try_from(const_val).expect("uint") as i64)
+            }
+            ty::TyKind::Int(_) => {
+                let const_val = rustc::mir::interpret::sign_extend(const_val, layout.size);
+                fx.bcx.ins().iconst(clif_ty, i64::try_from(const_val as i128).unwrap())
+            }
+            ty::TyKind::Float(FloatTy::F32) => {
+                fx.bcx.ins().f32const(Ieee32::with_bits(u32::try_from(const_val).unwrap()))
+            }
+            ty::TyKind::Float(FloatTy::F64) => {
+                fx.bcx.ins().f64const(Ieee64::with_bits(u64::try_from(const_val).unwrap()))
             }
-            ty::TyKind::Uint(_) | ty::TyKind::Ref(..) | ty::TyKind::RawPtr(..) => fx
-                .bcx
-                .ins()
-                .iconst(clif_ty, u64::try_from(const_val).expect("uint") as i64),
-            ty::TyKind::Int(_) => fx.bcx.ins().iconst(clif_ty, const_val as i128 as i64),
             _ => panic!(
-                "CValue::const_val for non bool/integer/pointer type {:?} is not allowed",
+                "CValue::const_val for non bool/char/float/integer/pointer type {:?} is not allowed",
                 layout.ty
             ),
         };