Skip to content

Commit

Permalink
Improve public interface of CValue and CPlace
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Mar 27, 2023
1 parent 174b73e commit edc05ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,9 @@ pub(crate) fn codegen_drop<'tcx>(
// | ... |
// \-------/
//
let (ptr, vtable) = drop_place.to_ptr_maybe_unsized();
let (ptr, vtable) = drop_place.to_ptr_unsized();
let ptr = ptr.get_addr(fx);
let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap());
let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable);

// FIXME(eddyb) perhaps move some of this logic into
// `Instance::resolve_drop_in_place`?
Expand Down
11 changes: 4 additions & 7 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,11 +694,11 @@ fn codegen_stmt<'tcx>(
}
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), ref operand, _to_ty) => {
let operand = codegen_operand(fx, operand);
operand.unsize_value(fx, lval);
crate::unsize::coerce_unsized_into(fx, operand, lval);
}
Rvalue::Cast(CastKind::DynStar, ref operand, _) => {
let operand = codegen_operand(fx, operand);
operand.coerce_dyn_star(fx, lval);
crate::unsize::coerce_dyn_star(fx, operand, lval);
}
Rvalue::Cast(CastKind::Transmute, ref operand, _to_ty) => {
let operand = codegen_operand(fx, operand);
Expand Down Expand Up @@ -844,9 +844,7 @@ fn codegen_array_len<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, place: CPlace<'tcx
let len = fx.monomorphize(len).eval_target_usize(fx.tcx, ParamEnv::reveal_all()) as i64;
fx.bcx.ins().iconst(fx.pointer_type, len)
}
ty::Slice(_elem_ty) => {
place.to_ptr_maybe_unsized().1.expect("Length metadata for slice place")
}
ty::Slice(_elem_ty) => place.to_ptr_unsized().1,
_ => bug!("Rvalue::Len({:?})", place),
}
}
Expand Down Expand Up @@ -900,8 +898,7 @@ pub(crate) fn codegen_place<'tcx>(
ty::Slice(elem_ty) => {
assert!(from_end, "slice subslices should be `from_end`");
let elem_layout = fx.layout_of(*elem_ty);
let (ptr, len) = cplace.to_ptr_maybe_unsized();
let len = len.unwrap();
let (ptr, len) = cplace.to_ptr_unsized();
cplace = CPlace::for_ptr_with_extra(
ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * (from as i64)),
fx.bcx.ins().iadd_imm(len, -(from as i64 + to as i64)),
Expand Down
50 changes: 23 additions & 27 deletions src/value_and_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,6 @@ impl<'tcx> CValue<'tcx> {
}
}

pub(crate) fn unsize_value(self, fx: &mut FunctionCx<'_, '_, 'tcx>, dest: CPlace<'tcx>) {
crate::unsize::coerce_unsized_into(fx, self, dest);
}

pub(crate) fn coerce_dyn_star(self, fx: &mut FunctionCx<'_, '_, 'tcx>, dest: CPlace<'tcx>) {
crate::unsize::coerce_dyn_star(fx, self, dest);
}

/// If `ty` is signed, `const_val` must already be sign extended.
pub(crate) fn const_val(
fx: &mut FunctionCx<'_, '_, 'tcx>,
Expand Down Expand Up @@ -454,18 +446,21 @@ impl<'tcx> CPlace<'tcx> {

#[track_caller]
pub(crate) fn to_ptr(self) -> Pointer {
match self.to_ptr_maybe_unsized() {
(ptr, None) => ptr,
(_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
match self.inner {
CPlaceInner::Addr(ptr, None) => ptr,
CPlaceInner::Addr(_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => {
bug!("Expected CPlace::Addr, found {:?}", self)
}
}
}

#[track_caller]
pub(crate) fn to_ptr_maybe_unsized(self) -> (Pointer, Option<Value>) {
pub(crate) fn to_ptr_unsized(self) -> (Pointer, Value) {
match self.inner {
CPlaceInner::Addr(ptr, extra) => (ptr, extra),
CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => {
bug!("Expected CPlace::Addr, found {:?}", self)
CPlaceInner::Addr(ptr, Some(extra)) => (ptr, extra),
CPlaceInner::Addr(_, None) | CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => {
bug!("Expected unsized cplace, found {:?}", self)
}
}
}
Expand Down Expand Up @@ -498,7 +493,7 @@ impl<'tcx> CPlace<'tcx> {
from: CValue<'tcx>,
method: &'static str,
) {
fn transmute_value<'tcx>(
fn transmute_scalar<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
var: Variable,
data: Value,
Expand Down Expand Up @@ -569,7 +564,7 @@ impl<'tcx> CPlace<'tcx> {
CPlaceInner::Var(_local, var) => {
let data = CValue(from.0, dst_layout).load_scalar(fx);
let dst_ty = fx.clif_type(self.layout().ty).unwrap();
transmute_value(fx, var, data, dst_ty);
transmute_scalar(fx, var, data, dst_ty);
}
CPlaceInner::VarPair(_local, var1, var2) => {
let (data1, data2) = if from.layout().ty == dst_layout.ty {
Expand All @@ -580,8 +575,8 @@ impl<'tcx> CPlace<'tcx> {
CValue(CValueInner::ByRef(ptr, None), dst_layout).load_scalar_pair(fx)
};
let (dst_ty1, dst_ty2) = fx.clif_pair_type(self.layout().ty).unwrap();
transmute_value(fx, var1, data1, dst_ty1);
transmute_value(fx, var2, data2, dst_ty2);
transmute_scalar(fx, var1, data1, dst_ty1);
transmute_scalar(fx, var2, data2, dst_ty2);
}
CPlaceInner::Addr(_, Some(_)) => bug!("Can't write value to unsized place {:?}", self),
CPlaceInner::Addr(to_ptr, None) => {
Expand Down Expand Up @@ -666,7 +661,12 @@ impl<'tcx> CPlace<'tcx> {
_ => {}
}

let (base, extra) = self.to_ptr_maybe_unsized();
let (base, extra) = match self.inner {
CPlaceInner::Addr(ptr, extra) => (ptr, extra),
CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => {
bug!("Expected CPlace::Addr, found {:?}", self)
}
};

let (field_ptr, field_layout) = codegen_field(fx, base, extra, layout, field);
if field_layout.is_unsized() {
Expand Down Expand Up @@ -721,7 +721,7 @@ impl<'tcx> CPlace<'tcx> {
| CPlaceInner::VarPair(_, _, _) => bug!("Can't index into {self:?}"),
}
}
ty::Slice(elem_ty) => (fx.layout_of(*elem_ty), self.to_ptr_maybe_unsized().0),
ty::Slice(elem_ty) => (fx.layout_of(*elem_ty), self.to_ptr_unsized().0),
_ => bug!("place_index({:?})", self.layout().ty),
};

Expand All @@ -746,12 +746,8 @@ impl<'tcx> CPlace<'tcx> {
layout: TyAndLayout<'tcx>,
) -> CValue<'tcx> {
if has_ptr_meta(fx.tcx, self.layout().ty) {
let (ptr, extra) = self.to_ptr_maybe_unsized();
CValue::by_val_pair(
ptr.get_addr(fx),
extra.expect("unsized type without metadata"),
layout,
)
let (ptr, extra) = self.to_ptr_unsized();
CValue::by_val_pair(ptr.get_addr(fx), extra, layout)
} else {
CValue::by_val(self.to_ptr().get_addr(fx), layout)
}
Expand Down

0 comments on commit edc05ae

Please sign in to comment.