Skip to content

Commit

Permalink
Generalized base::unsized_info
Browse files Browse the repository at this point in the history
  • Loading branch information
denismerigoux authored and eddyb committed Nov 16, 2018
1 parent 484e07c commit 034f697
Show file tree
Hide file tree
Showing 26 changed files with 341 additions and 290 deletions.
19 changes: 9 additions & 10 deletions src/librustc_codegen_llvm/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ use builder::{Builder, MemFlags};
use callee;
use rustc_mir::monomorphize::item::DefPathBasedNames;
use common::{self, IntPredicate, RealPredicate, TypeKind};
use consts;
use context::CodegenCx;
use debuginfo;
use declare;
Expand Down Expand Up @@ -188,16 +187,16 @@ pub fn compare_simd_types<'a, 'tcx: 'a, Builder: BuilderMethods<'a, 'tcx>>(
/// The `old_info` argument is a bit funny. It is intended for use
/// in an upcast, where the new vtable for an object will be derived
/// from the old one.
pub fn unsized_info(
cx: &CodegenCx<'ll, 'tcx>,
pub fn unsized_info<'tcx, Cx: CodegenMethods<'tcx>>(
cx: &Cx,
source: Ty<'tcx>,
target: Ty<'tcx>,
old_info: Option<&'ll Value>,
) -> &'ll Value {
let (source, target) = cx.tcx.struct_lockstep_tails(source, target);
old_info: Option<Cx::Value>,
) -> Cx::Value {
let (source, target) = cx.tcx().struct_lockstep_tails(source, target);
match (&source.sty, &target.sty) {
(&ty::Array(_, len), &ty::Slice(_)) => {
cx.const_usize(len.unwrap_usize(cx.tcx))
cx.const_usize(len.unwrap_usize(cx.tcx()))
}
(&ty::Dynamic(..), &ty::Dynamic(..)) => {
// For now, upcasts are limited to changes in marker
Expand All @@ -206,10 +205,10 @@ pub fn unsized_info(
old_info.expect("unsized_info: missing old info for trait upcast")
}
(_, &ty::Dynamic(ref data, ..)) => {
let vtable_ptr = cx.layout_of(cx.tcx.mk_mut_ptr(target))
let vtable_ptr = cx.layout_of(cx.tcx().mk_mut_ptr(target))
.field(cx, abi::FAT_PTR_EXTRA);
consts::ptrcast(meth::get_vtable(cx, source, data.principal()),
vtable_ptr.llvm_type(cx))
cx.static_ptrcast(meth::get_vtable(cx, source, data.principal()),
cx.backend_type(vtable_ptr))
}
_ => bug!("unsized_info: invalid unsizing {:?} -> {:?}",
source,
Expand Down
39 changes: 32 additions & 7 deletions src/librustc_codegen_llvm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use context::CodegenCx;
use type_::Type;
use value::Value;
use libc::{c_uint, c_char};
use rustc::ty::TyCtxt;
use rustc::ty::layout::{Align, Size};
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::layout::{Align, Size, TyLayout};
use rustc::session::{config, Session};
use rustc_data_structures::small_c_str::SmallCStr;
use interfaces::*;
Expand Down Expand Up @@ -56,7 +56,36 @@ bitflags! {
}
}

impl HasCodegen for Builder<'a, 'll, 'tcx> {
impl BackendTypes for Builder<'_, 'll, '_> {
type Value = &'ll Value;
type BasicBlock = &'ll BasicBlock;
type Type = &'ll Type;
type Context = &'ll llvm::Context;
}

impl ty::layout::HasDataLayout for Builder<'_, '_, '_> {
fn data_layout(&self) -> &ty::layout::TargetDataLayout {
self.cx.data_layout()
}
}

impl ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
self.cx.tcx
}
}

impl ty::layout::LayoutOf for Builder<'_, '_, 'tcx> {
type Ty = Ty<'tcx>;
type TyLayout = TyLayout<'tcx>;

fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
self.cx.layout_of(ty)
}
}


impl HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
type CodegenCx = CodegenCx<'ll, 'tcx>;
}

Expand Down Expand Up @@ -98,10 +127,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
self.cx.sess()
}

fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> {
self.cx.tcx
}

fn llfn(&self) -> &'ll Value {
unsafe {
llvm::LLVMGetBasicBlockParent(self.llbb())
Expand Down
28 changes: 15 additions & 13 deletions src/librustc_codegen_llvm/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use llvm;
use monomorphize::Instance;
use type_of::LayoutLlvmExt;
use value::Value;
use interfaces::BaseTypeMethods;
use interfaces::*;

use rustc::hir::def_id::DefId;
use rustc::ty::{self, TypeFoldable};
Expand Down Expand Up @@ -206,31 +206,33 @@ pub fn get_fn(
llfn
}

pub fn resolve_and_get_fn(
cx: &CodegenCx<'ll, 'tcx>,
pub fn resolve_and_get_fn<'tcx,
Cx: Backend<'tcx> + MiscMethods<'tcx> + TypeMethods<'tcx>
>(
cx: &Cx,
def_id: DefId,
substs: &'tcx Substs<'tcx>,
) -> &'ll Value {
get_fn(
cx,
) -> Cx::Value {
cx.get_fn(
ty::Instance::resolve(
cx.tcx,
cx.tcx(),
ty::ParamEnv::reveal_all(),
def_id,
substs
).unwrap()
)
}

pub fn resolve_and_get_fn_for_vtable(
cx: &CodegenCx<'ll, 'tcx>,
pub fn resolve_and_get_fn_for_vtable<'tcx,
Cx: Backend<'tcx> + MiscMethods<'tcx> + TypeMethods<'tcx>
>(
cx: &Cx,
def_id: DefId,
substs: &'tcx Substs<'tcx>,
) -> &'ll Value {
get_fn(
cx,
) -> Cx::Value {
cx.get_fn(
ty::Instance::resolve_for_vtable(
cx.tcx,
cx.tcx(),
ty::ParamEnv::reveal_all(),
def_id,
substs
Expand Down
10 changes: 4 additions & 6 deletions src/librustc_codegen_llvm/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ use declare;
use type_::Type;
use type_of::LayoutLlvmExt;
use value::Value;
use interfaces::{Backend, ConstMethods, BaseTypeMethods};
use interfaces::{BackendTypes, BuilderMethods, ConstMethods, BaseTypeMethods};

use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::layout::{HasDataLayout, LayoutOf};
use rustc::hir;
use interfaces::BuilderMethods;

use libc::{c_uint, c_char};

Expand Down Expand Up @@ -213,15 +212,14 @@ impl Funclet<'ll> {
}
}

impl Backend for CodegenCx<'ll, 'tcx> {
impl BackendTypes for CodegenCx<'ll, 'tcx> {
type Value = &'ll Value;
type BasicBlock = &'ll BasicBlock;
type Type = &'ll Type;
type Context = &'ll llvm::Context;
}

impl<'ll, 'tcx: 'll> ConstMethods for CodegenCx<'ll, 'tcx> {

impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
// LLVM constant constructors.
fn const_null(&self, t: &'ll Type) -> &'ll Value {
unsafe {
Expand Down Expand Up @@ -319,7 +317,7 @@ impl<'ll, 'tcx: 'll> ConstMethods for CodegenCx<'ll, 'tcx> {
fn const_str_slice(&self, s: LocalInternedString) -> &'ll Value {
let len = s.len();
let cs = consts::ptrcast(self.const_cstr(s, false),
self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(&self)));
self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(self)));
self.const_fat_ptr(cs, self.const_usize(len as u64))
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl StaticMethods<'tcx> for CodegenCx<'ll, 'tcx> {

let g = if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {

let llty = self.layout_of(ty).llvm_type(&self);
let llty = self.layout_of(ty).llvm_type(self);
let (g, attrs) = match self.tcx.hir.get(id) {
Node::Item(&hir::Item {
ref attrs, span, node: hir::ItemKind::Static(..), ..
Expand Down Expand Up @@ -329,7 +329,7 @@ impl StaticMethods<'tcx> for CodegenCx<'ll, 'tcx> {

let instance = Instance::mono(self.tcx, def_id);
let ty = instance.ty(self.tcx);
let llty = self.layout_of(ty).llvm_type(&self);
let llty = self.layout_of(ty).llvm_type(self);
let g = if val_llty == llty {
g
} else {
Expand Down
15 changes: 13 additions & 2 deletions src/librustc_codegen_llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use value::Value;
use monomorphize::partitioning::CodegenUnit;
use type_::Type;
use type_of::PointeeInfo;
use interfaces::{BaseTypeMethods, DerivedTypeMethods, IntrinsicDeclarationMethods};
use interfaces::*;

use rustc_data_structures::base_n;
use rustc_data_structures::small_c_str::SmallCStr;
Expand Down Expand Up @@ -322,7 +322,18 @@ impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
}
}

impl IntrinsicDeclarationMethods for CodegenCx<'b, 'tcx> {
impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
fn vtables(&self) -> &RefCell<FxHashMap<(Ty<'tcx>,
ty::PolyExistentialTraitRef<'tcx>), &'ll Value>>
{
&self.vtables
}
fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
callee::get_fn(&&self,instance)
}
}

impl IntrinsicDeclarationMethods<'tcx> for CodegenCx<'b, 'tcx> {
fn get_intrinsic(&self, key: &str) -> &'b Value {
if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
return v;
Expand Down
105 changes: 54 additions & 51 deletions src/librustc_codegen_llvm/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use super::utils::{debug_context, DIB, span_start,
use super::namespace::mangled_name_of_instance;
use super::type_names::compute_debuginfo_type_name;
use super::{CrateDebugContext};
use interfaces::*;
use abi;
use interfaces::ConstMethods;
use value::Value;
Expand Down Expand Up @@ -1983,58 +1984,60 @@ pub fn extend_scope_to_file(
}
}

/// Creates debug information for the given vtable, which is for the
/// given type.
///
/// Adds the created metadata nodes directly to the crate's IR.
pub fn create_vtable_metadata(
cx: &CodegenCx<'ll, 'tcx>,
ty: ty::Ty<'tcx>,
vtable: &'ll Value,
) {
if cx.dbg_cx.is_none() {
return;
}

let type_metadata = type_metadata(cx, ty, syntax_pos::DUMMY_SP);

unsafe {
// LLVMRustDIBuilderCreateStructType() wants an empty array. A null
// pointer will lead to hard to trace and debug LLVM assertions
// later on in llvm/lib/IR/Value.cpp.
let empty_array = create_DIArray(DIB(cx), &[]);

let name = const_cstr!("vtable");
impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
/// Creates debug information for the given vtable, which is for the
/// given type.
///
/// Adds the created metadata nodes directly to the crate's IR.
fn create_vtable_metadata(
&self,
ty: ty::Ty<'tcx>,
vtable: &'ll Value,
) {
if self.dbg_cx.is_none() {
return;
}

// Create a new one each time. We don't want metadata caching
// here, because each vtable will refer to a unique containing
// type.
let vtable_type = llvm::LLVMRustDIBuilderCreateStructType(
DIB(cx),
NO_SCOPE_METADATA,
name.as_ptr(),
unknown_file_metadata(cx),
UNKNOWN_LINE_NUMBER,
Size::ZERO.bits(),
cx.tcx.data_layout.pointer_align.abi_bits() as u32,
DIFlags::FlagArtificial,
None,
empty_array,
0,
Some(type_metadata),
name.as_ptr()
);
let type_metadata = type_metadata(&self, ty, syntax_pos::DUMMY_SP);

llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx),
NO_SCOPE_METADATA,
name.as_ptr(),
ptr::null(),
unknown_file_metadata(cx),
UNKNOWN_LINE_NUMBER,
vtable_type,
true,
vtable,
None,
0);
unsafe {
// LLVMRustDIBuilderCreateStructType() wants an empty array. A null
// pointer will lead to hard to trace and debug LLVM assertions
// later on in llvm/lib/IR/Value.cpp.
let empty_array = create_DIArray(DIB(&self), &[]);

let name = const_cstr!("vtable");

// Create a new one each time. We don't want metadata caching
// here, because each vtable will refer to a unique containing
// type.
let vtable_type = llvm::LLVMRustDIBuilderCreateStructType(
DIB(&self),
NO_SCOPE_METADATA,
name.as_ptr(),
unknown_file_metadata(&self),
UNKNOWN_LINE_NUMBER,
Size::ZERO.bits(),
self.tcx.data_layout.pointer_align.abi_bits() as u32,
DIFlags::FlagArtificial,
None,
empty_array,
0,
Some(type_metadata),
name.as_ptr()
);

llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(&self),
NO_SCOPE_METADATA,
name.as_ptr(),
ptr::null(),
unknown_file_metadata(&self),
UNKNOWN_LINE_NUMBER,
vtable_type,
true,
vtable,
None,
0);
}
}
}
1 change: 0 additions & 1 deletion src/librustc_codegen_llvm/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ mod source_loc;
pub use self::create_scope_map::{create_mir_scopes, MirDebugScope};
pub use self::source_loc::start_emitting_source_locations;
pub use self::metadata::create_global_var_metadata;
pub use self::metadata::create_vtable_metadata;
pub use self::metadata::extend_scope_to_file;
pub use self::source_loc::set_source_location;

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std;
use builder::Builder;
use common::*;
use meth;
use rustc::ty::layout::LayoutOf;
use rustc::ty::layout::{LayoutOf, HasTyCtxt};
use rustc::ty::{self, Ty};
use value::Value;
use interfaces::{BuilderMethods, ConstMethods};
Expand Down
Loading

0 comments on commit 034f697

Please sign in to comment.