]> git.lizzy.rs Git - rust.git/blob - src/vtable.rs
Rustfmt
[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 = fx.get_function_id(
85         crate::rustc_mir::monomorphize::resolve_drop_in_place(tcx, ty),
86     );
87
88     let mut components: Vec<_> = vec![Some(drop_in_place_fn), None, None];
89
90     if let Some(trait_ref) = trait_ref {
91         let trait_ref = trait_ref.with_self_ty(tcx, ty);
92         let methods = tcx.vtable_methods(trait_ref);
93         let methods = methods.iter().cloned().map(|opt_mth| {
94             opt_mth.map_or(None, |(def_id, substs)| {
95                 Some(fx.get_function_id(
96                     Instance::resolve(tcx, ParamEnv::reveal_all(), def_id, substs).unwrap(),
97                 ))
98             })
99         });
100         components.extend(methods);
101     }
102
103     let mut data_ctx = DataContext::new();
104     let mut data = ::std::iter::repeat(0u8)
105         .take(components.len() * usize_size)
106         .collect::<Vec<u8>>()
107         .into_boxed_slice();
108     write_usize(fx.tcx, &mut data, SIZE_INDEX, size.bytes());
109     write_usize(fx.tcx, &mut data, ALIGN_INDEX, align.abi());
110     data_ctx.define(data);
111
112     for (i, component) in components.into_iter().enumerate() {
113         if let Some(func_id) = component {
114             let func_ref = fx.module.declare_func_in_data(func_id, &mut data_ctx);
115             data_ctx.write_function_addr((i * usize_size) as u32, func_ref);
116         }
117     }
118
119     let data_id = fx
120         .module
121         .declare_data(
122             &format!("vtable.{:?}.for.{:?}", trait_ref, ty),
123             Linkage::Local,
124             false,
125         ).unwrap();
126     fx.module.define_data(data_id, &data_ctx).unwrap();
127     data_id
128 }
129
130 fn write_usize(tcx: TyCtxt, buf: &mut [u8], idx: usize, num: u64) {
131     use byteorder::{BigEndian, LittleEndian, WriteBytesExt};
132
133     let usize_size = tcx
134         .layout_of(ParamEnv::reveal_all().and(tcx.types.usize))
135         .unwrap()
136         .size
137         .bytes() as usize;
138     let mut target = &mut buf[idx * usize_size..(idx + 1) * usize_size];
139
140     match tcx.data_layout.endian {
141         layout::Endian::Little => target.write_uint::<LittleEndian>(num, usize_size),
142         layout::Endian::Big => target.write_uint::<BigEndian>(num, usize_size),
143     }.unwrap()
144 }