Skip to content

Commit

Permalink
Merge pull request rust-lang#36 from oli-obk/alignment
Browse files Browse the repository at this point in the history
Use target byte order instead of host byteorder
  • Loading branch information
solson committed Jun 25, 2016
2 parents ba23b87 + 37287d2 commit f28feed
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 19 deletions.
30 changes: 24 additions & 6 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,32 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
// TODO(solson): Try making const_to_primval instead.
fn const_to_ptr(&mut self, const_val: &const_val::ConstVal) -> EvalResult<'tcx, Pointer> {
use rustc::middle::const_val::ConstVal::*;
use rustc_const_math::{ConstInt, ConstIsize, ConstUsize};
macro_rules! i2p {
($i:ident, $n:expr) => {{
let ptr = self.memory.allocate($n);
self.memory.write_int(ptr, $i as i64, $n)?;
Ok(ptr)
}}
}
match *const_val {
Float(_f) => unimplemented!(),
Integral(int) => {
// TODO(solson): Check int constant type.
let ptr = self.memory.allocate(8);
self.memory.write_uint(ptr, int.to_u64_unchecked(), 8)?;
Ok(ptr)
}
Integral(ConstInt::Infer(_)) => unreachable!(),
Integral(ConstInt::InferSigned(_)) => unreachable!(),
Integral(ConstInt::I8(i)) => i2p!(i, 1),
Integral(ConstInt::U8(i)) => i2p!(i, 1),
Integral(ConstInt::I16(i)) => i2p!(i, 2),
Integral(ConstInt::U16(i)) => i2p!(i, 2),
Integral(ConstInt::I32(i)) => i2p!(i, 4),
Integral(ConstInt::U32(i)) => i2p!(i, 4),
Integral(ConstInt::I64(i)) => i2p!(i, 8),
Integral(ConstInt::U64(i)) => i2p!(i, 8),
Integral(ConstInt::Isize(ConstIsize::Is16(i))) => i2p!(i, 2),
Integral(ConstInt::Isize(ConstIsize::Is32(i))) => i2p!(i, 4),
Integral(ConstInt::Isize(ConstIsize::Is64(i))) => i2p!(i, 8),
Integral(ConstInt::Usize(ConstUsize::Us16(i))) => i2p!(i, 2),
Integral(ConstInt::Usize(ConstUsize::Us32(i))) => i2p!(i, 4),
Integral(ConstInt::Usize(ConstUsize::Us64(i))) => i2p!(i, 8),
Str(ref s) => {
let psize = self.memory.pointer_size();
let static_ptr = self.memory.allocate(s.len());
Expand Down
65 changes: 52 additions & 13 deletions src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt};
use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian, BigEndian, self};
use std::collections::Bound::{Included, Excluded};
use std::collections::{btree_map, BTreeMap, HashMap, HashSet, VecDeque};
use std::{fmt, iter, mem, ptr};

use rustc::hir::def_id::DefId;
use rustc::ty::BareFnTy;
use rustc::ty::subst::Substs;
use rustc::ty::layout::TargetDataLayout;
use rustc::ty::layout::{self, TargetDataLayout};

use error::{EvalError, EvalResult};
use primval::PrimVal;
Expand Down Expand Up @@ -159,6 +159,10 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
pub fn pointer_size(&self) -> usize {
self.layout.pointer_size.bytes() as usize
}

pub fn endianess(&self) -> layout::Endian {
self.layout.endian
}
}

/// Allocation accessors
Expand Down Expand Up @@ -339,8 +343,9 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
pub fn read_ptr(&self, ptr: Pointer) -> EvalResult<'tcx, Pointer> {
let size = self.pointer_size();
self.check_defined(ptr, size)?;
let offset = self.get_bytes_unchecked(ptr, size)?
.read_uint::<NativeEndian>(size).unwrap() as usize;
let endianess = self.endianess();
let bytes = self.get_bytes_unchecked(ptr, size)?;
let offset = read_target_uint(endianess, bytes).unwrap() as usize;
let alloc = self.get(ptr.alloc_id)?;
match alloc.relocations.get(&ptr.offset) {
Some(&alloc_id) => Ok(Pointer { alloc_id: alloc_id, offset: offset }),
Expand All @@ -349,11 +354,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
}

pub fn write_ptr(&mut self, dest: Pointer, ptr: Pointer) -> EvalResult<'tcx, ()> {
{
let size = self.pointer_size();
let mut bytes = self.get_bytes_mut(dest, size)?;
bytes.write_uint::<NativeEndian>(ptr.offset as u64, size).unwrap();
}
self.write_usize(dest, ptr.offset as u64)?;
self.get_mut(dest.alloc_id)?.relocations.insert(dest.offset, ptr.alloc_id);
Ok(())
}
Expand Down Expand Up @@ -391,19 +392,25 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
}

pub fn read_int(&self, ptr: Pointer, size: usize) -> EvalResult<'tcx, i64> {
self.get_bytes(ptr, size).map(|mut b| b.read_int::<NativeEndian>(size).unwrap())
self.get_bytes(ptr, size).map(|b| read_target_int(self.endianess(), b).unwrap())
}

pub fn write_int(&mut self, ptr: Pointer, n: i64, size: usize) -> EvalResult<'tcx, ()> {
self.get_bytes_mut(ptr, size).map(|mut b| b.write_int::<NativeEndian>(n, size).unwrap())
let endianess = self.endianess();
let b = self.get_bytes_mut(ptr, size)?;
write_target_int(endianess, b, n).unwrap();
Ok(())
}

pub fn read_uint(&self, ptr: Pointer, size: usize) -> EvalResult<'tcx, u64> {
self.get_bytes(ptr, size).map(|mut b| b.read_uint::<NativeEndian>(size).unwrap())
self.get_bytes(ptr, size).map(|b| read_target_uint(self.endianess(), b).unwrap())
}

pub fn write_uint(&mut self, ptr: Pointer, n: u64, size: usize) -> EvalResult<'tcx, ()> {
self.get_bytes_mut(ptr, size).map(|mut b| b.write_uint::<NativeEndian>(n, size).unwrap())
let endianess = self.endianess();
let b = self.get_bytes_mut(ptr, size)?;
write_target_uint(endianess, b, n).unwrap();
Ok(())
}

pub fn read_isize(&self, ptr: Pointer) -> EvalResult<'tcx, i64> {
Expand Down Expand Up @@ -513,6 +520,38 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
}
}

////////////////////////////////////////////////////////////////////////////////
// Methods to access integers in the target endianess
////////////////////////////////////////////////////////////////////////////////

fn write_target_uint(endianess: layout::Endian, mut target: &mut [u8], data: u64) -> Result<(), byteorder::Error> {
let len = target.len();
match endianess {
layout::Endian::Little => target.write_uint::<LittleEndian>(data, len),
layout::Endian::Big => target.write_uint::<BigEndian>(data, len),
}
}
fn write_target_int(endianess: layout::Endian, mut target: &mut [u8], data: i64) -> Result<(), byteorder::Error> {
let len = target.len();
match endianess {
layout::Endian::Little => target.write_int::<LittleEndian>(data, len),
layout::Endian::Big => target.write_int::<BigEndian>(data, len),
}
}

fn read_target_uint(endianess: layout::Endian, mut source: &[u8]) -> Result<u64, byteorder::Error> {
match endianess {
layout::Endian::Little => source.read_uint::<LittleEndian>(source.len()),
layout::Endian::Big => source.read_uint::<BigEndian>(source.len()),
}
}
fn read_target_int(endianess: layout::Endian, mut source: &[u8]) -> Result<i64, byteorder::Error> {
match endianess {
layout::Endian::Little => source.read_int::<LittleEndian>(source.len()),
layout::Endian::Big => source.read_int::<BigEndian>(source.len()),
}
}

////////////////////////////////////////////////////////////////////////////////
// Undefined byte tracking
////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit f28feed

Please sign in to comment.