]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_codegen_cranelift/src/vtable.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / compiler / rustc_codegen_cranelift / src / vtable.rs
1 //! Codegen vtables and vtable accesses.
2 //!
3 //! See librustc_codegen_llvm/meth.rs for reference
4 // FIXME dedup this logic between miri, cg_llvm and cg_clif
5
6 use crate::prelude::*;
7
8 const DROP_FN_INDEX: usize = 0;
9 const SIZE_INDEX: usize = 1;
10 const ALIGN_INDEX: usize = 2;
11
12 fn vtable_memflags() -> MemFlags {
13 let mut flags = MemFlags::trusted(); // A vtable access is always aligned and will never trap.
14 flags.set_readonly(); // A vtable is always read-only.
15 flags
16 }
17
18 pub(crate) fn drop_fn_of_obj(fx: &mut FunctionCx<'_, '_, impl Module>, vtable: Value) -> Value {
19 let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize;
20 fx.bcx.ins().load(
21 pointer_ty(fx.tcx),
22 vtable_memflags(),
23 vtable,
24 (DROP_FN_INDEX * usize_size) as i32,
25 )
26 }
27
28 pub(crate) fn size_of_obj(fx: &mut FunctionCx<'_, '_, impl Module>, vtable: Value) -> Value {
29 let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize;
30 fx.bcx.ins().load(
31 pointer_ty(fx.tcx),
32 vtable_memflags(),
33 vtable,
34 (SIZE_INDEX * usize_size) as i32,
35 )
36 }
37
38 pub(crate) fn min_align_of_obj(fx: &mut FunctionCx<'_, '_, impl Module>, vtable: Value) -> Value {
39 let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize;
40 fx.bcx.ins().load(
41 pointer_ty(fx.tcx),
42 vtable_memflags(),
43 vtable,
44 (ALIGN_INDEX * usize_size) as i32,
45 )
46 }
47
48 pub(crate) fn get_ptr_and_method_ref<'tcx>(
49 fx: &mut FunctionCx<'_, 'tcx, impl Module>,
50 arg: CValue<'tcx>,
51 idx: usize,
52 ) -> (Value, Value) {
53 let (ptr, vtable) = if let Abi::ScalarPair(_, _) = arg.layout().abi {
54 arg.load_scalar_pair(fx)
55 } else {
56 let (ptr, vtable) = arg.try_to_ptr().unwrap();
57 (ptr.get_addr(fx), vtable.unwrap())
58 };
59
60 let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes();
61 let func_ref = fx.bcx.ins().load(
62 pointer_ty(fx.tcx),
63 vtable_memflags(),
64 vtable,
65 ((idx + 3) * usize_size as usize) as i32,
66 );
67 (ptr, func_ref)
68 }
69
70 pub(crate) fn get_vtable<'tcx>(
71 fx: &mut FunctionCx<'_, 'tcx, impl Module>,
72 layout: TyAndLayout<'tcx>,
73 trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
74 ) -> Value {
75 let data_id = if let Some(data_id) = fx.cx.vtables.get(&(layout.ty, trait_ref)) {
76 *data_id
77 } else {
78 let data_id = build_vtable(fx, layout, trait_ref);
79 fx.cx.vtables.insert((layout.ty, trait_ref), data_id);
80 data_id
81 };
82
83 let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
84 fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
85 }
86
87 fn build_vtable<'tcx>(
88 fx: &mut FunctionCx<'_, 'tcx, impl Module>,
89 layout: TyAndLayout<'tcx>,
90 trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
91 ) -> DataId {
92 let tcx = fx.tcx;
93 let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize;
94
95 let drop_in_place_fn = import_function(
96 tcx,
97 &mut fx.cx.module,
98 Instance::resolve_drop_in_place(tcx, layout.ty).polymorphize(fx.tcx),
99 );
100
101 let mut components: Vec<_> = vec![Some(drop_in_place_fn), None, None];
102
103 let methods_root;
104 let methods = if let Some(trait_ref) = trait_ref {
105 methods_root = tcx.vtable_methods(trait_ref.with_self_ty(tcx, layout.ty));
106 methods_root.iter()
107 } else {
108 (&[]).iter()
109 };
110 let methods = methods.cloned().map(|opt_mth| {
111 opt_mth.map(|(def_id, substs)| {
112 import_function(
113 tcx,
114 &mut fx.cx.module,
115 Instance::resolve_for_vtable(tcx, ParamEnv::reveal_all(), def_id, substs)
116 .unwrap()
117 .polymorphize(fx.tcx),
118 )
119 })
120 });
121 components.extend(methods);
122
123 let mut data_ctx = DataContext::new();
124 let mut data = ::std::iter::repeat(0u8)
125 .take(components.len() * usize_size)
126 .collect::<Vec<u8>>()
127 .into_boxed_slice();
128
129 write_usize(fx.tcx, &mut data, SIZE_INDEX, layout.size.bytes());
130 write_usize(fx.tcx, &mut data, ALIGN_INDEX, layout.align.abi.bytes());
131 data_ctx.define(data);
132
133 for (i, component) in components.into_iter().enumerate() {
134 if let Some(func_id) = component {
135 let func_ref = fx.cx.module.declare_func_in_data(func_id, &mut data_ctx);
136 data_ctx.write_function_addr((i * usize_size) as u32, func_ref);
137 }
138 }
139
140 data_ctx.set_align(fx.tcx.data_layout.pointer_align.pref.bytes());
141
142 let data_id = fx
143 .cx
144 .module
145 .declare_data(
146 &format!(
147 "__vtable.{}.for.{:?}.{}",
148 trait_ref
149 .as_ref()
150 .map(|trait_ref| format!("{:?}", trait_ref.skip_binder()).into())
151 .unwrap_or(std::borrow::Cow::Borrowed("???")),
152 layout.ty,
153 fx.cx.vtables.len(),
154 ),
155 Linkage::Local,
156 false,
157 false,
158 )
159 .unwrap();
160
161 // FIXME don't duplicate definitions in lazy jit mode
162 let _ = fx.cx.module.define_data(data_id, &data_ctx);
163
164 data_id
165 }
166
167 fn write_usize(tcx: TyCtxt<'_>, buf: &mut [u8], idx: usize, num: u64) {
168 let pointer_size = tcx
169 .layout_of(ParamEnv::reveal_all().and(tcx.types.usize))
170 .unwrap()
171 .size
172 .bytes() as usize;
173 let target = &mut buf[idx * pointer_size..(idx + 1) * pointer_size];
174
175 match tcx.data_layout.endian {
176 rustc_target::abi::Endian::Little => match pointer_size {
177 4 => target.copy_from_slice(&(num as u32).to_le_bytes()),
178 8 => target.copy_from_slice(&(num as u64).to_le_bytes()),
179 _ => todo!("pointer size {} is not yet supported", pointer_size),
180 },
181 rustc_target::abi::Endian::Big => match pointer_size {
182 4 => target.copy_from_slice(&(num as u32).to_be_bytes()),
183 8 => target.copy_from_slice(&(num as u64).to_be_bytes()),
184 _ => todo!("pointer size {} is not yet supported", pointer_size),
185 },
186 }
187 }