]> git.lizzy.rs Git - rust.git/blob - src/vtable.rs
Rustup to rustc 1.30.0-nightly (cb6d2dfa8 2018-09-16)
[rust.git] / src / vtable.rs
1 //! See librustc_codegen_llvm/meth.rs for reference
2
3 use crate::prelude::*;
4
5 const DROP_FN_INDEX: usize = 0;
6 const SIZE_INDEX: usize = 1;
7 const ALIGN_INDEX: usize = 2;
8
9 pub fn size_of_obj<'a, 'tcx: 'a>(
10     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
11     val: CValue<'tcx>,
12 ) -> Value {
13     let (_ptr, vtable) = val.load_value_pair(fx);
14     let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize;
15     fx.bcx.ins().load(
16         pointer_ty(fx.tcx),
17         MemFlags::new(),
18         vtable,
19         (SIZE_INDEX * usize_size) as i32,
20     )
21 }
22
23 pub fn min_align_of_obj<'a, 'tcx: 'a>(
24     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
25     val: CValue<'tcx>,
26 ) -> Value {
27     let (_ptr, vtable) = val.load_value_pair(fx);
28     let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize;
29     fx.bcx.ins().load(
30         pointer_ty(fx.tcx),
31         MemFlags::new(),
32         vtable,
33         (ALIGN_INDEX * usize_size) as i32,
34     )
35 }
36
37 pub fn get_ptr_and_method_ref<'a, 'tcx: 'a>(
38     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
39     arg: CValue<'tcx>,
40     idx: usize,
41 ) -> (Value, Value) {
42     let (ptr, vtable) = arg.load_value_pair(fx);
43     let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes();
44     let func_ref = fx.bcx.ins().load(
45         pointer_ty(fx.tcx),
46         MemFlags::new(),
47         vtable,
48         ((idx + 3) * usize_size as usize) as i32,
49     );
50     (ptr, func_ref)
51 }
52
53 pub fn get_vtable<'a, 'tcx: 'a>(
54     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
55     ty: Ty<'tcx>,
56     trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
57 ) -> Value {
58     let data_id = if let Some(data_id) = fx.caches.vtables.get(&(ty, trait_ref)) {
59         *data_id
60     } else {
61         let data_id = build_vtable(fx, ty, trait_ref);
62         fx.caches.vtables.insert((ty, trait_ref), data_id);
63         data_id
64     };
65
66     let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
67     fx.bcx
68         .ins()
69         .global_value(fx.module.pointer_type(), local_data_id)
70 }
71
72 fn build_vtable<'a, 'tcx: 'a>(
73     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
74     ty: Ty<'tcx>,
75     trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
76 ) -> DataId {
77     let tcx = fx.tcx;
78     let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize;
79
80     let (size, align) = tcx
81         .layout_of(ParamEnv::reveal_all().and(ty))
82         .unwrap()
83         .size_and_align();
84     let drop_in_place_fn =
85         fx.get_function_id(crate::rustc_mir::monomorphize::resolve_drop_in_place(tcx, ty));
86
87     let mut components: Vec<_> = vec![Some(drop_in_place_fn), None, None];
88
89     if let Some(trait_ref) = trait_ref {
90         let trait_ref = trait_ref.with_self_ty(tcx, ty);
91         let methods = tcx.vtable_methods(trait_ref);
92         let methods = methods.iter().cloned().map(|opt_mth| {
93             opt_mth.map_or(None, |(def_id, substs)| {
94                 Some(fx.get_function_id(
95                     Instance::resolve(tcx, ParamEnv::reveal_all(), def_id, substs).unwrap(),
96                 ))
97             })
98         });
99         components.extend(methods);
100     }
101
102     let mut data_ctx = DataContext::new();
103     let mut data = ::std::iter::repeat(0u8)
104         .take(components.len() * usize_size)
105         .collect::<Vec<u8>>()
106         .into_boxed_slice();
107     write_usize(fx.tcx, &mut data, SIZE_INDEX, size.bytes());
108     write_usize(fx.tcx, &mut data, ALIGN_INDEX, align.abi());
109     data_ctx.define(data);
110
111     for (i, component) in components.into_iter().enumerate() {
112         if let Some(func_id) = component {
113             let func_ref = fx.module.declare_func_in_data(func_id, &mut data_ctx);
114             data_ctx.write_function_addr((i * usize_size) as u32, func_ref);
115         }
116     }
117
118     let data_id = fx
119         .module
120         .declare_data(
121             &format!("vtable.{:?}.for.{:?}", trait_ref, ty),
122             Linkage::Local,
123             false,
124         ).unwrap();
125     fx.module.define_data(data_id, &data_ctx).unwrap();
126     data_id
127 }
128
129 fn write_usize(tcx: TyCtxt, buf: &mut [u8], idx: usize, num: u64) {
130     use byteorder::{BigEndian, LittleEndian, WriteBytesExt};
131
132     let usize_size = tcx
133         .layout_of(ParamEnv::reveal_all().and(tcx.types.usize))
134         .unwrap()
135         .size
136         .bytes() as usize;
137     let mut target = &mut buf[idx * usize_size..(idx + 1) * usize_size];
138
139     match tcx.data_layout.endian {
140         layout::Endian::Little => target.write_uint::<LittleEndian>(num, usize_size),
141         layout::Endian::Big => target.write_uint::<BigEndian>(num, usize_size),
142     }.unwrap()
143 }