]> git.lizzy.rs Git - rust.git/blob - src/common.rs
Move CValue and CPlace to separate file and remove duplicate scalar_to_clif_type
[rust.git] / src / common.rs
1 use rustc_target::spec::{HasTargetSpec, Target};
2
3 use cranelift_module::Module;
4
5 use crate::prelude::*;
6
7 pub fn mir_var(loc: Local) -> Variable {
8     Variable::with_u32(loc.index() as u32)
9 }
10
11 pub fn pointer_ty(tcx: TyCtxt) -> types::Type {
12     match tcx.data_layout.pointer_size.bits() {
13         16 => types::I16,
14         32 => types::I32,
15         64 => types::I64,
16         bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits),
17     }
18 }
19
20 pub fn clif_type_from_ty<'a, 'tcx: 'a>(
21     tcx: TyCtxt<'a, 'tcx, 'tcx>,
22     ty: Ty<'tcx>,
23 ) -> Option<types::Type> {
24     Some(match ty.sty {
25         ty::Bool => types::I8,
26         ty::Uint(size) => match size {
27             UintTy::U8 => types::I8,
28             UintTy::U16 => types::I16,
29             UintTy::U32 => types::I32,
30             UintTy::U64 => types::I64,
31             UintTy::U128 => unimpl!("u128"),
32             UintTy::Usize => pointer_ty(tcx),
33         },
34         ty::Int(size) => match size {
35             IntTy::I8 => types::I8,
36             IntTy::I16 => types::I16,
37             IntTy::I32 => types::I32,
38             IntTy::I64 => types::I64,
39             IntTy::I128 => unimpl!("i128"),
40             IntTy::Isize => pointer_ty(tcx),
41         },
42         ty::Char => types::I32,
43         ty::Float(size) => match size {
44             FloatTy::F32 => types::F32,
45             FloatTy::F64 => types::F64,
46         },
47         ty::FnPtr(_) => pointer_ty(tcx),
48         ty::RawPtr(TypeAndMut { ty, mutbl: _ }) | ty::Ref(_, ty, _) => {
49             if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
50                 pointer_ty(tcx)
51             } else {
52                 return None;
53             }
54         }
55         ty::Param(_) => bug!("ty param {:?}", ty),
56         _ => return None,
57     })
58 }
59
60 pub fn codegen_select(bcx: &mut FunctionBuilder, cond: Value, lhs: Value, rhs: Value) -> Value {
61     let lhs_ty = bcx.func.dfg.value_type(lhs);
62     let rhs_ty = bcx.func.dfg.value_type(rhs);
63     assert_eq!(lhs_ty, rhs_ty);
64     if lhs_ty == types::I8 || lhs_ty == types::I16 {
65         // FIXME workaround for missing enocding for select.i8
66         let lhs = bcx.ins().uextend(types::I32, lhs);
67         let rhs = bcx.ins().uextend(types::I32, rhs);
68         let res = bcx.ins().select(cond, lhs, rhs);
69         bcx.ins().ireduce(lhs_ty, res)
70     } else {
71         bcx.ins().select(cond, lhs, rhs)
72     }
73 }
74
75 pub fn clif_intcast<'a, 'tcx: 'a>(
76     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
77     val: Value,
78     to: Type,
79     signed: bool,
80 ) -> Value {
81     let from = fx.bcx.func.dfg.value_type(val);
82     if from == to {
83         return val;
84     }
85     if to.wider_or_equal(from) {
86         if signed {
87             fx.bcx.ins().sextend(to, val)
88         } else {
89             fx.bcx.ins().uextend(to, val)
90         }
91     } else {
92         fx.bcx.ins().ireduce(to, val)
93     }
94 }
95
96 pub struct FunctionCx<'a, 'tcx: 'a, B: Backend> {
97     // FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
98     pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
99     pub module: &'a mut Module<B>,
100     pub pointer_type: Type, // Cached from module
101
102     pub instance: Instance<'tcx>,
103     pub mir: &'tcx Body<'tcx>,
104
105     pub bcx: FunctionBuilder<'a>,
106     pub ebb_map: HashMap<BasicBlock, Ebb>,
107     pub local_map: HashMap<Local, CPlace<'tcx>>,
108
109     pub clif_comments: crate::pretty_clif::CommentWriter,
110     pub constants: &'a mut crate::constant::ConstantCx,
111     pub caches: &'a mut Caches<'tcx>,
112     pub source_info_set: indexmap::IndexSet<SourceInfo>,
113 }
114
115 impl<'a, 'tcx: 'a, B: Backend> LayoutOf for FunctionCx<'a, 'tcx, B> {
116     type Ty = Ty<'tcx>;
117     type TyLayout = TyLayout<'tcx>;
118
119     fn layout_of(&self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
120         let ty = self.monomorphize(&ty);
121         self.tcx.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap()
122     }
123 }
124
125 impl<'a, 'tcx, B: Backend + 'a> layout::HasTyCtxt<'tcx> for FunctionCx<'a, 'tcx, B> {
126     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> {
127         self.tcx
128     }
129 }
130
131 impl<'a, 'tcx, B: Backend + 'a> layout::HasDataLayout for FunctionCx<'a, 'tcx, B> {
132     fn data_layout(&self) -> &layout::TargetDataLayout {
133         &self.tcx.data_layout
134     }
135 }
136
137 impl<'a, 'tcx, B: Backend + 'a> layout::HasParamEnv<'tcx> for FunctionCx<'a, 'tcx, B> {
138     fn param_env(&self) -> ParamEnv<'tcx> {
139         ParamEnv::reveal_all()
140     }
141 }
142
143 impl<'a, 'tcx, B: Backend + 'a> HasTargetSpec for FunctionCx<'a, 'tcx, B> {
144     fn target_spec(&self) -> &Target {
145         &self.tcx.sess.target.target
146     }
147 }
148
149 impl<'a, 'tcx, B: Backend> BackendTypes for FunctionCx<'a, 'tcx, B> {
150     type Value = Value;
151     type BasicBlock = Ebb;
152     type Type = Type;
153     type Funclet = !;
154     type DIScope = !;
155 }
156
157 impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
158     pub fn monomorphize<T>(&self, value: &T) -> T
159     where
160         T: TypeFoldable<'tcx>,
161     {
162         self.tcx.subst_and_normalize_erasing_regions(
163             self.instance.substs,
164             ty::ParamEnv::reveal_all(),
165             value,
166         )
167     }
168
169     pub fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {
170         clif_type_from_ty(self.tcx, self.monomorphize(&ty))
171     }
172
173     pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
174         *self.ebb_map.get(&bb).unwrap()
175     }
176
177     pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
178         *self.local_map.get(&local).unwrap()
179     }
180
181     pub fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
182         let (index, _) = self.source_info_set.insert_full(source_info);
183         self.bcx.set_srcloc(SourceLoc::new(index as u32));
184     }
185 }