1 //! Codegen vtables and vtable accesses.
3 //! See `rustc_codegen_ssa/src/meth.rs` for reference.
5 use crate::constant::data_id_for_alloc_id;
8 pub(crate) fn vtable_memflags() -> MemFlags {
9 let mut flags = MemFlags::trusted(); // A vtable access is always aligned and will never trap.
10 flags.set_readonly(); // A vtable is always read-only.
14 pub(crate) fn drop_fn_of_obj(fx: &mut FunctionCx<'_, '_, '_>, vtable: Value) -> Value {
15 let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize;
20 (ty::COMMON_VTABLE_ENTRIES_DROPINPLACE * usize_size) as i32,
24 pub(crate) fn size_of_obj(fx: &mut FunctionCx<'_, '_, '_>, vtable: Value) -> Value {
25 let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize;
30 (ty::COMMON_VTABLE_ENTRIES_SIZE * usize_size) as i32,
34 pub(crate) fn min_align_of_obj(fx: &mut FunctionCx<'_, '_, '_>, vtable: Value) -> Value {
35 let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize;
40 (ty::COMMON_VTABLE_ENTRIES_ALIGN * usize_size) as i32,
44 pub(crate) fn get_ptr_and_method_ref<'tcx>(
45 fx: &mut FunctionCx<'_, '_, 'tcx>,
49 let (ptr, vtable) = if let Abi::ScalarPair(_, _) = arg.layout().abi {
50 arg.load_scalar_pair(fx)
52 let (ptr, vtable) = arg.try_to_ptr().unwrap();
53 (ptr.get_addr(fx), vtable.unwrap())
56 let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes();
57 let func_ref = fx.bcx.ins().load(
61 (idx * usize_size as usize) as i32,
66 pub(crate) fn get_vtable<'tcx>(
67 fx: &mut FunctionCx<'_, '_, 'tcx>,
69 trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
71 let alloc_id = fx.tcx.vtable_allocation((ty, trait_ref));
73 data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, Mutability::Not);
74 let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
75 if fx.clif_comments.enabled() {
76 fx.add_comment(local_data_id, format!("vtable: {:?}", alloc_id));
78 fx.bcx.ins().global_value(fx.pointer_type, local_data_id)