slightly cleaner, if more verbose, vtable handling in codegen backends
This commit is contained in:
parent
3dad266f40
commit
adf6d37d83
3 changed files with 28 additions and 22 deletions
|
@ -195,11 +195,6 @@ pub(crate) fn codegen_const_value<'tcx>(
|
||||||
}
|
}
|
||||||
Scalar::Ptr(ptr, _size) => {
|
Scalar::Ptr(ptr, _size) => {
|
||||||
let (alloc_id, offset) = ptr.into_parts(); // we know the `offset` is relative
|
let (alloc_id, offset) = ptr.into_parts(); // we know the `offset` is relative
|
||||||
// For vtables, get the underlying data allocation.
|
|
||||||
let alloc_id = match fx.tcx.global_alloc(alloc_id) {
|
|
||||||
GlobalAlloc::VTable(ty, trait_ref) => fx.tcx.vtable_allocation((ty, trait_ref)),
|
|
||||||
_ => alloc_id,
|
|
||||||
};
|
|
||||||
let base_addr = match fx.tcx.global_alloc(alloc_id) {
|
let base_addr = match fx.tcx.global_alloc(alloc_id) {
|
||||||
GlobalAlloc::Memory(alloc) => {
|
GlobalAlloc::Memory(alloc) => {
|
||||||
let data_id = data_id_for_alloc_id(
|
let data_id = data_id_for_alloc_id(
|
||||||
|
@ -221,7 +216,20 @@ pub(crate) fn codegen_const_value<'tcx>(
|
||||||
fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
|
fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
|
||||||
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
|
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
|
||||||
}
|
}
|
||||||
GlobalAlloc::VTable(..) => bug!("vtables are already handled"),
|
GlobalAlloc::VTable(ty, trait_ref) => {
|
||||||
|
let alloc_id = fx.tcx.vtable_allocation((ty, trait_ref));
|
||||||
|
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
|
||||||
|
// FIXME: factor this common code with the `Memory` arm into a function?
|
||||||
|
let data_id = data_id_for_alloc_id(
|
||||||
|
&mut fx.constants_cx,
|
||||||
|
fx.module,
|
||||||
|
alloc_id,
|
||||||
|
alloc.inner().mutability,
|
||||||
|
);
|
||||||
|
let local_data_id =
|
||||||
|
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
||||||
|
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
|
||||||
|
}
|
||||||
GlobalAlloc::Static(def_id) => {
|
GlobalAlloc::Static(def_id) => {
|
||||||
assert!(fx.tcx.is_static(def_id));
|
assert!(fx.tcx.is_static(def_id));
|
||||||
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
|
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
|
||||||
|
|
|
@ -183,13 +183,6 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
|
||||||
}
|
}
|
||||||
Scalar::Ptr(ptr, _size) => {
|
Scalar::Ptr(ptr, _size) => {
|
||||||
let (alloc_id, offset) = ptr.into_parts();
|
let (alloc_id, offset) = ptr.into_parts();
|
||||||
// For vtables, get the underlying data allocation.
|
|
||||||
let alloc_id = match self.tcx.global_alloc(alloc_id) {
|
|
||||||
GlobalAlloc::VTable(ty, trait_ref) => {
|
|
||||||
self.tcx.vtable_allocation((ty, trait_ref))
|
|
||||||
}
|
|
||||||
_ => alloc_id,
|
|
||||||
};
|
|
||||||
let base_addr =
|
let base_addr =
|
||||||
match self.tcx.global_alloc(alloc_id) {
|
match self.tcx.global_alloc(alloc_id) {
|
||||||
GlobalAlloc::Memory(alloc) => {
|
GlobalAlloc::Memory(alloc) => {
|
||||||
|
@ -208,7 +201,11 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
|
||||||
GlobalAlloc::Function(fn_instance) => {
|
GlobalAlloc::Function(fn_instance) => {
|
||||||
self.get_fn_addr(fn_instance)
|
self.get_fn_addr(fn_instance)
|
||||||
},
|
},
|
||||||
GlobalAlloc::VTable(..) => panic!("vtables are already handled"),
|
GlobalAlloc::VTable(ty, trait_ref) => {
|
||||||
|
let alloc = self.tcx.global_alloc(self.tcx.vtable_allocation((ty, trait_ref))).unwrap_memory();
|
||||||
|
let init = const_alloc_to_gcc(self, alloc);
|
||||||
|
self.static_addr_of(init, alloc.inner().align, None)
|
||||||
|
}
|
||||||
GlobalAlloc::Static(def_id) => {
|
GlobalAlloc::Static(def_id) => {
|
||||||
assert!(self.tcx.is_static(def_id));
|
assert!(self.tcx.is_static(def_id));
|
||||||
self.get_static(def_id).get_address(None)
|
self.get_static(def_id).get_address(None)
|
||||||
|
|
|
@ -240,13 +240,6 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
||||||
}
|
}
|
||||||
Scalar::Ptr(ptr, _size) => {
|
Scalar::Ptr(ptr, _size) => {
|
||||||
let (alloc_id, offset) = ptr.into_parts();
|
let (alloc_id, offset) = ptr.into_parts();
|
||||||
// For vtables, get the underlying data allocation.
|
|
||||||
let alloc_id = match self.tcx.global_alloc(alloc_id) {
|
|
||||||
GlobalAlloc::VTable(ty, trait_ref) => {
|
|
||||||
self.tcx.vtable_allocation((ty, trait_ref))
|
|
||||||
}
|
|
||||||
_ => alloc_id,
|
|
||||||
};
|
|
||||||
let (base_addr, base_addr_space) = match self.tcx.global_alloc(alloc_id) {
|
let (base_addr, base_addr_space) = match self.tcx.global_alloc(alloc_id) {
|
||||||
GlobalAlloc::Memory(alloc) => {
|
GlobalAlloc::Memory(alloc) => {
|
||||||
let init = const_alloc_to_llvm(self, alloc);
|
let init = const_alloc_to_llvm(self, alloc);
|
||||||
|
@ -264,7 +257,15 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
||||||
self.get_fn_addr(fn_instance.polymorphize(self.tcx)),
|
self.get_fn_addr(fn_instance.polymorphize(self.tcx)),
|
||||||
self.data_layout().instruction_address_space,
|
self.data_layout().instruction_address_space,
|
||||||
),
|
),
|
||||||
GlobalAlloc::VTable(..) => bug!("vtables are already handled"),
|
GlobalAlloc::VTable(ty, trait_ref) => {
|
||||||
|
let alloc = self
|
||||||
|
.tcx
|
||||||
|
.global_alloc(self.tcx.vtable_allocation((ty, trait_ref)))
|
||||||
|
.unwrap_memory();
|
||||||
|
let init = const_alloc_to_llvm(self, alloc);
|
||||||
|
let value = self.static_addr_of(init, alloc.inner().align, None);
|
||||||
|
(value, AddressSpace::DATA)
|
||||||
|
}
|
||||||
GlobalAlloc::Static(def_id) => {
|
GlobalAlloc::Static(def_id) => {
|
||||||
assert!(self.tcx.is_static(def_id));
|
assert!(self.tcx.is_static(def_id));
|
||||||
assert!(!self.tcx.is_thread_local_static(def_id));
|
assert!(!self.tcx.is_thread_local_static(def_id));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue