Clean up MIR transform
This commit is contained in:
parent
96db5e9c7b
commit
33b4d20338
2 changed files with 51 additions and 11 deletions
|
@ -26,7 +26,8 @@ impl<const D: u64> EnumSizeOpt<D> {
|
||||||
ty::Adt(adt_def, _substs) if adt_def.is_enum() => {
|
ty::Adt(adt_def, _substs) if adt_def.is_enum() => {
|
||||||
let p_e = tcx.param_env(body_did);
|
let p_e = tcx.param_env(body_did);
|
||||||
// FIXME(jknodt) handle error better below
|
// FIXME(jknodt) handle error better below
|
||||||
let layout = tcx.layout_of(p_e.and(ty)).unwrap();
|
let layout =
|
||||||
|
if let Ok(layout) = tcx.layout_of(p_e.and(ty)) { layout } else { return None };
|
||||||
let variants = &layout.variants;
|
let variants = &layout.variants;
|
||||||
match variants {
|
match variants {
|
||||||
Variants::Single { .. } => None,
|
Variants::Single { .. } => None,
|
||||||
|
@ -84,7 +85,16 @@ impl<const D: u64> EnumSizeOpt<D> {
|
||||||
} else {
|
} else {
|
||||||
let mut data =
|
let mut data =
|
||||||
vec![0; std::mem::size_of::<usize>() * num_variants as usize];
|
vec![0; std::mem::size_of::<usize>() * num_variants as usize];
|
||||||
data.copy_from_slice(unsafe { std::mem::transmute(&sizes[..]) });
|
|
||||||
|
let mut curr = 0;
|
||||||
|
for byte in sizes
|
||||||
|
.iter()
|
||||||
|
.flat_map(|sz| sz.bytes().to_ne_bytes())
|
||||||
|
.take(data.len())
|
||||||
|
{
|
||||||
|
data[curr] = byte;
|
||||||
|
curr += 1;
|
||||||
|
}
|
||||||
let alloc = interpret::Allocation::from_bytes(
|
let alloc = interpret::Allocation::from_bytes(
|
||||||
data,
|
data,
|
||||||
tcx.data_layout.ptr_sized_integer().align(&tcx.data_layout).abi,
|
tcx.data_layout.ptr_sized_integer().align(&tcx.data_layout).abi,
|
||||||
|
@ -123,9 +133,9 @@ impl<const D: u64> EnumSizeOpt<D> {
|
||||||
kind: StatementKind::Assign(box (place, rval)),
|
kind: StatementKind::Assign(box (place, rval)),
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME(jknodt) do I need to add a storage live here for this place?
|
|
||||||
let discr_place = Place {
|
let discr_place = Place {
|
||||||
local: patch.new_temp(tcx.types.usize, span),
|
// How do I get the discriminant type?
|
||||||
|
local: patch.new_temp(tcx.types.isize, span),
|
||||||
projection: List::empty(),
|
projection: List::empty(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -155,9 +165,8 @@ impl<const D: u64> EnumSizeOpt<D> {
|
||||||
)),
|
)),
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME(jknodt) do I need to add a storage live here for this place?
|
|
||||||
let dst = Place {
|
let dst = Place {
|
||||||
local: patch.new_temp(tcx.mk_mut_ptr(tcx.types.u8), span),
|
local: patch.new_temp(tcx.mk_mut_ptr(ty), span),
|
||||||
projection: List::empty(),
|
projection: List::empty(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -169,9 +178,22 @@ impl<const D: u64> EnumSizeOpt<D> {
|
||||||
)),
|
)),
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME(jknodt) do I need to add a storage live here for this place?
|
let dst_cast_ty = tcx.mk_mut_ptr(tcx.types.u8);
|
||||||
|
let dst_cast_place = Place {
|
||||||
|
local: patch.new_temp(dst_cast_ty, span),
|
||||||
|
projection: List::empty(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let dst_cast = Statement {
|
||||||
|
source_info,
|
||||||
|
kind: StatementKind::Assign(box (
|
||||||
|
dst_cast_place,
|
||||||
|
Rvalue::Cast(CastKind::Misc, Operand::Copy(dst), dst_cast_ty),
|
||||||
|
)),
|
||||||
|
};
|
||||||
|
|
||||||
let src = Place {
|
let src = Place {
|
||||||
local: patch.new_temp(tcx.mk_imm_ptr(tcx.types.u8), span),
|
local: patch.new_temp(tcx.mk_imm_ptr(ty), span),
|
||||||
projection: List::empty(),
|
projection: List::empty(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -179,15 +201,29 @@ impl<const D: u64> EnumSizeOpt<D> {
|
||||||
source_info,
|
source_info,
|
||||||
kind: StatementKind::Assign(box (
|
kind: StatementKind::Assign(box (
|
||||||
src,
|
src,
|
||||||
Rvalue::AddressOf(Mutability::Mut, *rhs),
|
Rvalue::AddressOf(Mutability::Not, *rhs),
|
||||||
|
)),
|
||||||
|
};
|
||||||
|
|
||||||
|
let src_cast_ty = tcx.mk_imm_ptr(tcx.types.u8);
|
||||||
|
let src_cast_place = Place {
|
||||||
|
local: patch.new_temp(src_cast_ty, span),
|
||||||
|
projection: List::empty(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let src_cast = Statement {
|
||||||
|
source_info,
|
||||||
|
kind: StatementKind::Assign(box (
|
||||||
|
src_cast_place,
|
||||||
|
Rvalue::Cast(CastKind::Misc, Operand::Copy(src), src_cast_ty),
|
||||||
)),
|
)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let copy_bytes = Statement {
|
let copy_bytes = Statement {
|
||||||
source_info,
|
source_info,
|
||||||
kind: StatementKind::CopyNonOverlapping(box CopyNonOverlapping {
|
kind: StatementKind::CopyNonOverlapping(box CopyNonOverlapping {
|
||||||
src: Operand::Copy(src),
|
src: Operand::Copy(src_cast_place),
|
||||||
dst: Operand::Copy(src),
|
dst: Operand::Copy(dst_cast_place),
|
||||||
count: Operand::Constant(
|
count: Operand::Constant(
|
||||||
box (Constant {
|
box (Constant {
|
||||||
span,
|
span,
|
||||||
|
@ -211,7 +247,9 @@ impl<const D: u64> EnumSizeOpt<D> {
|
||||||
store_discr,
|
store_discr,
|
||||||
store_size,
|
store_size,
|
||||||
dst_ptr,
|
dst_ptr,
|
||||||
|
dst_cast,
|
||||||
src_ptr,
|
src_ptr,
|
||||||
|
src_cast,
|
||||||
copy_bytes,
|
copy_bytes,
|
||||||
store_dead,
|
store_dead,
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
#![feature(box_patterns)]
|
#![feature(box_patterns)]
|
||||||
#![feature(drain_filter)]
|
#![feature(drain_filter)]
|
||||||
#![feature(let_chains)]
|
#![feature(let_chains)]
|
||||||
|
#![feature(let_else)]
|
||||||
|
#![feature(entry_insert)]
|
||||||
#![feature(map_try_insert)]
|
#![feature(map_try_insert)]
|
||||||
#![feature(min_specialization)]
|
#![feature(min_specialization)]
|
||||||
#![feature(never_type)]
|
#![feature(never_type)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue