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

Commit 7bf0670

Browse files
erikdesjardinsantoyo
authored andcommitted
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 5dcda26 commit 7bf0670

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
@@ -768,7 +768,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
768768
bx.range_metadata(load, vr);
769769
}
770770
}
771-
abi::Pointer if vr.start < vr.end && !vr.contains(0) => {
771+
abi::Pointer(_) if vr.start < vr.end && !vr.contains(0) => {
772772
bx.nonnull_metadata(load);
773773
}
774774
_ => {}

src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
221221
let base_addr = self.const_bitcast(base_addr, self.usize_type);
222222
let offset = self.context.new_rvalue_from_long(self.usize_type, offset.bytes() as i64);
223223
let ptr = self.const_bitcast(base_addr + offset, ptr_type);
224-
if layout.primitive() != Pointer {
224+
if !matches!(layout.primitive(), Pointer(_)) {
225225
self.const_bitcast(ptr.dereference(None).to_rvalue(), ty)
226226
}
227227
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;
@@ -306,13 +306,21 @@ pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: ConstAl
306306
)
307307
.expect("const_alloc_to_llvm: could not read relocation pointer")
308308
as u64;
309+
310+
let address_space = match cx.tcx.global_alloc(alloc_id) {
311+
GlobalAlloc::Function(..) => cx.data_layout().instruction_address_space,
312+
GlobalAlloc::Static(..) | GlobalAlloc::Memory(..) | GlobalAlloc::VTable(..) => {
313+
AddressSpace::DATA
314+
}
315+
};
316+
309317
llvals.push(cx.scalar_to_backend(
310318
InterpScalar::from_pointer(
311319
interpret::Pointer::new(alloc_id, Size::from_bytes(ptr_offset)),
312320
&cx.tcx,
313321
),
314-
abi::Scalar::Initialized { value: Primitive::Pointer, valid_range: WrappingRange::full(dl.pointer_size) },
315-
cx.type_i8p(),
322+
abi::Scalar::Initialized { value: Primitive::Pointer(address_space), valid_range: WrappingRange::full(dl.pointer_size) },
323+
cx.type_i8p_ext(address_space),
316324
));
317325
next_offset = offset + pointer_size;
318326
}

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)