Improve public interface of CValue and CPlace
This commit is contained in:
parent
174b73e3c9
commit
edc05ae489
@ -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 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
|
// FIXME(eddyb) perhaps move some of this logic into
|
||||||
// `Instance::resolve_drop_in_place`?
|
// `Instance::resolve_drop_in_place`?
|
||||||
|
11
src/base.rs
11
src/base.rs
@ -694,11 +694,11 @@ fn codegen_stmt<'tcx>(
|
|||||||
}
|
}
|
||||||
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), ref operand, _to_ty) => {
|
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), ref operand, _to_ty) => {
|
||||||
let operand = codegen_operand(fx, operand);
|
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, _) => {
|
Rvalue::Cast(CastKind::DynStar, ref operand, _) => {
|
||||||
let operand = codegen_operand(fx, 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) => {
|
Rvalue::Cast(CastKind::Transmute, ref operand, _to_ty) => {
|
||||||
let operand = codegen_operand(fx, operand);
|
let operand = codegen_operand(fx, operand);
|
||||||
@ -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;
|
let len = fx.monomorphize(len).eval_target_usize(fx.tcx, ParamEnv::reveal_all()) as i64;
|
||||||
fx.bcx.ins().iconst(fx.pointer_type, len)
|
fx.bcx.ins().iconst(fx.pointer_type, len)
|
||||||
}
|
}
|
||||||
ty::Slice(_elem_ty) => {
|
ty::Slice(_elem_ty) => place.to_ptr_unsized().1,
|
||||||
place.to_ptr_maybe_unsized().1.expect("Length metadata for slice place")
|
|
||||||
}
|
|
||||||
_ => bug!("Rvalue::Len({:?})", place),
|
_ => bug!("Rvalue::Len({:?})", place),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -900,8 +898,7 @@ pub(crate) fn codegen_place<'tcx>(
|
|||||||
ty::Slice(elem_ty) => {
|
ty::Slice(elem_ty) => {
|
||||||
assert!(from_end, "slice subslices should be `from_end`");
|
assert!(from_end, "slice subslices should be `from_end`");
|
||||||
let elem_layout = fx.layout_of(*elem_ty);
|
let elem_layout = fx.layout_of(*elem_ty);
|
||||||
let (ptr, len) = cplace.to_ptr_maybe_unsized();
|
let (ptr, len) = cplace.to_ptr_unsized();
|
||||||
let len = len.unwrap();
|
|
||||||
cplace = CPlace::for_ptr_with_extra(
|
cplace = CPlace::for_ptr_with_extra(
|
||||||
ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * (from as i64)),
|
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)),
|
fx.bcx.ins().iadd_imm(len, -(from as i64 + to as i64)),
|
||||||
|
@ -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.
|
/// If `ty` is signed, `const_val` must already be sign extended.
|
||||||
pub(crate) fn const_val(
|
pub(crate) fn const_val(
|
||||||
fx: &mut FunctionCx<'_, '_, 'tcx>,
|
fx: &mut FunctionCx<'_, '_, 'tcx>,
|
||||||
@ -454,18 +446,21 @@ impl<'tcx> CPlace<'tcx> {
|
|||||||
|
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
pub(crate) fn to_ptr(self) -> Pointer {
|
pub(crate) fn to_ptr(self) -> Pointer {
|
||||||
match self.to_ptr_maybe_unsized() {
|
match self.inner {
|
||||||
(ptr, None) => ptr,
|
CPlaceInner::Addr(ptr, None) => ptr,
|
||||||
(_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
|
CPlaceInner::Addr(_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
|
||||||
|
CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => {
|
||||||
|
bug!("Expected CPlace::Addr, found {:?}", self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[track_caller]
|
#[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 {
|
match self.inner {
|
||||||
CPlaceInner::Addr(ptr, extra) => (ptr, extra),
|
CPlaceInner::Addr(ptr, Some(extra)) => (ptr, extra),
|
||||||
CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => {
|
CPlaceInner::Addr(_, None) | CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => {
|
||||||
bug!("Expected CPlace::Addr, found {:?}", self)
|
bug!("Expected unsized cplace, found {:?}", self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -498,7 +493,7 @@ impl<'tcx> CPlace<'tcx> {
|
|||||||
from: CValue<'tcx>,
|
from: CValue<'tcx>,
|
||||||
method: &'static str,
|
method: &'static str,
|
||||||
) {
|
) {
|
||||||
fn transmute_value<'tcx>(
|
fn transmute_scalar<'tcx>(
|
||||||
fx: &mut FunctionCx<'_, '_, 'tcx>,
|
fx: &mut FunctionCx<'_, '_, 'tcx>,
|
||||||
var: Variable,
|
var: Variable,
|
||||||
data: Value,
|
data: Value,
|
||||||
@ -569,7 +564,7 @@ impl<'tcx> CPlace<'tcx> {
|
|||||||
CPlaceInner::Var(_local, var) => {
|
CPlaceInner::Var(_local, var) => {
|
||||||
let data = CValue(from.0, dst_layout).load_scalar(fx);
|
let data = CValue(from.0, dst_layout).load_scalar(fx);
|
||||||
let dst_ty = fx.clif_type(self.layout().ty).unwrap();
|
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) => {
|
CPlaceInner::VarPair(_local, var1, var2) => {
|
||||||
let (data1, data2) = if from.layout().ty == dst_layout.ty {
|
let (data1, data2) = if from.layout().ty == dst_layout.ty {
|
||||||
@ -580,8 +575,8 @@ impl<'tcx> CPlace<'tcx> {
|
|||||||
CValue(CValueInner::ByRef(ptr, None), dst_layout).load_scalar_pair(fx)
|
CValue(CValueInner::ByRef(ptr, None), dst_layout).load_scalar_pair(fx)
|
||||||
};
|
};
|
||||||
let (dst_ty1, dst_ty2) = fx.clif_pair_type(self.layout().ty).unwrap();
|
let (dst_ty1, dst_ty2) = fx.clif_pair_type(self.layout().ty).unwrap();
|
||||||
transmute_value(fx, var1, data1, dst_ty1);
|
transmute_scalar(fx, var1, data1, dst_ty1);
|
||||||
transmute_value(fx, var2, data2, dst_ty2);
|
transmute_scalar(fx, var2, data2, dst_ty2);
|
||||||
}
|
}
|
||||||
CPlaceInner::Addr(_, Some(_)) => bug!("Can't write value to unsized place {:?}", self),
|
CPlaceInner::Addr(_, Some(_)) => bug!("Can't write value to unsized place {:?}", self),
|
||||||
CPlaceInner::Addr(to_ptr, None) => {
|
CPlaceInner::Addr(to_ptr, None) => {
|
||||||
@ -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);
|
let (field_ptr, field_layout) = codegen_field(fx, base, extra, layout, field);
|
||||||
if field_layout.is_unsized() {
|
if field_layout.is_unsized() {
|
||||||
@ -721,7 +721,7 @@ impl<'tcx> CPlace<'tcx> {
|
|||||||
| CPlaceInner::VarPair(_, _, _) => bug!("Can't index into {self:?}"),
|
| 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),
|
_ => bug!("place_index({:?})", self.layout().ty),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -746,12 +746,8 @@ impl<'tcx> CPlace<'tcx> {
|
|||||||
layout: TyAndLayout<'tcx>,
|
layout: TyAndLayout<'tcx>,
|
||||||
) -> CValue<'tcx> {
|
) -> CValue<'tcx> {
|
||||||
if has_ptr_meta(fx.tcx, self.layout().ty) {
|
if has_ptr_meta(fx.tcx, self.layout().ty) {
|
||||||
let (ptr, extra) = self.to_ptr_maybe_unsized();
|
let (ptr, extra) = self.to_ptr_unsized();
|
||||||
CValue::by_val_pair(
|
CValue::by_val_pair(ptr.get_addr(fx), extra, layout)
|
||||||
ptr.get_addr(fx),
|
|
||||||
extra.expect("unsized type without metadata"),
|
|
||||||
layout,
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
CValue::by_val(self.to_ptr().get_addr(fx), layout)
|
CValue::by_val(self.to_ptr().get_addr(fx), layout)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user