Skip to content

Commit 559b57e

Browse files
abi: add AddressSpace field to Primitive::Pointer
...and remove it from `PointeeInfo`, which isn't meant for this. There are still various places (marked with FIXMEs) that assume all pointers have the same size and alignment. Fixing this requires parsing non-default address spaces in the data layout string, which will be done in a followup.
1 parent 2671f37 commit 559b57e

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
709709
bx.range_metadata(load, vr);
710710
}
711711
}
712-
abi::Pointer if vr.start < vr.end && !vr.contains(0) => {
712+
abi::Pointer(_) if vr.start < vr.end && !vr.contains(0) => {
713713
bx.nonnull_metadata(load);
714714
}
715715
_ => {}

src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
211211
let base_addr = self.const_bitcast(base_addr, self.usize_type);
212212
let offset = self.context.new_rvalue_from_long(self.usize_type, offset.bytes() as i64);
213213
let ptr = self.const_bitcast(base_addr + offset, ptr_type);
214-
if layout.primitive() != Pointer {
214+
if !matches!(layout.primitive(), Pointer(_)) {
215215
self.const_bitcast(ptr.dereference(None).to_rvalue(), ty)
216216
}
217217
else {

src/consts.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}
77
use rustc_middle::mir::mono::MonoItem;
88
use rustc_middle::ty::{self, Instance, Ty};
99
use rustc_middle::ty::layout::LayoutOf;
10-
use rustc_middle::mir::interpret::{self, ConstAllocation, ErrorHandled, Scalar as InterpScalar, read_target_uint};
10+
use rustc_middle::mir::interpret::{self, ConstAllocation, ErrorHandled, GlobalAlloc, Scalar as InterpScalar, read_target_uint};
1111
use rustc_span::def_id::DefId;
12-
use rustc_target::abi::{self, Align, HasDataLayout, Primitive, Size, WrappingRange};
12+
use rustc_target::abi::{self, AddressSpace, Align, HasDataLayout, Primitive, Size, WrappingRange};
1313

1414
use crate::base;
1515
use crate::context::CodegenCx;
@@ -322,13 +322,21 @@ pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: ConstAl
322322
)
323323
.expect("const_alloc_to_llvm: could not read relocation pointer")
324324
as u64;
325+
326+
let address_space = match cx.tcx.global_alloc(alloc_id) {
327+
GlobalAlloc::Function(..) => cx.data_layout().instruction_address_space,
328+
GlobalAlloc::Static(..) | GlobalAlloc::Memory(..) | GlobalAlloc::VTable(..) => {
329+
AddressSpace::DATA
330+
}
331+
};
332+
325333
llvals.push(cx.scalar_to_backend(
326334
InterpScalar::from_pointer(
327335
interpret::Pointer::new(alloc_id, Size::from_bytes(ptr_offset)),
328336
&cx.tcx,
329337
),
330-
abi::Scalar::Initialized { value: Primitive::Pointer, valid_range: WrappingRange::full(dl.pointer_size) },
331-
cx.type_i8p(),
338+
abi::Scalar::Initialized { value: Primitive::Pointer(address_space), valid_range: WrappingRange::full(dl.pointer_size) },
339+
cx.type_i8p_ext(address_space),
332340
));
333341
next_offset = offset + pointer_size;
334342
}

src/type_of.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
253253
Int(i, false) => cx.type_from_unsigned_integer(i),
254254
F32 => cx.type_f32(),
255255
F64 => cx.type_f64(),
256-
Pointer => {
256+
Pointer(address_space) => {
257257
// If we know the alignment, pick something better than i8.
258258
let pointee =
259259
if let Some(pointee) = self.pointee_info_at(cx, offset) {
@@ -262,7 +262,7 @@ impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
262262
else {
263263
cx.type_i8()
264264
};
265-
cx.type_ptr_to(pointee)
265+
cx.type_ptr_to_ext(pointee, address_space)
266266
}
267267
}
268268
}

0 commit comments

Comments
 (0)