diff options
| author | Charles Lew <crlf0710@gmail.com> | 2021-06-14 18:02:53 +0800 |
|---|---|---|
| committer | Charles Lew <crlf0710@gmail.com> | 2021-06-15 01:59:00 +0800 |
| commit | a86d3a7e45e6c4db749df018678ef587ace21933 (patch) | |
| tree | 6c7cd6bc34a3c66b00cb11a916bacb47bd957016 /compiler/rustc_codegen_ssa/src | |
| parent | 14831568d506b5ee7be3e7d5a2f029ce9048b609 (diff) | |
| download | rust-a86d3a7e45e6c4db749df018678ef587ace21933.tar.gz rust-a86d3a7e45e6c4db749df018678ef587ace21933.zip | |
Refactor to make interpreter and codegen backend neutral to vtable internal representation.
Diffstat (limited to 'compiler/rustc_codegen_ssa/src')
| -rw-r--r-- | compiler/rustc_codegen_ssa/src/glue.rs | 7 | ||||
| -rw-r--r-- | compiler/rustc_codegen_ssa/src/meth.rs | 55 | ||||
| -rw-r--r-- | compiler/rustc_codegen_ssa/src/mir/block.rs | 6 |
3 files changed, 34 insertions, 34 deletions
diff --git a/compiler/rustc_codegen_ssa/src/glue.rs b/compiler/rustc_codegen_ssa/src/glue.rs index b88de0b2411..cf217b52c86 100644 --- a/compiler/rustc_codegen_ssa/src/glue.rs +++ b/compiler/rustc_codegen_ssa/src/glue.rs @@ -23,7 +23,12 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( ty::Dynamic(..) => { // load size/align from vtable let vtable = info.unwrap(); - (meth::SIZE.get_usize(bx, vtable), meth::ALIGN.get_usize(bx, vtable)) + ( + meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_SIZE) + .get_usize(bx, vtable), + meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_ALIGN) + .get_usize(bx, vtable), + ) } ty::Slice(_) | ty::Str => { let unit = layout.field(bx, 0); diff --git a/compiler/rustc_codegen_ssa/src/meth.rs b/compiler/rustc_codegen_ssa/src/meth.rs index bcc19c6a44b..4f0de729704 100644 --- a/compiler/rustc_codegen_ssa/src/meth.rs +++ b/compiler/rustc_codegen_ssa/src/meth.rs @@ -1,18 +1,14 @@ use crate::traits::*; -use rustc_middle::ty::{self, Instance, Ty}; +use rustc_middle::ty::{self, Instance, Ty, VtblEntry, COMMON_VTABLE_ENTRIES}; use rustc_target::abi::call::FnAbi; #[derive(Copy, Clone, Debug)] pub struct VirtualIndex(u64); -pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0); -pub const SIZE: VirtualIndex = VirtualIndex(1); -pub const ALIGN: VirtualIndex = VirtualIndex(2); - impl<'a, 'tcx> VirtualIndex { pub fn from_index(index: usize) -> Self { - VirtualIndex(index as u64 + 3) + VirtualIndex(index as u64) } pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>( @@ -77,43 +73,38 @@ pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>( // Not in the cache; build it. let nullptr = cx.const_null(cx.type_i8p_ext(cx.data_layout().instruction_address_space)); - let methods_root; - let methods = if let Some(trait_ref) = trait_ref { - methods_root = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty)); - methods_root.iter() + let vtable_entries = if let Some(trait_ref) = trait_ref { + tcx.vtable_entries(trait_ref.with_self_ty(tcx, ty)) } else { - (&[]).iter() + COMMON_VTABLE_ENTRIES }; - let methods = methods.cloned().map(|opt_mth| { - opt_mth.map_or(nullptr, |(def_id, substs)| { - cx.get_fn_addr( + let layout = cx.layout_of(ty); + // ///////////////////////////////////////////////////////////////////////////////////////////// + // If you touch this code, be sure to also make the corresponding changes to + // `get_vtable` in `rust_mir/interpret/traits.rs`. + // ///////////////////////////////////////////////////////////////////////////////////////////// + let components: Vec<_> = vtable_entries + .iter() + .map(|entry| match entry { + VtblEntry::MetadataDropInPlace => { + cx.get_fn_addr(Instance::resolve_drop_in_place(cx.tcx(), ty)) + } + VtblEntry::MetadataSize => cx.const_usize(layout.size.bytes()), + VtblEntry::MetadataAlign => cx.const_usize(layout.align.abi.bytes()), + VtblEntry::Vacant => nullptr, + VtblEntry::Method(def_id, substs) => cx.get_fn_addr( ty::Instance::resolve_for_vtable( cx.tcx(), ty::ParamEnv::reveal_all(), - def_id, + *def_id, substs, ) .unwrap() .polymorphize(cx.tcx()), - ) + ), }) - }); - - let layout = cx.layout_of(ty); - // ///////////////////////////////////////////////////////////////////////////////////////////// - // If you touch this code, be sure to also make the corresponding changes to - // `get_vtable` in `rust_mir/interpret/traits.rs`. - // ///////////////////////////////////////////////////////////////////////////////////////////// - let components: Vec<_> = [ - cx.get_fn_addr(Instance::resolve_drop_in_place(cx.tcx(), ty)), - cx.const_usize(layout.size.bytes()), - cx.const_usize(layout.align.abi.bytes()), - ] - .iter() - .cloned() - .chain(methods) - .collect(); + .collect(); let vtable_const = cx.const_struct(&components, false); let align = cx.data_layout().pointer_align.abi; diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index 93200bd1f26..2cb28d7361c 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -332,7 +332,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let fn_abi = FnAbi::of_instance(&bx, virtual_drop, &[]); let vtable = args[1]; args = &args[..1]; - (meth::DESTRUCTOR.get_fn(&mut bx, vtable, &fn_abi), fn_abi) + ( + meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_DROPINPLACE) + .get_fn(&mut bx, vtable, &fn_abi), + fn_abi, + ) } _ => (bx.get_fn_addr(drop_fn), FnAbi::of_instance(&bx, drop_fn, &[])), }; |
