1
Fork 0

Correctly align all allocs

Fixes #348
This commit is contained in:
bjorn3 2019-03-30 18:22:43 +01:00
parent 2f0093b8c2
commit c4af588f72
4 changed files with 25 additions and 10 deletions

View file

@ -17,7 +17,3 @@ rustc-std-workspace-alloc = { path = "./rustc-std-workspace-alloc" }
[profile.release] [profile.release]
debug = true debug = true
[profile.dev]
# FIXME correctly align constants, so that copy_nonoverlapping doesn't complain about alignment
debug-assertions = false

View file

@ -186,6 +186,21 @@ impl Sub for i16 {
} }
} }
#[lang = "rem"]
pub trait Rem<RHS = Self> {
type Output;
fn rem(self, rhs: RHS) -> Self::Output;
}
impl Rem for usize {
type Output = Self;
fn rem(self, rhs: Self) -> Self {
self % rhs
}
}
#[lang = "bitor"] #[lang = "bitor"]
pub trait BitOr<RHS = Self> { pub trait BitOr<RHS = Self> {
type Output; type Output;

View file

@ -134,6 +134,10 @@ fn main() {
call_return_u128_pair(); call_return_u128_pair();
let slice = &[0, 1] as &[i32];
let slice_ptr = slice as *const [i32] as *const i32;
assert_eq!(slice_ptr as usize % 4, 0);
//return; //return;
unsafe { unsafe {

View file

@ -3,7 +3,7 @@ use std::borrow::Cow;
use rustc::mir::interpret::{ use rustc::mir::interpret::{
read_target_uint, AllocId, GlobalAlloc, Allocation, ConstValue, InterpResult, GlobalId, Scalar, read_target_uint, AllocId, GlobalAlloc, Allocation, ConstValue, InterpResult, GlobalId, Scalar,
}; };
use rustc::ty::Const; use rustc::ty::{Const, layout::Align};
use rustc_mir::interpret::{ use rustc_mir::interpret::{
InterpCx, ImmTy, Machine, Memory, MemoryKind, OpTy, PlaceTy, InterpCx, ImmTy, Machine, Memory, MemoryKind, OpTy, PlaceTy,
StackPopCleanup, StackPopCleanup,
@ -170,13 +170,13 @@ fn trans_const_place<'a, 'tcx: 'a>(
//println!("const value: {:?} allocation: {:?}", value, alloc); //println!("const value: {:?} allocation: {:?}", value, alloc);
let alloc_id = fx.tcx.alloc_map.lock().create_memory_alloc(alloc); let alloc_id = fx.tcx.alloc_map.lock().create_memory_alloc(alloc);
fx.constants.todo.insert(TodoItem::Alloc(alloc_id)); fx.constants.todo.insert(TodoItem::Alloc(alloc_id));
let data_id = data_id_for_alloc_id(fx.module, alloc_id); let data_id = data_id_for_alloc_id(fx.module, alloc_id, alloc.align);
cplace_for_dataid(fx, const_.ty, data_id) cplace_for_dataid(fx, const_.ty, data_id)
} }
fn data_id_for_alloc_id(module: &mut Module<impl Backend>, alloc_id: AllocId) -> DataId { fn data_id_for_alloc_id<B: Backend>(module: &mut Module<B>, alloc_id: AllocId, align: Align) -> DataId {
module module
.declare_data(&format!("__alloc_{}", alloc_id.0), Linkage::Local, false, None) .declare_data(&format!("__alloc_{}", alloc_id.0), Linkage::Local, false, Some(align.bytes() as u8))
.unwrap() .unwrap()
} }
@ -245,8 +245,8 @@ fn define_all_allocs(
let (data_id, alloc) = match todo_item { let (data_id, alloc) = match todo_item {
TodoItem::Alloc(alloc_id) => { TodoItem::Alloc(alloc_id) => {
//println!("alloc_id {}", alloc_id); //println!("alloc_id {}", alloc_id);
let data_id = data_id_for_alloc_id(module, alloc_id);
let alloc = memory.get(alloc_id).unwrap(); let alloc = memory.get(alloc_id).unwrap();
let data_id = data_id_for_alloc_id(module, alloc_id, alloc.align);
(data_id, alloc) (data_id, alloc)
} }
TodoItem::Static(def_id) => { TodoItem::Static(def_id) => {
@ -302,7 +302,7 @@ fn define_all_allocs(
} }
GlobalAlloc::Memory(_) => { GlobalAlloc::Memory(_) => {
cx.todo.insert(TodoItem::Alloc(reloc)); cx.todo.insert(TodoItem::Alloc(reloc));
data_id_for_alloc_id(module, reloc) data_id_for_alloc_id(module, reloc, alloc.align)
} }
GlobalAlloc::Static(def_id) => { GlobalAlloc::Static(def_id) => {
cx.todo.insert(TodoItem::Static(def_id)); cx.todo.insert(TodoItem::Static(def_id));