Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1c77e2b

Browse files
committed
add a Vtable kind of symbolic allocations
1 parent 22a0a2e commit 1c77e2b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/constant.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,13 @@ 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-
let alloc_kind = fx.tcx.get_global_alloc(alloc_id);
199-
let base_addr = match alloc_kind {
200-
Some(GlobalAlloc::Memory(alloc)) => {
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+
};
203+
let base_addr = match fx.tcx.global_alloc(alloc_id) {
204+
GlobalAlloc::Memory(alloc) => {
201205
let data_id = data_id_for_alloc_id(
202206
&mut fx.constants_cx,
203207
fx.module,
@@ -211,13 +215,14 @@ pub(crate) fn codegen_const_value<'tcx>(
211215
}
212216
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
213217
}
214-
Some(GlobalAlloc::Function(instance)) => {
218+
GlobalAlloc::Function(instance) => {
215219
let func_id = crate::abi::import_function(fx.tcx, fx.module, instance);
216220
let local_func_id =
217221
fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
218222
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
219223
}
220-
Some(GlobalAlloc::Static(def_id)) => {
224+
GlobalAlloc::Vtable(..) => bug!("vtables are already handled"),
225+
GlobalAlloc::Static(def_id) => {
221226
assert!(fx.tcx.is_static(def_id));
222227
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
223228
let local_data_id =
@@ -227,7 +232,6 @@ pub(crate) fn codegen_const_value<'tcx>(
227232
}
228233
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
229234
}
230-
None => bug!("missing allocation {:?}", alloc_id),
231235
};
232236
let val = if offset.bytes() != 0 {
233237
fx.bcx.ins().iadd_imm(base_addr, i64::try_from(offset.bytes()).unwrap())
@@ -360,7 +364,9 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
360364
//println!("alloc_id {}", alloc_id);
361365
let alloc = match tcx.get_global_alloc(alloc_id).unwrap() {
362366
GlobalAlloc::Memory(alloc) => alloc,
363-
GlobalAlloc::Function(_) | GlobalAlloc::Static(_) => unreachable!(),
367+
GlobalAlloc::Function(_) | GlobalAlloc::Static(_) | GlobalAlloc::Vtable(..) => {
368+
unreachable!()
369+
}
364370
};
365371
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
366372
module
@@ -424,7 +430,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
424430
read_target_uint(endianness, bytes).unwrap()
425431
};
426432

427-
let reloc_target_alloc = tcx.get_global_alloc(alloc_id).unwrap();
433+
let reloc_target_alloc = tcx.global_alloc(alloc_id);
428434
let data_id = match reloc_target_alloc {
429435
GlobalAlloc::Function(instance) => {
430436
assert_eq!(addend, 0);
@@ -436,6 +442,10 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
436442
GlobalAlloc::Memory(target_alloc) => {
437443
data_id_for_alloc_id(cx, module, alloc_id, target_alloc.inner().mutability)
438444
}
445+
GlobalAlloc::Vtable(ty, trait_ref) => {
446+
let alloc_id = tcx.vtable_allocation((ty, trait_ref));
447+
data_id_for_alloc_id(cx, module, alloc_id, Mutability::Not)
448+
}
439449
GlobalAlloc::Static(def_id) => {
440450
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
441451
{

0 commit comments

Comments
 (0)