Skip to content

Commit

Permalink
Support for the program data address space option
Browse files Browse the repository at this point in the history
  • Loading branch information
TimNN committed Nov 8, 2018
1 parent 653da4f commit 50a2d47
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/librustc_codegen_llvm/abi.rs
Expand Up @@ -19,7 +19,7 @@ use type_::Type;
use type_of::{LayoutLlvmExt, PointerKind};
use value::Value;

use rustc_target::abi::{LayoutOf, Size, TyLayout, Abi as LayoutAbi};
use rustc_target::abi::{HasDataLayout, LayoutOf, Size, TyLayout, Abi as LayoutAbi};
use rustc::ty::{self, Ty};
use rustc::ty::layout;

Expand Down Expand Up @@ -276,6 +276,7 @@ pub trait FnTypeExt<'tcx> {
cx: &CodegenCx<'ll, 'tcx>,
abi: Abi);
fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
fn llvm_cconv(&self) -> llvm::CallConv;
fn apply_attrs_llfn(&self, llfn: &'ll Value);
fn apply_attrs_callsite(&self, bx: &Builder<'a, 'll, 'tcx>, callsite: &'ll Value);
Expand Down Expand Up @@ -657,6 +658,13 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
}
}

fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
unsafe {
llvm::LLVMPointerType(self.llvm_type(cx),
cx.data_layout().instruction_address_space as c_uint)
}
}

fn llvm_cconv(&self) -> llvm::CallConv {
match self.conv {
Conv::C => llvm::CCallConv,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/meth.rs
Expand Up @@ -39,7 +39,7 @@ impl<'a, 'tcx> VirtualIndex {
// Load the data pointer from the object.
debug!("get_fn({:?}, {:?})", llvtable, self);

let llvtable = bx.pointercast(llvtable, fn_ty.llvm_type(bx.cx).ptr_to().ptr_to());
let llvtable = bx.pointercast(llvtable, fn_ty.ptr_to_llvm_type(bx.cx).ptr_to());
let ptr_align = bx.tcx().data_layout.pointer_align;
let ptr = bx.load(bx.inbounds_gep(llvtable, &[C_usize(bx.cx, self.0)]), ptr_align);
bx.nonnull_metadata(ptr);
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_codegen_llvm/type_.rs
Expand Up @@ -234,6 +234,8 @@ impl Type {
}

pub fn ptr_to(&self) -> &Type {
assert_ne!(self.kind(), TypeKind::Function,
"don't call ptr_to on function types, use ptr_to_llvm_type on FnType instead");
unsafe {
llvm::LLVMPointerType(self, 0)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/type_of.rs
Expand Up @@ -265,7 +265,7 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> {
ty::ParamEnv::reveal_all(),
&sig,
);
FnType::new(cx, sig, &[]).llvm_type(cx).ptr_to()
FnType::new(cx, sig, &[]).ptr_to_llvm_type(cx)
}
_ => self.scalar_llvm_type_at(cx, scalar, Size::ZERO)
};
Expand Down
17 changes: 15 additions & 2 deletions src/librustc_target/abi/mod.rs
Expand Up @@ -35,7 +35,8 @@ pub struct TargetDataLayout {
pub aggregate_align: Align,

/// Alignments for vector types.
pub vector_align: Vec<(Size, Align)>
pub vector_align: Vec<(Size, Align)>,
pub instruction_address_space: u32,
}

impl Default for TargetDataLayout {
Expand All @@ -57,13 +58,22 @@ impl Default for TargetDataLayout {
vector_align: vec![
(Size::from_bits(64), Align::from_bits(64, 64).unwrap()),
(Size::from_bits(128), Align::from_bits(128, 128).unwrap())
]
],
instruction_address_space: 0,
}
}
}

impl TargetDataLayout {
pub fn parse(target: &Target) -> Result<TargetDataLayout, String> {
// Parse an address space index from a string.
let parse_address_space = |s: &str, cause: &str| {
s.parse::<u32>().map_err(|err| {
format!("invalid address space `{}` for `{}` in \"data-layout\": {}",
s, cause, err)
})
};

// Parse a bit count from a string.
let parse_bits = |s: &str, kind: &str, cause: &str| {
s.parse::<u64>().map_err(|err| {
Expand Down Expand Up @@ -96,6 +106,9 @@ impl TargetDataLayout {
match spec.split(':').collect::<Vec<_>>()[..] {
["e"] => dl.endian = Endian::Little,
["E"] => dl.endian = Endian::Big,
[p] if p.starts_with("P") => {
dl.instruction_address_space = parse_address_space(&p[1..], "P")?
}
["a", ref a..] => dl.aggregate_align = align(a, "a")?,
["f32", ref a..] => dl.f32_align = align(a, "f32")?,
["f64", ref a..] => dl.f64_align = align(a, "f64")?,
Expand Down

0 comments on commit 50a2d47

Please sign in to comment.