Skip to content

Commit f28feed

Browse files
authored
Merge pull request rust-lang#36 from oli-obk/alignment
Use target byte order instead of host byteorder
2 parents ba23b87 + 37287d2 commit f28feed

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
lines changed

Diff for: src/interpreter/mod.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,32 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
169169
// TODO(solson): Try making const_to_primval instead.
170170
fn const_to_ptr(&mut self, const_val: &const_val::ConstVal) -> EvalResult<'tcx, Pointer> {
171171
use rustc::middle::const_val::ConstVal::*;
172+
use rustc_const_math::{ConstInt, ConstIsize, ConstUsize};
173+
macro_rules! i2p {
174+
($i:ident, $n:expr) => {{
175+
let ptr = self.memory.allocate($n);
176+
self.memory.write_int(ptr, $i as i64, $n)?;
177+
Ok(ptr)
178+
}}
179+
}
172180
match *const_val {
173181
Float(_f) => unimplemented!(),
174-
Integral(int) => {
175-
// TODO(solson): Check int constant type.
176-
let ptr = self.memory.allocate(8);
177-
self.memory.write_uint(ptr, int.to_u64_unchecked(), 8)?;
178-
Ok(ptr)
179-
}
182+
Integral(ConstInt::Infer(_)) => unreachable!(),
183+
Integral(ConstInt::InferSigned(_)) => unreachable!(),
184+
Integral(ConstInt::I8(i)) => i2p!(i, 1),
185+
Integral(ConstInt::U8(i)) => i2p!(i, 1),
186+
Integral(ConstInt::I16(i)) => i2p!(i, 2),
187+
Integral(ConstInt::U16(i)) => i2p!(i, 2),
188+
Integral(ConstInt::I32(i)) => i2p!(i, 4),
189+
Integral(ConstInt::U32(i)) => i2p!(i, 4),
190+
Integral(ConstInt::I64(i)) => i2p!(i, 8),
191+
Integral(ConstInt::U64(i)) => i2p!(i, 8),
192+
Integral(ConstInt::Isize(ConstIsize::Is16(i))) => i2p!(i, 2),
193+
Integral(ConstInt::Isize(ConstIsize::Is32(i))) => i2p!(i, 4),
194+
Integral(ConstInt::Isize(ConstIsize::Is64(i))) => i2p!(i, 8),
195+
Integral(ConstInt::Usize(ConstUsize::Us16(i))) => i2p!(i, 2),
196+
Integral(ConstInt::Usize(ConstUsize::Us32(i))) => i2p!(i, 4),
197+
Integral(ConstInt::Usize(ConstUsize::Us64(i))) => i2p!(i, 8),
180198
Str(ref s) => {
181199
let psize = self.memory.pointer_size();
182200
let static_ptr = self.memory.allocate(s.len());

Diff for: src/memory.rs

+52-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt};
1+
use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian, BigEndian, self};
22
use std::collections::Bound::{Included, Excluded};
33
use std::collections::{btree_map, BTreeMap, HashMap, HashSet, VecDeque};
44
use std::{fmt, iter, mem, ptr};
55

66
use rustc::hir::def_id::DefId;
77
use rustc::ty::BareFnTy;
88
use rustc::ty::subst::Substs;
9-
use rustc::ty::layout::TargetDataLayout;
9+
use rustc::ty::layout::{self, TargetDataLayout};
1010

1111
use error::{EvalError, EvalResult};
1212
use primval::PrimVal;
@@ -159,6 +159,10 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
159159
pub fn pointer_size(&self) -> usize {
160160
self.layout.pointer_size.bytes() as usize
161161
}
162+
163+
pub fn endianess(&self) -> layout::Endian {
164+
self.layout.endian
165+
}
162166
}
163167

164168
/// Allocation accessors
@@ -339,8 +343,9 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
339343
pub fn read_ptr(&self, ptr: Pointer) -> EvalResult<'tcx, Pointer> {
340344
let size = self.pointer_size();
341345
self.check_defined(ptr, size)?;
342-
let offset = self.get_bytes_unchecked(ptr, size)?
343-
.read_uint::<NativeEndian>(size).unwrap() as usize;
346+
let endianess = self.endianess();
347+
let bytes = self.get_bytes_unchecked(ptr, size)?;
348+
let offset = read_target_uint(endianess, bytes).unwrap() as usize;
344349
let alloc = self.get(ptr.alloc_id)?;
345350
match alloc.relocations.get(&ptr.offset) {
346351
Some(&alloc_id) => Ok(Pointer { alloc_id: alloc_id, offset: offset }),
@@ -349,11 +354,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
349354
}
350355

351356
pub fn write_ptr(&mut self, dest: Pointer, ptr: Pointer) -> EvalResult<'tcx, ()> {
352-
{
353-
let size = self.pointer_size();
354-
let mut bytes = self.get_bytes_mut(dest, size)?;
355-
bytes.write_uint::<NativeEndian>(ptr.offset as u64, size).unwrap();
356-
}
357+
self.write_usize(dest, ptr.offset as u64)?;
357358
self.get_mut(dest.alloc_id)?.relocations.insert(dest.offset, ptr.alloc_id);
358359
Ok(())
359360
}
@@ -391,19 +392,25 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
391392
}
392393

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

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

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

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

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

523+
////////////////////////////////////////////////////////////////////////////////
524+
// Methods to access integers in the target endianess
525+
////////////////////////////////////////////////////////////////////////////////
526+
527+
fn write_target_uint(endianess: layout::Endian, mut target: &mut [u8], data: u64) -> Result<(), byteorder::Error> {
528+
let len = target.len();
529+
match endianess {
530+
layout::Endian::Little => target.write_uint::<LittleEndian>(data, len),
531+
layout::Endian::Big => target.write_uint::<BigEndian>(data, len),
532+
}
533+
}
534+
fn write_target_int(endianess: layout::Endian, mut target: &mut [u8], data: i64) -> Result<(), byteorder::Error> {
535+
let len = target.len();
536+
match endianess {
537+
layout::Endian::Little => target.write_int::<LittleEndian>(data, len),
538+
layout::Endian::Big => target.write_int::<BigEndian>(data, len),
539+
}
540+
}
541+
542+
fn read_target_uint(endianess: layout::Endian, mut source: &[u8]) -> Result<u64, byteorder::Error> {
543+
match endianess {
544+
layout::Endian::Little => source.read_uint::<LittleEndian>(source.len()),
545+
layout::Endian::Big => source.read_uint::<BigEndian>(source.len()),
546+
}
547+
}
548+
fn read_target_int(endianess: layout::Endian, mut source: &[u8]) -> Result<i64, byteorder::Error> {
549+
match endianess {
550+
layout::Endian::Little => source.read_int::<LittleEndian>(source.len()),
551+
layout::Endian::Big => source.read_int::<BigEndian>(source.len()),
552+
}
553+
}
554+
516555
////////////////////////////////////////////////////////////////////////////////
517556
// Undefined byte tracking
518557
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)