Skip to content

Commit e041b4a

Browse files
Support Harvard architectures.
1 parent 4ffff8f commit e041b4a

File tree

9 files changed

+41
-18
lines changed

9 files changed

+41
-18
lines changed

src/librustc_codegen_llvm/abi.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_target::abi::call::ArgType;
1111

1212
use rustc_codegen_ssa::traits::*;
1313

14-
use rustc_target::abi::{HasDataLayout, LayoutOf, Size, TyLayout, Abi as LayoutAbi};
14+
use rustc_target::abi::{LayoutOf, Size, TyLayout, Abi as LayoutAbi};
1515
use rustc::ty::{self, Ty, Instance};
1616
use rustc::ty::layout;
1717

@@ -311,7 +311,6 @@ pub trait FnTypeExt<'tcx> {
311311
cx: &CodegenCx<'ll, 'tcx>,
312312
abi: Abi);
313313
fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
314-
fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
315314
fn llvm_cconv(&self) -> llvm::CallConv;
316315
fn apply_attrs_llfn(&self, llfn: &'ll Value);
317316
fn apply_attrs_callsite(&self, bx: &mut Builder<'a, 'll, 'tcx>, callsite: &'ll Value);
@@ -695,13 +694,6 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
695694
}
696695
}
697696

698-
fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
699-
unsafe {
700-
llvm::LLVMPointerType(self.llvm_type(cx),
701-
cx.data_layout().instruction_address_space as c_uint)
702-
}
703-
}
704-
705697
fn llvm_cconv(&self) -> llvm::CallConv {
706698
match self.conv {
707699
Conv::C => llvm::CCallConv,

src/librustc_codegen_llvm/context.rs

+10
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub struct CodegenCx<'ll, 'tcx: 'll> {
8989
const_addr_space: AddrSpaceIdx,
9090
mutable_addr_space: AddrSpaceIdx,
9191
flat_addr_space: AddrSpaceIdx,
92+
instruction_addr_space: AddrSpaceIdx,
9293

9394
pub dbg_cx: Option<debuginfo::CrateDebugContext<'ll, 'tcx>>,
9495

@@ -299,6 +300,11 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
299300
.get(&AddrSpaceKind::Flat)
300301
.map(|v| v.index )
301302
.unwrap_or_default();
303+
let instruction_addr_space =
304+
tcx.sess.target.target.options.addr_spaces
305+
.get(&AddrSpaceKind::Instruction)
306+
.map(|v| v.index )
307+
.unwrap_or_default();
302308

303309
CodegenCx {
304310
tcx,
@@ -325,6 +331,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
325331
const_addr_space,
326332
mutable_addr_space,
327333
flat_addr_space,
334+
instruction_addr_space,
328335

329336
dbg_cx,
330337
eh_personality: Cell::new(None),
@@ -497,6 +504,9 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
497504

498505
from_props.shared_with.contains(&to_kind)
499506
}
507+
fn inst_addr_space(&self) -> AddrSpaceIdx {
508+
self.instruction_addr_space
509+
}
500510
fn alloca_addr_space(&self) -> AddrSpaceIdx {
501511
self.alloca_addr_space
502512
}

src/librustc_codegen_llvm/declare.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use context::CodegenCx;
2424
use type_::Type;
2525
use rustc_codegen_ssa::traits::*;
2626
use value::Value;
27+
use common::val_ty;
2728

2829
/// Declare a function.
2930
///
@@ -40,6 +41,7 @@ fn declare_raw_fn(
4041
let llfn = unsafe {
4142
llvm::LLVMRustGetOrInsertFunction(cx.llmod, namebuf.as_ptr(), ty)
4243
};
44+
assert_eq!(val_ty(llfn).address_space(), cx.inst_addr_space());
4345

4446
llvm::SetFunctionCallConv(llfn, callconv);
4547
// Function addresses in Rust are never significant, allowing functions to

src/librustc_codegen_llvm/type_.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
337337
ty.llvm_type(self)
338338
}
339339
fn fn_ptr_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
340-
ty.ptr_to_llvm_type(self)
340+
self.type_as_ptr_to(ty.llvm_type(self), self.inst_addr_space())
341+
341342
}
342343
fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {
343344
ty.llvm_type(self)

src/librustc_codegen_ssa/meth.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
8181
}
8282

8383
// Not in the cache. Build it.
84-
let nullptr = cx.const_null(cx.type_i8p());
84+
let nullptr = cx.const_null(cx.type_inst_i8p());
8585

8686
let methods_root;
8787
let methods = if let Some(trait_ref) = trait_ref {

src/librustc_codegen_ssa/traits/misc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub trait MiscMethods<'tcx>: BackendTypes {
2929
fn create_used_variable(&self);
3030

3131
fn can_cast_addr_space(&self, _from: AddrSpaceIdx, _to: AddrSpaceIdx) -> bool { true }
32+
fn inst_addr_space(&self) -> AddrSpaceIdx { Default::default() }
3233
fn alloca_addr_space(&self) -> AddrSpaceIdx { Default::default() }
3334
fn const_addr_space(&self) -> AddrSpaceIdx { Default::default() }
3435
fn mutable_addr_space(&self) -> AddrSpaceIdx { Default::default() }

src/librustc_codegen_ssa/traits/type_.rs

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ pub trait DerivedTypeMethods<'tcx>: BaseTypeMethods<'tcx> + MiscMethods<'tcx> {
8484
fn type_i8p_as(&self, addr_space: AddrSpaceIdx) -> Self::Type {
8585
self.type_as_ptr_to(self.type_i8(), addr_space)
8686
}
87+
fn type_inst_i8p(&self) -> Self::Type {
88+
self.type_i8p_as(self.inst_addr_space())
89+
}
8790
fn type_alloca_i8p(&self) -> Self::Type {
8891
self.type_i8p_as(self.alloca_addr_space())
8992
}
@@ -214,6 +217,9 @@ pub trait DerivedTypeMethods<'tcx>: BaseTypeMethods<'tcx> + MiscMethods<'tcx> {
214217
_ => { },
215218
}
216219
}
220+
fn type_ptr_to_inst(&self, ty: Self::Type) -> Self::Type {
221+
self.type_as_ptr_to(ty, self.inst_addr_space())
222+
}
217223
fn type_ptr_to_alloca(&self, ty: Self::Type) -> Self::Type {
218224
self.type_as_ptr_to(ty, self.alloca_addr_space())
219225
}

src/librustc_target/abi/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ pub struct TargetDataLayout {
3131
pub vector_align: Vec<(Size, AbiAndPrefAlign)>,
3232

3333
pub alloca_address_space: AddrSpaceIdx,
34-
35-
pub instruction_address_space: u32,
34+
pub instruction_address_space: AddrSpaceIdx,
3635
}
3736

3837
impl Default for TargetDataLayout {
@@ -54,11 +53,11 @@ impl Default for TargetDataLayout {
5453
pointer_align: AbiAndPrefAlign::new(align(64)),
5554
aggregate_align: AbiAndPrefAlign { abi: align(0), pref: align(64) },
5655
alloca_address_space: Default::default(),
56+
instruction_address_space: Default::default(),
5757
vector_align: vec![
5858
(Size::from_bits(64), AbiAndPrefAlign::new(align(64))),
5959
(Size::from_bits(128), AbiAndPrefAlign::new(align(128))),
6060
],
61-
instruction_address_space: 0,
6261
}
6362
}
6463
}
@@ -128,6 +127,11 @@ impl TargetDataLayout {
128127
let align = align(a, p)?;
129128
resize_and_set(&mut dl.pointers, idx, Some((size, align)));
130129
},
130+
[ref p] if p.starts_with("P") => {
131+
let idx = parse_bits(&p[1..], "u32",
132+
"instruction address space")? as u32;
133+
dl.instruction_address_space = AddrSpaceIdx(idx);
134+
}
131135
[s, ref a..] if s.starts_with("i") => {
132136
let bits = match s[1..].parse::<u64>() {
133137
Ok(bits) => bits,

src/librustc_target/spec/mod.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ pub enum AddrSpaceKind {
288288
ReadOnly,
289289
/// aka global
290290
ReadWrite,
291+
/// For Harvard architectures, the program instruction's address space
292+
Instruction,
291293
Named(String),
292294
}
293295

@@ -299,6 +301,7 @@ impl FromStr for AddrSpaceKind {
299301
"alloca" => AddrSpaceKind::Alloca,
300302
"readonly" => AddrSpaceKind::ReadOnly,
301303
"readwrite" => AddrSpaceKind::ReadWrite,
304+
"instruction" => AddrSpaceKind::Instruction,
302305
named => AddrSpaceKind::Named(named.into()),
303306
})
304307
}
@@ -310,6 +313,7 @@ impl fmt::Display for AddrSpaceKind {
310313
&AddrSpaceKind::Alloca => "alloca",
311314
&AddrSpaceKind::ReadOnly => "readonly",
312315
&AddrSpaceKind::ReadWrite => "readwrite",
316+
&AddrSpaceKind::Instruction => "instruction",
313317
&AddrSpaceKind::Named(ref s) => s,
314318
})
315319
}
@@ -389,10 +393,13 @@ impl Default for AddrSpaces {
389393
fn default() -> Self {
390394
let mut asp = BTreeMap::new();
391395

392-
let kinds = vec![AddrSpaceKind::ReadOnly,
393-
AddrSpaceKind::ReadWrite,
394-
AddrSpaceKind::Alloca,
395-
AddrSpaceKind::Flat, ];
396+
let kinds = vec![
397+
AddrSpaceKind::ReadOnly,
398+
AddrSpaceKind::ReadWrite,
399+
AddrSpaceKind::Alloca,
400+
AddrSpaceKind::Flat,
401+
AddrSpaceKind::Instruction,
402+
];
396403

397404
let insert = |asp: &mut BTreeMap<_, _>, kind, idx| {
398405
let props = AddrSpaceProps {

0 commit comments

Comments
 (0)