Skip to content

Commit 05f3336

Browse files
committed
[AVR] Ensure that function pointers stored within aggregates are annotated with the correct space
Before this patch, a function pointer stored within an aggregate like a struct or an enum would always have the default address space `0`. This patch removes this assumption and instead, introspects the inner type being pointed at, storing the target address space in the PointeeInfo struct so that downstream users may query it.
1 parent 4351488 commit 05f3336

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

src/librustc_codegen_llvm/type_of.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,13 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyAndLayout<'tcx> {
311311
F64 => cx.type_f64(),
312312
Pointer => {
313313
// If we know the alignment, pick something better than i8.
314-
let pointee = if let Some(pointee) = self.pointee_info_at(cx, offset) {
315-
cx.type_pointee_for_align(pointee.align)
316-
} else {
317-
cx.type_i8()
318-
};
319-
cx.type_ptr_to(pointee, AddressSpace::default())
314+
let (pointee, address_space) =
315+
if let Some(pointee) = self.pointee_info_at(cx, offset) {
316+
(cx.type_pointee_for_align(pointee.align), pointee.address_space)
317+
} else {
318+
(cx.type_i8(), AddressSpace::default())
319+
};
320+
cx.type_ptr_to(pointee, address_space)
320321
}
321322
}
322323
}

src/librustc_middle/ty/layout.rs

+34-4
Original file line numberDiff line numberDiff line change
@@ -2148,16 +2148,36 @@ where
21482148
}
21492149

21502150
fn pointee_info_at(this: TyAndLayout<'tcx>, cx: &C, offset: Size) -> Option<PointeeInfo> {
2151-
match this.ty.kind {
2151+
let addr_space_of_ty = |ty: Ty<'tcx>| {
2152+
if ty.is_fn() {
2153+
cx.data_layout().instruction_address_space
2154+
} else {
2155+
AddressSpace::default()
2156+
}
2157+
};
2158+
2159+
let pointee_info = match this.ty.kind {
21522160
ty::RawPtr(mt) if offset.bytes() == 0 => {
21532161
cx.layout_of(mt.ty).to_result().ok().map(|layout| PointeeInfo {
21542162
size: layout.size,
21552163
align: layout.align.abi,
21562164
safe: None,
2165+
address_space: addr_space_of_ty(mt.ty),
2166+
})
2167+
}
2168+
ty::FnPtr(fn_sig) if offset.bytes() == 0 => {
2169+
cx.layout_of(cx.tcx().mk_fn_ptr(fn_sig)).to_result().ok().map(|layout| {
2170+
PointeeInfo {
2171+
size: layout.size,
2172+
align: layout.align.abi,
2173+
safe: None,
2174+
address_space: cx.data_layout().instruction_address_space,
2175+
}
21572176
})
21582177
}
2159-
21602178
ty::Ref(_, ty, mt) if offset.bytes() == 0 => {
2179+
let address_space = addr_space_of_ty(ty);
2180+
21612181
let tcx = cx.tcx();
21622182
let is_freeze = ty.is_freeze(tcx, cx.param_env(), DUMMY_SP);
21632183
let kind = match mt {
@@ -2192,6 +2212,7 @@ where
21922212
size: layout.size,
21932213
align: layout.align.abi,
21942214
safe: Some(kind),
2215+
address_space,
21952216
})
21962217
}
21972218

@@ -2236,7 +2257,9 @@ where
22362257
result = field.to_result().ok().and_then(|field| {
22372258
if ptr_end <= field_start + field.size {
22382259
// We found the right field, look inside it.
2239-
field.pointee_info_at(cx, offset - field_start)
2260+
let field_info =
2261+
field.pointee_info_at(cx, offset - field_start);
2262+
field_info
22402263
} else {
22412264
None
22422265
}
@@ -2259,7 +2282,14 @@ where
22592282

22602283
result
22612284
}
2262-
}
2285+
};
2286+
2287+
debug!(
2288+
"pointee_info_at (offset={:?}, type kind: {:?}) => {:?}",
2289+
offset, this.ty.kind, pointee_info
2290+
);
2291+
2292+
pointee_info
22632293
}
22642294
}
22652295

src/librustc_target/abi/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ impl<T, E> MaybeResult<T> for Result<T, E> {
10241024
}
10251025
}
10261026

1027-
#[derive(Copy, Clone, PartialEq, Eq)]
1027+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
10281028
pub enum PointerKind {
10291029
/// Most general case, we know no restrictions to tell LLVM.
10301030
Shared,
@@ -1039,11 +1039,12 @@ pub enum PointerKind {
10391039
UniqueOwned,
10401040
}
10411041

1042-
#[derive(Copy, Clone)]
1042+
#[derive(Copy, Clone, Debug)]
10431043
pub struct PointeeInfo {
10441044
pub size: Size,
10451045
pub align: Align,
10461046
pub safe: Option<PointerKind>,
1047+
pub address_space: AddressSpace,
10471048
}
10481049

10491050
pub trait TyAndLayoutMethods<'a, C: LayoutOf<Ty = Self>>: Sized {

0 commit comments

Comments
 (0)