]> git.lizzy.rs Git - rust.git/commitdiff
Move const val handling to constant.rs
authorbjorn3 <bjorn3@users.noreply.github.com>
Sat, 14 Jul 2018 14:45:20 +0000 (16:45 +0200)
committerbjorn3 <bjorn3@users.noreply.github.com>
Sat, 14 Jul 2018 14:45:20 +0000 (16:45 +0200)
src/base.rs
src/constant.rs [new file with mode: 0644]
src/lib.rs

index c523c3979213b47c9a0699e12d06a81f8532e567..c7f07542b0e84c3c551275426a53b701bcb0e35a 100644 (file)
@@ -497,8 +497,6 @@ fn trans_place<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, place: &Place<'tcx>)
 }
 
 fn trans_operand<'a, 'tcx>(fx: &mut FunctionCx<'a, 'tcx>, operand: &Operand<'tcx>) -> CValue<'tcx> {
-    use rustc::mir::interpret::{Scalar, ConstValue, GlobalId};
-
     match operand {
         Operand::Move(place) |
         Operand::Copy(place) => {
@@ -506,37 +504,7 @@ fn trans_operand<'a, 'tcx>(fx: &mut FunctionCx<'a, 'tcx>, operand: &Operand<'tcx
             cplace.to_cvalue(fx)
         },
         Operand::Constant(const_) => {
-            let value = match const_.literal {
-                Literal::Value { value } => value,
-                Literal::Promoted { index } => fx
-                    .tcx
-                    .const_eval(ParamEnv::reveal_all().and(GlobalId {
-                        instance: fx.instance,
-                        promoted: Some(index),
-                    }))
-                    .unwrap(),
-            };
-
-            let layout = fx.layout_of(const_.ty);
-            match const_.ty.sty {
-                TypeVariants::TyBool => {
-                    let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
-                    CValue::const_val(fx, const_.ty, bits as u64 as i64)
-                }
-                TypeVariants::TyUint(_) => {
-                    let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
-                    CValue::const_val(fx, const_.ty, bits as u64 as i64)
-                }
-                TypeVariants::TyInt(_) => {
-                    let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
-                    CValue::const_val(fx, const_.ty, bits as i128 as i64)
-                }
-                TypeVariants::TyFnDef(def_id, substs) => {
-                    let func_ref = fx.get_function_ref(Instance::new(def_id, substs));
-                    CValue::Func(func_ref, fx.layout_of(const_.ty))
-                }
-                _ => unimplemented!("value {:?} ty {:?}", value, const_.ty),
-            }
+            ::constant::trans_constant(fx, const_)
         }
     }
 }
diff --git a/src/constant.rs b/src/constant.rs
new file mode 100644 (file)
index 0000000..ab3e437
--- /dev/null
@@ -0,0 +1,37 @@
+use prelude::*;
+
+pub fn trans_constant<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &Constant<'tcx>) -> CValue<'tcx> {
+    use rustc::mir::interpret::{Scalar, ConstValue, GlobalId};
+
+    let value = match const_.literal {
+        Literal::Value { value } => value,
+        Literal::Promoted { index } => fx
+            .tcx
+            .const_eval(ParamEnv::reveal_all().and(GlobalId {
+                instance: fx.instance,
+                promoted: Some(index),
+            }))
+            .unwrap(),
+    };
+
+    let layout = fx.layout_of(const_.ty);
+    match const_.ty.sty {
+        TypeVariants::TyBool => {
+            let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
+            CValue::const_val(fx, const_.ty, bits as u64 as i64)
+        }
+        TypeVariants::TyUint(_) => {
+            let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
+            CValue::const_val(fx, const_.ty, bits as u64 as i64)
+        }
+        TypeVariants::TyInt(_) => {
+            let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
+            CValue::const_val(fx, const_.ty, bits as i128 as i64)
+        }
+        TypeVariants::TyFnDef(def_id, substs) => {
+            let func_ref = fx.get_function_ref(Instance::new(def_id, substs));
+            CValue::Func(func_ref, fx.layout_of(const_.ty))
+        }
+        _ => unimplemented!("value {:?} ty {:?}", value, const_.ty),
+    }
+}
\ No newline at end of file
index 40ffaa5e0a0f41a449e9053ba5f9a877c5386383..e8cab41d81074e38ad5797c21d00c567f94b20c9 100644 (file)
@@ -34,6 +34,7 @@
 use std::io::Write;
 
 mod base;
+mod constant;
 mod common;
 mod pretty_clif;