Skip to content

Commit 496f4db

Browse files
committed
slightly cleaner, if more verbose, vtable handling in codegen backends
1 parent 108ef8b commit 496f4db

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/constant.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,6 @@ pub(crate) fn codegen_const_value<'tcx>(
195195
}
196196
Scalar::Ptr(ptr, _size) => {
197197
let (alloc_id, offset) = ptr.into_parts(); // we know the `offset` is relative
198-
// For vtables, get the underlying data allocation.
199-
let alloc_id = match fx.tcx.global_alloc(alloc_id) {
200-
GlobalAlloc::VTable(ty, trait_ref) => fx.tcx.vtable_allocation((ty, trait_ref)),
201-
_ => alloc_id,
202-
};
203198
let base_addr = match fx.tcx.global_alloc(alloc_id) {
204199
GlobalAlloc::Memory(alloc) => {
205200
let data_id = data_id_for_alloc_id(
@@ -221,7 +216,20 @@ pub(crate) fn codegen_const_value<'tcx>(
221216
fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
222217
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
223218
}
224-
GlobalAlloc::VTable(..) => bug!("vtables are already handled"),
219+
GlobalAlloc::VTable(ty, trait_ref) => {
220+
let alloc_id = fx.tcx.vtable_allocation((ty, trait_ref));
221+
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
222+
// FIXME: factor this common code with the `Memory` arm into a function?
223+
let data_id = data_id_for_alloc_id(
224+
&mut fx.constants_cx,
225+
fx.module,
226+
alloc_id,
227+
alloc.inner().mutability,
228+
);
229+
let local_data_id =
230+
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
231+
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
232+
}
225233
GlobalAlloc::Static(def_id) => {
226234
assert!(fx.tcx.is_static(def_id));
227235
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);

Diff for: compiler/rustc_codegen_gcc/src/common.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,6 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
183183
}
184184
Scalar::Ptr(ptr, _size) => {
185185
let (alloc_id, offset) = ptr.into_parts();
186-
// For vtables, get the underlying data allocation.
187-
let alloc_id = match self.tcx.global_alloc(alloc_id) {
188-
GlobalAlloc::VTable(ty, trait_ref) => {
189-
self.tcx.vtable_allocation((ty, trait_ref))
190-
}
191-
_ => alloc_id,
192-
};
193186
let base_addr =
194187
match self.tcx.global_alloc(alloc_id) {
195188
GlobalAlloc::Memory(alloc) => {
@@ -208,7 +201,11 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
208201
GlobalAlloc::Function(fn_instance) => {
209202
self.get_fn_addr(fn_instance)
210203
},
211-
GlobalAlloc::VTable(..) => panic!("vtables are already handled"),
204+
GlobalAlloc::VTable(ty, trait_ref) => {
205+
let alloc = self.tcx.global_alloc(self.tcx.vtable_allocation((ty, trait_ref))).unwrap_memory();
206+
let init = const_alloc_to_gcc(self, alloc);
207+
self.static_addr_of(init, alloc.inner().align, None)
208+
}
212209
GlobalAlloc::Static(def_id) => {
213210
assert!(self.tcx.is_static(def_id));
214211
self.get_static(def_id).get_address(None)

Diff for: compiler/rustc_codegen_llvm/src/common.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,6 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
240240
}
241241
Scalar::Ptr(ptr, _size) => {
242242
let (alloc_id, offset) = ptr.into_parts();
243-
// For vtables, get the underlying data allocation.
244-
let alloc_id = match self.tcx.global_alloc(alloc_id) {
245-
GlobalAlloc::VTable(ty, trait_ref) => {
246-
self.tcx.vtable_allocation((ty, trait_ref))
247-
}
248-
_ => alloc_id,
249-
};
250243
let (base_addr, base_addr_space) = match self.tcx.global_alloc(alloc_id) {
251244
GlobalAlloc::Memory(alloc) => {
252245
let init = const_alloc_to_llvm(self, alloc);
@@ -264,7 +257,15 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
264257
self.get_fn_addr(fn_instance.polymorphize(self.tcx)),
265258
self.data_layout().instruction_address_space,
266259
),
267-
GlobalAlloc::VTable(..) => bug!("vtables are already handled"),
260+
GlobalAlloc::VTable(ty, trait_ref) => {
261+
let alloc = self
262+
.tcx
263+
.global_alloc(self.tcx.vtable_allocation((ty, trait_ref)))
264+
.unwrap_memory();
265+
let init = const_alloc_to_llvm(self, alloc);
266+
let value = self.static_addr_of(init, alloc.inner().align, None);
267+
(value, AddressSpace::DATA)
268+
}
268269
GlobalAlloc::Static(def_id) => {
269270
assert!(self.tcx.is_static(def_id));
270271
assert!(!self.tcx.is_thread_local_static(def_id));

0 commit comments

Comments
 (0)