diff options
| author | Denis Merigoux <denis.merigoux@gmail.com> | 2018-10-03 13:49:57 +0200 |
|---|---|---|
| committer | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2018-11-16 15:07:24 +0200 |
| commit | c0a428ee702329b0ad818a67a6ecc9617df267c7 (patch) | |
| tree | c47602d70ccd0f3d54f47e29730e880751afada4 /src/librustc_codegen_ssa/meth.rs | |
| parent | 915382f7306be7841c4254cee13fa55a865bdd8b (diff) | |
| download | rust-c0a428ee702329b0ad818a67a6ecc9617df267c7.tar.gz rust-c0a428ee702329b0ad818a67a6ecc9617df267c7.zip | |
Great separation of librustc_codegen_llvm: librustc_codegen_ssa compiles
Diffstat (limited to 'src/librustc_codegen_ssa/meth.rs')
| -rw-r--r-- | src/librustc_codegen_ssa/meth.rs | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/librustc_codegen_ssa/meth.rs b/src/librustc_codegen_ssa/meth.rs new file mode 100644 index 00000000000..4584adfff65 --- /dev/null +++ b/src/librustc_codegen_ssa/meth.rs @@ -0,0 +1,126 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use rustc_target::abi::call::FnType; +use callee; +use rustc_mir::monomorphize; + +use interfaces::*; + +use rustc::ty::{self, Ty}; + +#[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: 'a> VirtualIndex { + pub fn from_index(index: usize) -> Self { + VirtualIndex(index as u64 + 3) + } + + pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>( + self, + bx: &Bx, + llvtable: Bx::Value, + fn_ty: &FnType<'tcx, Ty<'tcx>> + ) -> Bx::Value { + // Load the data pointer from the object. + debug!("get_fn({:?}, {:?})", llvtable, self); + + let llvtable = bx.pointercast( + llvtable, + bx.cx().type_ptr_to(bx.cx().fn_ptr_backend_type(fn_ty)) + ); + let ptr_align = bx.tcx().data_layout.pointer_align; + let ptr = bx.load( + bx.inbounds_gep(llvtable, &[bx.cx().const_usize(self.0)]), + ptr_align + ); + bx.nonnull_metadata(ptr); + // Vtable loads are invariant + bx.set_invariant_load(ptr); + ptr + } + + pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>( + self, + bx: &Bx, + llvtable: Bx::Value + ) -> Bx::Value { + // Load the data pointer from the object. + debug!("get_int({:?}, {:?})", llvtable, self); + + let llvtable = bx.pointercast(llvtable, bx.cx().type_ptr_to(bx.cx().type_isize())); + let usize_align = bx.tcx().data_layout.pointer_align; + let ptr = bx.load( + bx.inbounds_gep(llvtable, &[bx.cx().const_usize(self.0)]), + usize_align + ); + // Vtable loads are invariant + bx.set_invariant_load(ptr); + ptr + } +} + +/// Creates a dynamic vtable for the given type and vtable origin. +/// This is used only for objects. +/// +/// The vtables are cached instead of created on every call. +/// +/// The `trait_ref` encodes the erased self type. Hence if we are +/// making an object `Foo<Trait>` from a value of type `Foo<T>`, then +/// `trait_ref` would map `T:Trait`. +pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>( + cx: &Cx, + ty: Ty<'tcx>, + trait_ref: ty::PolyExistentialTraitRef<'tcx>, +) -> Cx::Value { + let tcx = cx.tcx(); + + debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref); + + // Check the cache. + if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) { + return val; + } + + // Not in the cache. Build it. + let nullptr = cx.const_null(cx.type_i8p()); + + let methods = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty)); + let methods = methods.iter().cloned().map(|opt_mth| { + opt_mth.map_or(nullptr, |(def_id, substs)| { + callee::resolve_and_get_fn_for_vtable(cx, def_id, substs) + }) + }); + + let (size, align) = cx.layout_of(ty).size_and_align(); + // ///////////////////////////////////////////////////////////////////////////////////////////// + // 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(monomorphize::resolve_drop_in_place(cx.tcx(), ty)), + cx.const_usize(size.bytes()), + cx.const_usize(align.abi()) + ].iter().cloned().chain(methods).collect(); + + let vtable_const = cx.const_struct(&components, false); + let align = cx.data_layout().pointer_align; + let vtable = cx.static_addr_of(vtable_const, align, Some("vtable")); + + cx.create_vtable_metadata(ty, vtable); + + cx.vtables().borrow_mut().insert((ty, trait_ref), vtable); + vtable +} |
