]> git.lizzy.rs Git - rust.git/blob - src/common.rs
Remove CValue::Func
[rust.git] / src / common.rs
1 use std::fmt;
2
3 use rustc_target::spec::{HasTargetSpec, Target};
4
5 use cranelift_module::Module;
6
7 use crate::prelude::*;
8
9 pub fn mir_var(loc: Local) -> Variable {
10     Variable::with_u32(loc.index() as u32)
11 }
12
13 pub fn cton_type_from_ty<'a, 'tcx: 'a>(
14     tcx: TyCtxt<'a, 'tcx, 'tcx>,
15     ty: Ty<'tcx>,
16 ) -> Option<types::Type> {
17     Some(match ty.sty {
18         TypeVariants::TyBool => types::I8,
19         TypeVariants::TyUint(size) => match size {
20             UintTy::U8 => types::I8,
21             UintTy::U16 => types::I16,
22             UintTy::U32 => types::I32,
23             UintTy::U64 => types::I64,
24             UintTy::U128 => unimpl!("u128"),
25             UintTy::Usize => types::I64,
26         },
27         TypeVariants::TyInt(size) => match size {
28             IntTy::I8 => types::I8,
29             IntTy::I16 => types::I16,
30             IntTy::I32 => types::I32,
31             IntTy::I64 => types::I64,
32             IntTy::I128 => unimpl!("i128"),
33             IntTy::Isize => types::I64,
34         },
35         TypeVariants::TyChar => types::I32,
36         TypeVariants::TyFloat(size) => match size {
37             FloatTy::F32 => types::F32,
38             FloatTy::F64 => types::F64,
39         },
40         TypeVariants::TyFnPtr(_) => types::I64,
41         TypeVariants::TyRawPtr(TypeAndMut { ty, mutbl: _ }) | TypeVariants::TyRef(_, ty, _) => {
42             if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
43                 types::I64
44             } else {
45                 return None;
46             }
47         }
48         TypeVariants::TyParam(_) => bug!("{:?}: {:?}", ty, ty.sty),
49         _ => return None,
50     })
51 }
52
53 fn codegen_field<'a, 'tcx: 'a>(
54     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
55     base: Value,
56     layout: TyLayout<'tcx>,
57     field: mir::Field,
58 ) -> (Value, TyLayout<'tcx>) {
59     let field_offset = layout.fields.offset(field.index());
60     let field_ty = layout.field(&*fx, field.index());
61     if field_offset.bytes() > 0 {
62         let field_offset = fx.bcx.ins().iconst(types::I64, field_offset.bytes() as i64);
63         (fx.bcx.ins().iadd(base, field_offset), field_ty)
64     } else {
65         (base, field_ty)
66     }
67 }
68
69 /// A read-only value
70 #[derive(Debug, Copy, Clone)]
71 pub enum CValue<'tcx> {
72     ByRef(Value, TyLayout<'tcx>),
73     ByVal(Value, TyLayout<'tcx>),
74 }
75
76 impl<'tcx> CValue<'tcx> {
77     pub fn layout(&self) -> TyLayout<'tcx> {
78         match *self {
79             CValue::ByRef(_, layout) | CValue::ByVal(_, layout) => layout,
80         }
81     }
82
83     pub fn force_stack<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value
84     where
85         'tcx: 'a,
86     {
87         match self {
88             CValue::ByRef(value, _layout) => value,
89             CValue::ByVal(value, layout) => {
90                 let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
91                     kind: StackSlotKind::ExplicitSlot,
92                     size: layout.size.bytes() as u32,
93                     offset: None,
94                 });
95                 fx.bcx.ins().stack_store(value, stack_slot, 0);
96                 fx.bcx.ins().stack_addr(types::I64, stack_slot, 0)
97             }
98         }
99     }
100
101     pub fn load_value<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value
102     where
103         'tcx: 'a,
104     {
105         match self {
106             CValue::ByRef(addr, layout) => {
107                 let cton_ty = fx
108                     .cton_type(layout.ty)
109                     .expect(&format!("load_value of type {:?}", layout.ty));
110                 fx.bcx.ins().load(cton_ty, MemFlags::new(), addr, 0)
111             }
112             CValue::ByVal(value, _layout) => value,
113         }
114     }
115
116     pub fn expect_byref(self) -> (Value, TyLayout<'tcx>) {
117         match self {
118             CValue::ByRef(value, layout) => (value, layout),
119             CValue::ByVal(_, _) => bug!("Expected CValue::ByRef, found CValue::ByVal: {:?}", self),
120         }
121     }
122
123     pub fn value_field<'a>(
124         self,
125         fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
126         field: mir::Field,
127     ) -> CValue<'tcx>
128     where
129         'tcx: 'a,
130     {
131         let (base, layout) = match self {
132             CValue::ByRef(addr, layout) => (addr, layout),
133             _ => bug!("place_field for {:?}", self),
134         };
135
136         let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
137         CValue::ByRef(field_ptr, field_layout)
138     }
139
140     pub fn const_val<'a>(
141         fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
142         ty: Ty<'tcx>,
143         const_val: i64,
144     ) -> CValue<'tcx>
145     where
146         'tcx: 'a,
147     {
148         let cton_ty = fx.cton_type(ty).unwrap();
149         let layout = fx.layout_of(ty);
150         CValue::ByVal(fx.bcx.ins().iconst(cton_ty, const_val), layout)
151     }
152
153     pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
154         match self {
155             CValue::ByRef(addr, _) => CValue::ByRef(addr, layout),
156             CValue::ByVal(val, _) => CValue::ByVal(val, layout),
157         }
158     }
159 }
160
161 /// A place where you can write a value to or read a value from
162 #[derive(Debug, Copy, Clone)]
163 pub enum CPlace<'tcx> {
164     Var(Local, TyLayout<'tcx>),
165     Addr(Value, TyLayout<'tcx>),
166 }
167
168 impl<'a, 'tcx: 'a> CPlace<'tcx> {
169     pub fn layout(&self) -> TyLayout<'tcx> {
170         match *self {
171             CPlace::Var(_, layout) | CPlace::Addr(_, layout) => layout,
172         }
173     }
174
175     pub fn temp(fx: &mut FunctionCx<'a, 'tcx, impl Backend>, ty: Ty<'tcx>) -> CPlace<'tcx> {
176         let layout = fx.layout_of(ty);
177         let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
178             kind: StackSlotKind::ExplicitSlot,
179             size: layout.size.bytes() as u32,
180             offset: None,
181         });
182         CPlace::Addr(fx.bcx.ins().stack_addr(types::I64, stack_slot, 0), layout)
183     }
184
185     pub fn from_stack_slot(
186         fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
187         stack_slot: StackSlot,
188         ty: Ty<'tcx>,
189     ) -> CPlace<'tcx> {
190         let layout = fx.layout_of(ty);
191         CPlace::Addr(fx.bcx.ins().stack_addr(types::I64, stack_slot, 0), layout)
192     }
193
194     pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CValue<'tcx> {
195         match self {
196             CPlace::Var(var, layout) => CValue::ByVal(fx.bcx.use_var(mir_var(var)), layout),
197             CPlace::Addr(addr, layout) => CValue::ByRef(addr, layout),
198         }
199     }
200
201     pub fn expect_addr(self) -> Value {
202         match self {
203             CPlace::Addr(addr, _layout) => addr,
204             CPlace::Var(_, _) => bug!("Expected CPlace::Addr, found CPlace::Var"),
205         }
206     }
207
208     pub fn write_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>, from: CValue<'tcx>) {
209         match (&self.layout().ty.sty, &from.layout().ty.sty) {
210             (TypeVariants::TyRef(_, t, dest_mut), TypeVariants::TyRef(_, u, src_mut))
211                 if (if *dest_mut != ::rustc::hir::Mutability::MutImmutable && src_mut != dest_mut {
212                     false
213                 } else if t != u {
214                     false
215                 } else {
216                     true
217                 }) =>
218             {
219                 // &mut T -> &T is allowed
220                 // &'a T -> &'b T is allowed
221             }
222             _ => {
223                 assert_eq!(
224                     self.layout().ty,
225                     from.layout().ty,
226                     "Can't write value of incompatible type to place {:?} {:?}\n\n{:#?}",
227                     self.layout().ty.sty,
228                     from.layout().ty.sty,
229                     fx,
230                 );
231             }
232         }
233
234         match self {
235             CPlace::Var(var, _) => {
236                 let data = from.load_value(fx);
237                 fx.bcx.def_var(mir_var(var), data)
238             }
239             CPlace::Addr(addr, layout) => {
240                 let size = layout.size.bytes() as i32;
241
242                 if let Some(_) = fx.cton_type(layout.ty) {
243                     let data = from.load_value(fx);
244                     fx.bcx.ins().store(MemFlags::new(), data, addr, 0);
245                 } else {
246                     let from = from.expect_byref();
247                     let mut offset = 0;
248                     while size - offset >= 8 {
249                         let byte = fx
250                             .bcx
251                             .ins()
252                             .load(types::I64, MemFlags::new(), from.0, offset);
253                         fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
254                         offset += 8;
255                     }
256                     while size - offset >= 4 {
257                         let byte = fx
258                             .bcx
259                             .ins()
260                             .load(types::I32, MemFlags::new(), from.0, offset);
261                         fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
262                         offset += 4;
263                     }
264                     while offset < size {
265                         let byte = fx
266                             .bcx
267                             .ins()
268                             .load(types::I8, MemFlags::new(), from.0, offset);
269                         fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
270                         offset += 1;
271                     }
272                 }
273             }
274         }
275     }
276
277     pub fn place_field(
278         self,
279         fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
280         field: mir::Field,
281     ) -> CPlace<'tcx> {
282         let base = self.expect_addr();
283         let layout = self.layout();
284
285         let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
286         CPlace::Addr(field_ptr, field_layout)
287     }
288
289     pub fn place_index(
290         self,
291         fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
292         index: Value,
293     ) -> CPlace<'tcx> {
294         let addr = self.expect_addr();
295         let layout = self.layout();
296         match layout.ty.sty {
297             TypeVariants::TyArray(elem_ty, _) => {
298                 let elem_layout = fx.layout_of(elem_ty);
299                 let size = fx
300                     .bcx
301                     .ins()
302                     .iconst(types::I64, elem_layout.size.bytes() as i64);
303                 let offset = fx.bcx.ins().imul(size, index);
304                 CPlace::Addr(fx.bcx.ins().iadd(addr, offset), elem_layout)
305             }
306             TypeVariants::TySlice(_elem_ty) => unimplemented!("place_index(TySlice)"),
307             _ => bug!("place_index({:?})", layout.ty),
308         }
309     }
310
311     pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
312         match self {
313             CPlace::Var(var, _) => CPlace::Var(var, layout),
314             CPlace::Addr(addr, _) => CPlace::Addr(addr, layout),
315         }
316     }
317
318     pub fn downcast_variant(self, fx: &FunctionCx<'a, 'tcx, impl Backend>, variant: usize) -> Self {
319         let layout = self.layout().for_variant(fx, variant);
320         self.unchecked_cast_to(layout)
321     }
322 }
323
324 pub fn cton_intcast<'a, 'tcx: 'a>(
325     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
326     val: Value,
327     to: Type,
328     signed: bool,
329 ) -> Value {
330     let from = fx.bcx.func.dfg.value_type(val);
331     if from == to {
332         return val;
333     }
334     if to.wider_or_equal(from) {
335         if signed {
336             fx.bcx.ins().sextend(to, val)
337         } else {
338             fx.bcx.ins().uextend(to, val)
339         }
340     } else {
341         fx.bcx.ins().ireduce(to, val)
342     }
343 }
344
345 pub struct FunctionCx<'a, 'tcx: 'a, B: Backend + 'a> {
346     pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
347     pub module: &'a mut Module<B>,
348     pub instance: Instance<'tcx>,
349     pub mir: &'tcx Mir<'tcx>,
350     pub param_substs: &'tcx Substs<'tcx>,
351     pub bcx: FunctionBuilder<'a>,
352     pub ebb_map: HashMap<BasicBlock, Ebb>,
353     pub local_map: HashMap<Local, CPlace<'tcx>>,
354     pub comments: HashMap<Inst, String>,
355     pub constants: &'a mut crate::constant::ConstantCx,
356
357     /// add_global_comment inserts a comment here
358     pub top_nop: Option<Inst>,
359 }
360
361 impl<'a, 'tcx: 'a, B: Backend + 'a> fmt::Debug for FunctionCx<'a, 'tcx, B> {
362     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
363         writeln!(f, "{:?}", self.param_substs)?;
364         writeln!(f, "{:?}", self.local_map)?;
365
366         let mut clif = String::new();
367         let mut writer = crate::pretty_clif::CommentWriter(self.comments.clone());
368         ::cranelift::codegen::write::decorate_function(
369             &mut writer,
370             &mut clif,
371             &self.bcx.func,
372             None,
373         ).unwrap();
374         writeln!(f, "\n{}", clif)
375     }
376 }
377
378 impl<'a, 'tcx: 'a, B: Backend> LayoutOf for &'a FunctionCx<'a, 'tcx, B> {
379     type Ty = Ty<'tcx>;
380     type TyLayout = TyLayout<'tcx>;
381
382     fn layout_of(self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
383         let ty = self.monomorphize(&ty);
384         self.tcx.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap()
385     }
386 }
387
388 impl<'a, 'tcx, B: Backend + 'a> layout::HasTyCtxt<'tcx> for &'a FunctionCx<'a, 'tcx, B> {
389     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> {
390         self.tcx
391     }
392 }
393
394 impl<'a, 'tcx, B: Backend + 'a> layout::HasDataLayout for &'a FunctionCx<'a, 'tcx, B> {
395     fn data_layout(&self) -> &layout::TargetDataLayout {
396         &self.tcx.data_layout
397     }
398 }
399
400 impl<'a, 'tcx, B: Backend + 'a> HasTargetSpec for &'a FunctionCx<'a, 'tcx, B> {
401     fn target_spec(&self) -> &Target {
402         &self.tcx.sess.target.target
403     }
404 }
405
406 impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
407     pub fn monomorphize<T>(&self, value: &T) -> T
408     where
409         T: TypeFoldable<'tcx>,
410     {
411         self.tcx.subst_and_normalize_erasing_regions(
412             self.param_substs,
413             ty::ParamEnv::reveal_all(),
414             value,
415         )
416     }
417
418     pub fn cton_type(&self, ty: Ty<'tcx>) -> Option<Type> {
419         cton_type_from_ty(self.tcx, self.monomorphize(&ty))
420     }
421
422     pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
423         *self.ebb_map.get(&bb).unwrap()
424     }
425
426     pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
427         *self.local_map.get(&local).unwrap()
428     }
429 }