3 use rustc_target::spec::{HasTargetSpec, Target};
5 use cranelift_module::Module;
9 pub fn mir_var(loc: Local) -> Variable {
10 Variable::with_u32(loc.index() as u32)
13 pub fn pointer_ty(tcx: TyCtxt) -> types::Type {
14 match tcx.data_layout.pointer_size.bits() {
18 bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits),
22 pub fn cton_type_from_ty<'a, 'tcx: 'a>(
23 tcx: TyCtxt<'a, 'tcx, 'tcx>,
25 ) -> Option<types::Type> {
27 ty::Bool => types::I8,
28 ty::Uint(size) => match size {
29 UintTy::U8 => types::I8,
30 UintTy::U16 => types::I16,
31 UintTy::U32 => types::I32,
32 UintTy::U64 => types::I64,
33 UintTy::U128 => unimpl!("u128"),
34 UintTy::Usize => pointer_ty(tcx),
36 ty::Int(size) => match size {
37 IntTy::I8 => types::I8,
38 IntTy::I16 => types::I16,
39 IntTy::I32 => types::I32,
40 IntTy::I64 => types::I64,
41 IntTy::I128 => unimpl!("i128"),
42 IntTy::Isize => pointer_ty(tcx),
44 ty::Char => types::I32,
45 ty::Float(size) => match size {
46 FloatTy::F32 => types::F32,
47 FloatTy::F64 => types::F64,
49 ty::FnPtr(_) => pointer_ty(tcx),
50 ty::RawPtr(TypeAndMut { ty, mutbl: _ }) | ty::Ref(_, ty, _) => {
51 if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
57 ty::Param(_) => bug!("{:?}: {:?}", ty, ty.sty),
62 fn codegen_field<'a, 'tcx: 'a>(
63 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
65 layout: TyLayout<'tcx>,
67 ) -> (Value, TyLayout<'tcx>) {
68 let field_offset = layout.fields.offset(field.index());
69 let field_ty = layout.field(&*fx, field.index());
70 if field_offset.bytes() > 0 {
72 fx.bcx.ins().iadd_imm(base, field_offset.bytes() as i64),
81 #[derive(Debug, Copy, Clone)]
82 pub enum CValue<'tcx> {
83 ByRef(Value, TyLayout<'tcx>),
84 ByVal(Value, TyLayout<'tcx>),
85 ByValPair(Value, Value, TyLayout<'tcx>),
88 impl<'tcx> CValue<'tcx> {
89 pub fn layout(&self) -> TyLayout<'tcx> {
91 CValue::ByRef(_, layout)
92 | CValue::ByVal(_, layout)
93 | CValue::ByValPair(_, _, layout) => layout,
97 pub fn force_stack<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value
102 CValue::ByRef(value, _layout) => value,
103 CValue::ByVal(value, layout) => {
104 let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
105 kind: StackSlotKind::ExplicitSlot,
106 size: layout.size.bytes() as u32,
109 fx.bcx.ins().stack_store(value, stack_slot, 0);
112 .stack_addr(fx.module.pointer_type(), stack_slot, 0)
114 CValue::ByValPair(value, extra, layout) => {
115 let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
116 kind: StackSlotKind::ExplicitSlot,
117 size: layout.size.bytes() as u32,
120 let base = fx.bcx.ins().stack_addr(types::I64, stack_slot, 0);
121 let a_addr = codegen_field(fx, base, layout, mir::Field::new(0)).0;
122 let b_addr = codegen_field(fx, base, layout, mir::Field::new(1)).0;
123 fx.bcx.ins().store(MemFlags::new(), value, a_addr, 0);
124 fx.bcx.ins().store(MemFlags::new(), extra, b_addr, 0);
130 pub fn load_value<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value
135 CValue::ByRef(addr, layout) => {
137 .cton_type(layout.ty)
138 .expect(&format!("load_value of type {:?}", layout.ty));
139 fx.bcx.ins().load(cton_ty, MemFlags::new(), addr, 0)
141 CValue::ByVal(value, _layout) => value,
142 CValue::ByValPair(_, _, _layout) => bug!("Please use load_value_pair for ByValPair"),
146 pub fn load_value_pair<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> (Value, Value)
151 CValue::ByRef(addr, layout) => {
154 fx.tcx.data_layout.pointer_size.bytes() * 2
156 let val1_offset = layout.fields.offset(0).bytes() as i32;
157 let val2_offset = layout.fields.offset(1).bytes() as i32;
161 .load(fx.module.pointer_type(), MemFlags::new(), addr, val1_offset);
165 .load(fx.module.pointer_type(), MemFlags::new(), addr, val2_offset);
168 CValue::ByVal(_, _layout) => bug!("Please use load_value for ByVal"),
169 CValue::ByValPair(val1, val2, _layout) => (val1, val2),
173 pub fn expect_byref(self) -> (Value, TyLayout<'tcx>) {
175 CValue::ByRef(value, layout) => (value, layout),
176 CValue::ByVal(_, _) => bug!("Expected CValue::ByRef, found CValue::ByVal: {:?}", self),
177 CValue::ByValPair(_, _, _) => bug!(
178 "Expected CValue::ByRef, found CValue::ByValPair: {:?}",
184 pub fn value_field<'a>(
186 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
192 let (base, layout) = match self {
193 CValue::ByRef(addr, layout) => (addr, layout),
194 _ => bug!("place_field for {:?}", self),
197 let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
198 CValue::ByRef(field_ptr, field_layout)
201 pub fn const_val<'a>(
202 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
209 let cton_ty = fx.cton_type(ty).unwrap();
210 let layout = fx.layout_of(ty);
211 CValue::ByVal(fx.bcx.ins().iconst(cton_ty, const_val), layout)
214 pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
216 CValue::ByRef(addr, _) => CValue::ByRef(addr, layout),
217 CValue::ByVal(val, _) => CValue::ByVal(val, layout),
218 CValue::ByValPair(val, extra, _) => CValue::ByValPair(val, extra, layout),
223 /// A place where you can write a value to or read a value from
224 #[derive(Debug, Copy, Clone)]
225 pub enum CPlace<'tcx> {
226 Var(Local, TyLayout<'tcx>),
227 Addr(Value, TyLayout<'tcx>),
230 impl<'a, 'tcx: 'a> CPlace<'tcx> {
231 pub fn layout(&self) -> TyLayout<'tcx> {
233 CPlace::Var(_, layout) | CPlace::Addr(_, layout) => layout,
237 pub fn temp(fx: &mut FunctionCx<'a, 'tcx, impl Backend>, ty: Ty<'tcx>) -> CPlace<'tcx> {
238 let layout = fx.layout_of(ty);
239 let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
240 kind: StackSlotKind::ExplicitSlot,
241 size: layout.size.bytes() as u32,
247 .stack_addr(fx.module.pointer_type(), stack_slot, 0),
252 pub fn from_stack_slot(
253 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
254 stack_slot: StackSlot,
257 let layout = fx.layout_of(ty);
261 .stack_addr(fx.module.pointer_type(), stack_slot, 0),
266 pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CValue<'tcx> {
268 CPlace::Var(var, layout) => CValue::ByVal(fx.bcx.use_var(mir_var(var)), layout),
269 CPlace::Addr(addr, layout) => CValue::ByRef(addr, layout),
273 pub fn expect_addr(self) -> Value {
275 CPlace::Addr(addr, _layout) => addr,
276 CPlace::Var(_, _) => bug!("Expected CPlace::Addr, found CPlace::Var"),
280 pub fn write_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>, from: CValue<'tcx>) {
281 match (&self.layout().ty.sty, &from.layout().ty.sty) {
282 (ty::Ref(_, t, dest_mut), ty::Ref(_, u, src_mut))
283 if (if *dest_mut != ::rustc::hir::Mutability::MutImmutable && src_mut != dest_mut {
291 // &mut T -> &T is allowed
292 // &'a T -> &'b T is allowed
298 "Can't write value of incompatible type to place {:?} {:?}\n\n{:#?}",
299 self.layout().ty.sty,
300 from.layout().ty.sty,
307 CPlace::Var(var, _) => {
308 let data = from.load_value(fx);
309 fx.bcx.def_var(mir_var(var), data)
311 CPlace::Addr(addr, layout) => {
312 let size = layout.size.bytes() as i32;
315 CValue::ByVal(val, _layout) => {
316 fx.bcx.ins().store(MemFlags::new(), val, addr, 0);
318 CValue::ByValPair(val1, val2, _layout) => {
319 let val1_offset = layout.fields.offset(0).bytes() as i32;
320 let val2_offset = layout.fields.offset(1).bytes() as i32;
321 fx.bcx.ins().store(MemFlags::new(), val1, addr, val1_offset);
322 fx.bcx.ins().store(MemFlags::new(), val2, addr, val2_offset);
324 CValue::ByRef(from, _layout) => {
326 while size - offset >= 8 {
327 let byte = fx.bcx.ins().load(
328 fx.module.pointer_type(),
333 fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
336 while size - offset >= 4 {
337 let byte = fx.bcx.ins().load(types::I32, MemFlags::new(), from, offset);
338 fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
341 while offset < size {
342 let byte = fx.bcx.ins().load(types::I8, MemFlags::new(), from, offset);
343 fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
354 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
357 let base = self.expect_addr();
358 let layout = self.layout();
360 let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
361 CPlace::Addr(field_ptr, field_layout)
366 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
369 let addr = self.expect_addr();
370 let layout = self.layout();
371 match layout.ty.sty {
372 ty::Array(elem_ty, _) => {
373 let elem_layout = fx.layout_of(elem_ty);
377 .imul_imm(index, elem_layout.size.bytes() as i64);
378 CPlace::Addr(fx.bcx.ins().iadd(addr, offset), elem_layout)
380 ty::Slice(_elem_ty) => unimplemented!("place_index(TySlice)"),
381 _ => bug!("place_index({:?})", layout.ty),
385 pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
387 CPlace::Var(var, _) => CPlace::Var(var, layout),
388 CPlace::Addr(addr, _) => CPlace::Addr(addr, layout),
392 pub fn downcast_variant(self, fx: &FunctionCx<'a, 'tcx, impl Backend>, variant: usize) -> Self {
393 let layout = self.layout().for_variant(fx, variant);
394 self.unchecked_cast_to(layout)
398 pub fn cton_intcast<'a, 'tcx: 'a>(
399 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
404 let from = fx.bcx.func.dfg.value_type(val);
408 if to.wider_or_equal(from) {
410 fx.bcx.ins().sextend(to, val)
412 fx.bcx.ins().uextend(to, val)
415 fx.bcx.ins().ireduce(to, val)
419 pub struct FunctionCx<'a, 'tcx: 'a, B: Backend + 'a> {
420 pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
421 pub module: &'a mut Module<B>,
422 pub instance: Instance<'tcx>,
423 pub mir: &'tcx Mir<'tcx>,
424 pub param_substs: &'tcx Substs<'tcx>,
425 pub bcx: FunctionBuilder<'a>,
426 pub ebb_map: HashMap<BasicBlock, Ebb>,
427 pub local_map: HashMap<Local, CPlace<'tcx>>,
428 pub comments: HashMap<Inst, String>,
429 pub constants: &'a mut crate::constant::ConstantCx,
431 /// add_global_comment inserts a comment here
432 pub top_nop: Option<Inst>,
435 impl<'a, 'tcx: 'a, B: Backend + 'a> fmt::Debug for FunctionCx<'a, 'tcx, B> {
436 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
437 writeln!(f, "{:?}", self.param_substs)?;
438 writeln!(f, "{:?}", self.local_map)?;
440 let mut clif = String::new();
441 let mut writer = crate::pretty_clif::CommentWriter(self.comments.clone());
442 ::cranelift::codegen::write::decorate_function(
448 writeln!(f, "\n{}", clif)
452 impl<'a, 'tcx: 'a, B: Backend> LayoutOf for &'a FunctionCx<'a, 'tcx, B> {
454 type TyLayout = TyLayout<'tcx>;
456 fn layout_of(self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
457 let ty = self.monomorphize(&ty);
458 self.tcx.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap()
462 impl<'a, 'tcx, B: Backend + 'a> layout::HasTyCtxt<'tcx> for &'a FunctionCx<'a, 'tcx, B> {
463 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> {
468 impl<'a, 'tcx, B: Backend + 'a> layout::HasDataLayout for &'a FunctionCx<'a, 'tcx, B> {
469 fn data_layout(&self) -> &layout::TargetDataLayout {
470 &self.tcx.data_layout
474 impl<'a, 'tcx, B: Backend + 'a> HasTargetSpec for &'a FunctionCx<'a, 'tcx, B> {
475 fn target_spec(&self) -> &Target {
476 &self.tcx.sess.target.target
480 impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
481 pub fn monomorphize<T>(&self, value: &T) -> T
483 T: TypeFoldable<'tcx>,
485 self.tcx.subst_and_normalize_erasing_regions(
487 ty::ParamEnv::reveal_all(),
492 pub fn cton_type(&self, ty: Ty<'tcx>) -> Option<Type> {
493 cton_type_from_ty(self.tcx, self.monomorphize(&ty))
496 pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
497 *self.ebb_map.get(&bb).unwrap()
500 pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
501 *self.local_map.get(&local).unwrap()