3 fn codegen_field<'a, 'tcx: 'a>(
4 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
6 layout: TyLayout<'tcx>,
8 ) -> (Value, TyLayout<'tcx>) {
9 let field_offset = layout.fields.offset(field.index());
10 let field_ty = layout.field(&*fx, field.index());
11 if field_offset.bytes() > 0 {
13 fx.bcx.ins().iadd_imm(base, field_offset.bytes() as i64),
22 #[derive(Debug, Copy, Clone)]
23 pub struct CValue<'tcx>(CValueInner, TyLayout<'tcx>);
25 #[derive(Debug, Copy, Clone)]
29 ByValPair(Value, Value),
32 impl<'tcx> CValue<'tcx> {
33 pub fn by_ref(value: Value, layout: TyLayout<'tcx>) -> CValue<'tcx> {
34 CValue(CValueInner::ByRef(value), layout)
37 pub fn by_val(value: Value, layout: TyLayout<'tcx>) -> CValue<'tcx> {
38 CValue(CValueInner::ByVal(value), layout)
41 pub fn by_val_pair(value: Value, extra: Value, layout: TyLayout<'tcx>) -> CValue<'tcx> {
42 CValue(CValueInner::ByValPair(value, extra), layout)
45 pub fn layout(&self) -> TyLayout<'tcx> {
49 pub fn force_stack<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value
55 CValueInner::ByRef(value) => value,
56 CValueInner::ByVal(_) | CValueInner::ByValPair(_, _) => {
57 let cplace = CPlace::new_stack_slot(fx, layout.ty);
58 cplace.write_cvalue(fx, self);
64 /// Load a value with layout.abi of scalar
65 pub fn load_scalar<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value
71 CValueInner::ByRef(addr) => {
72 let scalar = match layout.abi {
73 layout::Abi::Scalar(ref scalar) => scalar.clone(),
76 let clif_ty = scalar_to_clif_type(fx.tcx, scalar);
77 fx.bcx.ins().load(clif_ty, MemFlags::new(), addr, 0)
79 CValueInner::ByVal(value) => value,
80 CValueInner::ByValPair(_, _) => bug!("Please use load_scalar_pair for ByValPair"),
84 /// Load a value pair with layout.abi of scalar pair
85 pub fn load_scalar_pair<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> (Value, Value)
91 CValueInner::ByRef(addr) => {
92 let (a, b) = match &layout.abi {
93 layout::Abi::ScalarPair(a, b) => (a.clone(), b.clone()),
96 let clif_ty1 = scalar_to_clif_type(fx.tcx, a.clone());
97 let clif_ty2 = scalar_to_clif_type(fx.tcx, b);
98 let val1 = fx.bcx.ins().load(clif_ty1, MemFlags::new(), addr, 0);
99 let val2 = fx.bcx.ins().load(
103 a.value.size(&fx.tcx).bytes() as i32,
107 CValueInner::ByVal(_) => bug!("Please use load_scalar for ByVal"),
108 CValueInner::ByValPair(val1, val2) => (val1, val2),
112 pub fn value_field<'a>(
114 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
121 let base = match self.0 {
122 CValueInner::ByRef(addr) => addr,
123 _ => bug!("place_field for {:?}", self),
126 let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
127 CValue::by_ref(field_ptr, field_layout)
130 pub fn unsize_value<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>, dest: CPlace<'tcx>) {
131 crate::unsize::coerce_unsized_into(fx, self, dest);
134 pub fn const_val<'a>(
135 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
142 let clif_ty = fx.clif_type(ty).unwrap();
143 let layout = fx.layout_of(ty);
144 CValue::by_val(fx.bcx.ins().iconst(clif_ty, const_val), layout)
147 pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
148 CValue(self.0, layout)
152 /// A place where you can write a value to or read a value from
153 #[derive(Debug, Copy, Clone)]
154 pub enum CPlace<'tcx> {
155 Var(Local, TyLayout<'tcx>),
156 Addr(Value, Option<Value>, TyLayout<'tcx>),
157 Stack(StackSlot, TyLayout<'tcx>),
158 NoPlace(TyLayout<'tcx>),
161 impl<'a, 'tcx: 'a> CPlace<'tcx> {
162 pub fn layout(&self) -> TyLayout<'tcx> {
164 CPlace::Var(_, layout)
165 | CPlace::Addr(_, _, layout)
166 | CPlace::Stack(_, layout)
167 | CPlace::NoPlace(layout) => layout,
171 pub fn no_place(layout: TyLayout<'tcx>) -> CPlace<'tcx> {
172 CPlace::NoPlace(layout)
175 pub fn new_stack_slot(
176 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
179 let layout = fx.layout_of(ty);
180 assert!(!layout.is_unsized());
181 if layout.size.bytes() == 0 {
182 return CPlace::NoPlace(layout);
185 let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
186 kind: StackSlotKind::ExplicitSlot,
187 size: layout.size.bytes() as u32,
190 CPlace::Stack(stack_slot, layout)
194 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
196 layout: TyLayout<'tcx>,
199 .declare_var(mir_var(local), fx.clif_type(layout.ty).unwrap());
200 CPlace::Var(local, layout)
203 pub fn for_addr(addr: Value, layout: TyLayout<'tcx>) -> CPlace<'tcx> {
204 CPlace::Addr(addr, None, layout)
207 pub fn for_addr_with_extra(addr: Value, extra: Value, layout: TyLayout<'tcx>) -> CPlace<'tcx> {
208 CPlace::Addr(addr, Some(extra), layout)
211 pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CValue<'tcx> {
213 CPlace::Var(var, layout) => CValue::by_val(fx.bcx.use_var(mir_var(var)), layout),
214 CPlace::Addr(addr, extra, layout) => {
215 assert!(extra.is_none(), "unsized values are not yet supported");
216 CValue::by_ref(addr, layout)
218 CPlace::Stack(stack_slot, layout) => CValue::by_ref(
219 fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0),
222 CPlace::NoPlace(layout) => CValue::by_ref(
225 .iconst(fx.pointer_type, fx.pointer_type.bytes() as i64),
231 pub fn to_addr(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value {
232 match self.to_addr_maybe_unsized(fx) {
233 (addr, None) => addr,
234 (_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
238 pub fn to_addr_maybe_unsized(
240 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
241 ) -> (Value, Option<Value>) {
243 CPlace::Addr(addr, extra, _layout) => (addr, extra),
244 CPlace::Stack(stack_slot, _layout) => (
245 fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0),
248 CPlace::NoPlace(_) => (fx.bcx.ins().iconst(fx.pointer_type, 45), None),
249 CPlace::Var(_, _) => bug!("Expected CPlace::Addr, found CPlace::Var"),
253 pub fn write_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>, from: CValue<'tcx>) {
254 use rustc::hir::Mutability::*;
256 let from_ty = from.layout().ty;
257 let to_ty = self.layout().ty;
259 fn assert_assignable<'a, 'tcx: 'a>(fx: &FunctionCx<'a, 'tcx, impl Backend>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) {
260 match (&from_ty.sty, &to_ty.sty) {
261 (ty::Ref(_, t, MutImmutable), ty::Ref(_, u, MutImmutable))
262 | (ty::Ref(_, t, MutMutable), ty::Ref(_, u, MutImmutable))
263 | (ty::Ref(_, t, MutMutable), ty::Ref(_, u, MutMutable)) => {
264 assert_assignable(fx, t, u);
265 // &mut T -> &T is allowed
266 // &'a T -> &'b T is allowed
268 (ty::Ref(_, _, MutImmutable), ty::Ref(_, _, MutMutable)) => {
269 panic!("Cant assign value of type {} to place of type {}", from_ty, to_ty)
271 (ty::FnPtr(_), ty::FnPtr(_)) => {
272 let from_sig = fx.tcx.normalize_erasing_late_bound_regions(
273 ParamEnv::reveal_all(),
274 &from_ty.fn_sig(fx.tcx),
276 let to_sig = fx.tcx.normalize_erasing_late_bound_regions(
277 ParamEnv::reveal_all(),
278 &to_ty.fn_sig(fx.tcx),
282 "Can't write fn ptr with incompatible sig {:?} to place with sig {:?}\n\n{:#?}",
283 from_sig, to_sig, fx,
285 // fn(&T) -> for<'l> fn(&'l T) is allowed
287 (ty::Dynamic(from_traits, _), ty::Dynamic(to_traits, _)) => {
288 let from_traits = fx.tcx.normalize_erasing_late_bound_regions(
289 ParamEnv::reveal_all(),
292 let to_traits = fx.tcx.normalize_erasing_late_bound_regions(
293 ParamEnv::reveal_all(),
297 from_traits, to_traits,
298 "Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}",
299 from_traits, to_traits, fx,
301 // dyn for<'r> Trait<'r> -> dyn Trait<'_> is allowed
307 "Can't write value with incompatible type {:?} to place with type {:?}\n\n{:#?}",
316 assert_assignable(fx, from_ty, to_ty);
318 let (addr, dst_layout) = match self {
319 CPlace::Var(var, _) => {
320 let data = from.load_scalar(fx);
321 fx.bcx.def_var(mir_var(var), data);
324 CPlace::Addr(addr, None, dst_layout) => (addr, dst_layout),
325 CPlace::Stack(stack_slot, dst_layout) => (
326 fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0),
329 CPlace::NoPlace(layout) => {
330 if layout.abi != Abi::Uninhabited {
331 assert_eq!(layout.size.bytes(), 0, "{:?}", layout);
335 CPlace::Addr(_, _, _) => bug!("Can't write value to unsized place {:?}", self),
339 CValueInner::ByVal(val) => {
340 fx.bcx.ins().store(MemFlags::new(), val, addr, 0);
342 CValueInner::ByValPair(value, extra) => {
343 match dst_layout.abi {
344 Abi::ScalarPair(ref a, _) => {
345 fx.bcx.ins().store(MemFlags::new(), value, addr, 0);
350 a.value.size(&fx.tcx).bytes() as u32 as i32,
354 "Non ScalarPair abi {:?} for ByValPair CValue",
359 CValueInner::ByRef(from_addr) => {
360 let src_layout = from.1;
361 let size = dst_layout.size.bytes();
362 let src_align = src_layout.align.abi.bytes() as u8;
363 let dst_align = dst_layout.align.abi.bytes() as u8;
364 fx.bcx.emit_small_memcpy(
365 fx.module.target_config(),
378 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
381 let layout = self.layout();
382 let (base, extra) = self.to_addr_maybe_unsized(fx);
384 let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
385 let extra = if field_layout.is_unsized() {
386 assert!(extra.is_some());
391 CPlace::Addr(field_ptr, extra, field_layout)
396 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
399 let (elem_layout, addr) = match self.layout().ty.sty {
400 ty::Array(elem_ty, _) => (fx.layout_of(elem_ty), self.to_addr(fx)),
401 ty::Slice(elem_ty) => (fx.layout_of(elem_ty), self.to_addr_maybe_unsized(fx).0),
402 _ => bug!("place_index({:?})", self.layout().ty),
408 .imul_imm(index, elem_layout.size.bytes() as i64);
410 CPlace::Addr(fx.bcx.ins().iadd(addr, offset), None, elem_layout)
413 pub fn place_deref(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CPlace<'tcx> {
414 let inner_layout = fx.layout_of(self.layout().ty.builtin_deref(true).unwrap().ty);
415 if !inner_layout.is_unsized() {
416 CPlace::Addr(self.to_cvalue(fx).load_scalar(fx), None, inner_layout)
418 match self.layout().abi {
419 Abi::ScalarPair(ref a, ref b) => {
420 let addr = self.to_addr(fx);
424 .load(scalar_to_clif_type(fx.tcx, a.clone()), MemFlags::new(), addr, 0);
425 let extra = fx.bcx.ins().load(
426 scalar_to_clif_type(fx.tcx, b.clone()),
429 a.value.size(&fx.tcx).bytes() as u32 as i32,
431 CPlace::Addr(ptr, Some(extra), inner_layout)
434 "Fat ptr doesn't have abi ScalarPair, but it has {:?}",
441 pub fn write_place_ref(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>, dest: CPlace<'tcx>) {
442 if !self.layout().is_unsized() {
443 let ptr = CValue::by_val(self.to_addr(fx), dest.layout());
444 dest.write_cvalue(fx, ptr);
446 let (value, extra) = self.to_addr_maybe_unsized(fx);
448 match dest.layout().abi {
449 Abi::ScalarPair(ref a, _) => {
450 let dest_addr = dest.to_addr(fx);
451 fx.bcx.ins().store(MemFlags::new(), value, dest_addr, 0);
454 extra.expect("unsized type without metadata"),
456 a.value.size(&fx.tcx).bytes() as u32 as i32,
460 "Non ScalarPair abi {:?} in write_place_ref dest",
467 pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
468 assert!(!self.layout().is_unsized());
470 CPlace::Var(var, _) => CPlace::Var(var, layout),
471 CPlace::Addr(addr, extra, _) => CPlace::Addr(addr, extra, layout),
472 CPlace::Stack(stack_slot, _) => CPlace::Stack(stack_slot, layout),
473 CPlace::NoPlace(_) => {
474 assert!(layout.size.bytes() == 0);
475 CPlace::NoPlace(layout)
480 pub fn downcast_variant(
482 fx: &FunctionCx<'a, 'tcx, impl Backend>,
485 let layout = self.layout().for_variant(fx, variant);
486 self.unchecked_cast_to(layout)