3 use rustc_target::spec::{HasTargetSpec, Target};
5 use cranelift_module::{DataId, Module};
9 pub type CurrentBackend = ::cranelift_simplejit::SimpleJITBackend;
11 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
12 pub struct Variable(Local);
14 impl EntityRef for Variable {
15 fn new(u: usize) -> Self {
16 Variable(Local::new(u))
19 fn index(self) -> usize {
24 pub fn cton_type_from_ty<'a, 'tcx: 'a>(
25 tcx: TyCtxt<'a, 'tcx, 'tcx>,
27 ) -> Option<types::Type> {
29 TypeVariants::TyBool => types::I8,
30 TypeVariants::TyUint(size) => match size {
31 UintTy::U8 => types::I8,
32 UintTy::U16 => types::I16,
33 UintTy::U32 => types::I32,
34 UintTy::U64 => types::I64,
35 UintTy::U128 => unimplemented!("u128"),
36 UintTy::Usize => types::I64,
38 TypeVariants::TyInt(size) => match size {
39 IntTy::I8 => types::I8,
40 IntTy::I16 => types::I16,
41 IntTy::I32 => types::I32,
42 IntTy::I64 => types::I64,
43 IntTy::I128 => unimplemented!("i128"),
44 IntTy::Isize => types::I64,
46 TypeVariants::TyChar => types::I32,
47 TypeVariants::TyFloat(size) => match size {
48 FloatTy::F32 => types::F32,
49 FloatTy::F64 => types::F64,
51 TypeVariants::TyFnPtr(_) => types::I64,
52 TypeVariants::TyRawPtr(TypeAndMut { ty, mutbl: _ }) | TypeVariants::TyRef(_, ty, _) => {
53 if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
59 TypeVariants::TyParam(_) => bug!("{:?}: {:?}", ty, ty.sty),
64 fn codegen_field<'a, 'tcx: 'a>(
65 fx: &mut FunctionCx<'a, 'tcx>,
67 layout: TyLayout<'tcx>,
69 ) -> (Value, TyLayout<'tcx>) {
70 let field_offset = layout.fields.offset(field.index());
71 let field_ty = layout.field(&*fx, field.index());
72 if field_offset.bytes() > 0 {
73 let field_offset = fx.bcx.ins().iconst(types::I64, field_offset.bytes() as i64);
74 (fx.bcx.ins().iadd(base, field_offset), field_ty)
81 #[derive(Debug, Copy, Clone)]
82 pub enum CValue<'tcx> {
83 ByRef(Value, TyLayout<'tcx>),
84 ByVal(Value, TyLayout<'tcx>),
85 Func(FuncRef, TyLayout<'tcx>),
88 impl<'tcx> CValue<'tcx> {
89 pub fn layout(&self) -> TyLayout<'tcx> {
91 CValue::ByRef(_, layout) | CValue::ByVal(_, layout) | CValue::Func(_, layout) => layout,
95 pub fn force_stack<'a>(self, fx: &mut FunctionCx<'a, 'tcx>) -> Value
100 CValue::ByRef(value, _layout) => value,
101 CValue::ByVal(value, layout) => {
102 let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
103 kind: StackSlotKind::ExplicitSlot,
104 size: layout.size.bytes() as u32,
107 fx.bcx.ins().stack_store(value, stack_slot, 0);
108 fx.bcx.ins().stack_addr(types::I64, stack_slot, 0)
110 CValue::Func(func, ty) => {
111 let func = fx.bcx.ins().func_addr(types::I64, func);
112 CValue::ByVal(func, ty).force_stack(fx)
117 pub fn load_value<'a>(self, fx: &mut FunctionCx<'a, 'tcx>) -> Value
122 CValue::ByRef(addr, layout) => {
124 .cton_type(layout.ty)
125 .expect(&format!("load_value of type {:?}", layout.ty));
126 fx.bcx.ins().load(cton_ty, MemFlags::new(), addr, 0)
128 CValue::ByVal(value, _layout) => value,
129 CValue::Func(func, _layout) => fx.bcx.ins().func_addr(types::I64, func),
133 pub fn expect_byref(self) -> (Value, TyLayout<'tcx>) {
135 CValue::ByRef(value, layout) => (value, layout),
136 CValue::ByVal(_, _) => bug!("Expected CValue::ByRef, found CValue::ByVal: {:?}", self),
137 CValue::Func(_, _) => bug!("Expected CValue::ByRef, found CValue::Func: {:?}", self),
141 pub fn value_field<'a>(self, fx: &mut FunctionCx<'a, 'tcx>, field: mir::Field) -> CValue<'tcx>
145 let (base, layout) = match self {
146 CValue::ByRef(addr, layout) => (addr, layout),
147 _ => bug!("place_field for {:?}", self),
150 let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
151 CValue::ByRef(field_ptr, field_layout)
154 pub fn const_val<'a>(
155 fx: &mut FunctionCx<'a, 'tcx>,
162 let cton_ty = fx.cton_type(ty).unwrap();
163 let layout = fx.layout_of(ty);
164 CValue::ByVal(fx.bcx.ins().iconst(cton_ty, const_val), layout)
167 pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
169 CValue::ByRef(addr, _) => CValue::ByRef(addr, layout),
170 CValue::ByVal(val, _) => CValue::ByVal(val, layout),
171 CValue::Func(fun, _) => CValue::Func(fun, layout),
176 /// A place where you can write a value to or read a value from
177 #[derive(Debug, Copy, Clone)]
178 pub enum CPlace<'tcx> {
179 Var(Variable, TyLayout<'tcx>),
180 Addr(Value, TyLayout<'tcx>),
183 impl<'a, 'tcx: 'a> CPlace<'tcx> {
184 pub fn layout(&self) -> TyLayout<'tcx> {
186 CPlace::Var(_, layout) | CPlace::Addr(_, layout) => layout,
190 pub fn temp(fx: &mut FunctionCx<'a, 'tcx>, ty: Ty<'tcx>) -> CPlace<'tcx> {
191 let layout = fx.layout_of(ty);
192 let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
193 kind: StackSlotKind::ExplicitSlot,
194 size: layout.size.bytes() as u32,
197 CPlace::Addr(fx.bcx.ins().stack_addr(types::I64, stack_slot, 0), layout)
200 pub fn from_stack_slot(
201 fx: &mut FunctionCx<'a, 'tcx>,
202 stack_slot: StackSlot,
205 let layout = fx.layout_of(ty);
206 CPlace::Addr(fx.bcx.ins().stack_addr(types::I64, stack_slot, 0), layout)
209 pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx>) -> CValue<'tcx> {
211 CPlace::Var(var, layout) => CValue::ByVal(fx.bcx.use_var(var), layout),
212 CPlace::Addr(addr, layout) => CValue::ByRef(addr, layout),
216 pub fn expect_addr(self) -> Value {
218 CPlace::Addr(addr, _layout) => addr,
219 CPlace::Var(_, _) => bug!("Expected CPlace::Addr, found CPlace::Var"),
223 pub fn write_cvalue(self, fx: &mut FunctionCx<'a, 'tcx>, from: CValue<'tcx>) {
224 match (&self.layout().ty.sty, &from.layout().ty.sty) {
225 (TypeVariants::TyRef(_, t, dest_mut), TypeVariants::TyRef(_, u, src_mut))
226 if (if *dest_mut != ::rustc::hir::Mutability::MutImmutable && src_mut != dest_mut {
234 // &mut T -> &T is allowed
235 // &'a T -> &'b T is allowed
241 "Can't write value of incompatible type to place {:?} {:?}\n\n{:#?}",
242 self.layout().ty.sty,
243 from.layout().ty.sty,
250 CPlace::Var(var, _) => {
251 let data = from.load_value(fx);
252 fx.bcx.def_var(var, data)
254 CPlace::Addr(addr, layout) => {
255 let size = layout.size.bytes() as i32;
257 if let Some(_) = fx.cton_type(layout.ty) {
258 let data = from.load_value(fx);
259 fx.bcx.ins().store(MemFlags::new(), data, addr, 0);
261 let from = from.expect_byref();
263 while size - offset >= 8 {
267 .load(types::I64, MemFlags::new(), from.0, offset);
268 fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
271 while size - offset >= 4 {
275 .load(types::I32, MemFlags::new(), from.0, offset);
276 fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
279 while offset < size {
283 .load(types::I8, MemFlags::new(), from.0, offset);
284 fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
292 pub fn place_field(self, fx: &mut FunctionCx<'a, 'tcx>, field: mir::Field) -> CPlace<'tcx> {
293 let base = self.expect_addr();
294 let layout = self.layout();
296 let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
297 CPlace::Addr(field_ptr, field_layout)
300 pub fn place_index(self, fx: &mut FunctionCx<'a, 'tcx>, index: Value) -> CPlace<'tcx> {
301 let addr = self.expect_addr();
302 let layout = self.layout();
303 match layout.ty.sty {
304 TypeVariants::TyArray(elem_ty, _) => {
305 let elem_layout = fx.layout_of(elem_ty);
309 .iconst(types::I64, elem_layout.size.bytes() as i64);
310 let offset = fx.bcx.ins().imul(size, index);
311 CPlace::Addr(fx.bcx.ins().iadd(addr, offset), elem_layout)
313 TypeVariants::TySlice(_elem_ty) => unimplemented!("place_index(TySlice)"),
314 _ => bug!("place_index({:?})", layout.ty),
318 pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
320 CPlace::Var(var, _) => CPlace::Var(var, layout),
321 CPlace::Addr(addr, _) => CPlace::Addr(addr, layout),
325 pub fn downcast_variant(self, fx: &FunctionCx<'a, 'tcx>, variant: usize) -> Self {
326 let layout = self.layout().for_variant(fx, variant);
327 self.unchecked_cast_to(layout)
331 pub fn cton_intcast<'a, 'tcx: 'a>(
332 fx: &mut FunctionCx<'a, 'tcx>,
338 let from = fx.cton_type(from).unwrap();
339 let to = fx.cton_type(to).unwrap();
343 if to.wider_or_equal(from) {
345 fx.bcx.ins().sextend(to, val)
347 fx.bcx.ins().uextend(to, val)
350 fx.bcx.ins().ireduce(to, val)
354 pub struct FunctionCx<'a, 'tcx: 'a> {
355 pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
356 pub module: &'a mut Module<CurrentBackend>,
357 pub instance: Instance<'tcx>,
358 pub mir: &'tcx Mir<'tcx>,
359 pub param_substs: &'tcx Substs<'tcx>,
360 pub bcx: FunctionBuilder<'a, Variable>,
361 pub ebb_map: HashMap<BasicBlock, Ebb>,
362 pub local_map: HashMap<Local, CPlace<'tcx>>,
363 pub comments: HashMap<Inst, String>,
364 pub constants: &'a mut HashMap<AllocId, DataId>,
367 impl<'a, 'tcx: 'a> fmt::Debug for FunctionCx<'a, 'tcx> {
368 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
369 writeln!(f, "{:?}", self.param_substs)?;
370 writeln!(f, "{:?}", self.local_map)?;
372 let mut clif = String::new();
373 let mut writer = crate::pretty_clif::CommentWriter(self.comments.clone());
374 ::cranelift::codegen::write::decorate_function(
380 writeln!(f, "\n{}", clif)
384 impl<'a, 'tcx: 'a> LayoutOf for &'a FunctionCx<'a, 'tcx> {
386 type TyLayout = TyLayout<'tcx>;
388 fn layout_of(self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
389 let ty = self.monomorphize(&ty);
390 self.tcx.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap()
394 impl<'a, 'tcx> layout::HasTyCtxt<'tcx> for &'a FunctionCx<'a, 'tcx> {
395 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> {
400 impl<'a, 'tcx> layout::HasDataLayout for &'a FunctionCx<'a, 'tcx> {
401 fn data_layout(&self) -> &layout::TargetDataLayout {
402 &self.tcx.data_layout
406 impl<'a, 'tcx> HasTargetSpec for &'a FunctionCx<'a, 'tcx> {
407 fn target_spec(&self) -> &Target {
408 &self.tcx.sess.target.target
412 impl<'a, 'tcx: 'a> FunctionCx<'a, 'tcx> {
413 pub fn monomorphize<T>(&self, value: &T) -> T
415 T: TypeFoldable<'tcx>,
417 self.tcx.subst_and_normalize_erasing_regions(
419 ty::ParamEnv::reveal_all(),
424 pub fn cton_type(&self, ty: Ty<'tcx>) -> Option<Type> {
425 cton_type_from_ty(self.tcx, self.monomorphize(&ty))
428 pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
429 *self.ebb_map.get(&bb).unwrap()
432 pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
433 *self.local_map.get(&local).unwrap()