Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement floats by running the ops on the host architecture #42

Merged
merged 3 commits into from
Jul 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ 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};
use rustc_const_math::{ConstInt, ConstIsize, ConstUsize, ConstFloat};
macro_rules! i2p {
($i:ident, $n:expr) => {{
let ptr = self.memory.allocate($n);
Expand All @@ -178,7 +178,17 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
}}
}
match *const_val {
Float(_f) => unimplemented!(),
Float(ConstFloat::F32(f)) => {
let ptr = self.memory.allocate(4);
self.memory.write_f32(ptr, f)?;
Ok(ptr)
},
Float(ConstFloat::F64(f)) => {
let ptr = self.memory.allocate(8);
self.memory.write_f64(ptr, f)?;
Ok(ptr)
},
Float(ConstFloat::FInfer{..}) => unreachable!(),
Integral(ConstInt::Infer(_)) => unreachable!(),
Integral(ConstInt::InferSigned(_)) => unreachable!(),
Integral(ConstInt::I8(i)) => i2p!(i, 1),
Expand Down Expand Up @@ -824,7 +834,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
}

pub fn read_primval(&mut self, ptr: Pointer, ty: Ty<'tcx>) -> EvalResult<'tcx, PrimVal> {
use syntax::ast::{IntTy, UintTy};
use syntax::ast::{IntTy, UintTy, FloatTy};
let val = match (self.memory.pointer_size(), &ty.sty) {
(_, &ty::TyBool) => PrimVal::Bool(self.memory.read_bool(ptr)?),
(_, &ty::TyChar) => {
Expand All @@ -849,6 +859,9 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
(8, &ty::TyUint(UintTy::Us)) |
(_, &ty::TyUint(UintTy::U64)) => PrimVal::U64(self.memory.read_uint(ptr, 8)? as u64),

(_, &ty::TyFloat(FloatTy::F32)) => PrimVal::F32(self.memory.read_f32(ptr)?),
(_, &ty::TyFloat(FloatTy::F64)) => PrimVal::F64(self.memory.read_f64(ptr)?),

(_, &ty::TyFnDef(def_id, substs, fn_ty)) => {
PrimVal::FnPtr(self.memory.create_fn_ptr(def_id, substs, fn_ty))
},
Expand Down
54 changes: 54 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
PrimVal::U64(n) => self.write_uint(ptr, n as u64, 8),
PrimVal::Char(c) => self.write_uint(ptr, c as u64, 4),
PrimVal::IntegerPtr(n) => self.write_uint(ptr, n as u64, pointer_size),
PrimVal::F32(f) => self.write_f32(ptr, f),
PrimVal::F64(f) => self.write_f64(ptr, f),
PrimVal::FnPtr(_p) |
PrimVal::AbstractPtr(_p) => unimplemented!(),
}
Expand Down Expand Up @@ -464,6 +466,28 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
let size = self.pointer_size();
self.write_uint(ptr, n, size)
}

pub fn write_f32(&mut self, ptr: Pointer, f: f32) -> EvalResult<'tcx, ()> {
let endianess = self.endianess();
let b = self.get_bytes_mut(ptr, 4)?;
write_target_f32(endianess, b, f).unwrap();
Ok(())
}

pub fn write_f64(&mut self, ptr: Pointer, f: f64) -> EvalResult<'tcx, ()> {
let endianess = self.endianess();
let b = self.get_bytes_mut(ptr, 8)?;
write_target_f64(endianess, b, f).unwrap();
Ok(())
}

pub fn read_f32(&self, ptr: Pointer) -> EvalResult<'tcx, f32> {
self.get_bytes(ptr, 4).map(|b| read_target_f32(self.endianess(), b).unwrap())
}

pub fn read_f64(&self, ptr: Pointer) -> EvalResult<'tcx, f64> {
self.get_bytes(ptr, 8).map(|b| read_target_f64(self.endianess(), b).unwrap())
}
}

/// Relocations
Expand Down Expand Up @@ -586,6 +610,36 @@ fn read_target_int(endianess: layout::Endian, mut source: &[u8]) -> Result<i64,
}
}

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

fn write_target_f32(endianess: layout::Endian, mut target: &mut [u8], data: f32) -> Result<(), byteorder::Error> {
match endianess {
layout::Endian::Little => target.write_f32::<LittleEndian>(data),
layout::Endian::Big => target.write_f32::<BigEndian>(data),
}
}
fn write_target_f64(endianess: layout::Endian, mut target: &mut [u8], data: f64) -> Result<(), byteorder::Error> {
match endianess {
layout::Endian::Little => target.write_f64::<LittleEndian>(data),
layout::Endian::Big => target.write_f64::<BigEndian>(data),
}
}

fn read_target_f32(endianess: layout::Endian, mut source: &[u8]) -> Result<f32, byteorder::Error> {
match endianess {
layout::Endian::Little => source.read_f32::<LittleEndian>(),
layout::Endian::Big => source.read_f32::<BigEndian>(),
}
}
fn read_target_f64(endianess: layout::Endian, mut source: &[u8]) -> Result<f64, byteorder::Error> {
match endianess {
layout::Endian::Little => source.read_f64::<LittleEndian>(),
layout::Endian::Big => source.read_f64::<BigEndian>(),
}
}

////////////////////////////////////////////////////////////////////////////////
// Undefined byte tracking
////////////////////////////////////////////////////////////////////////////////
Expand Down
33 changes: 33 additions & 0 deletions src/primval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum PrimVal {
FnPtr(Pointer),
IntegerPtr(u64),
Char(char),

F32(f32), F64(f64),
}

/// returns the result of the operation and whether the operation overflowed
Expand Down Expand Up @@ -57,6 +59,32 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
})
}

macro_rules! float_binops {
($v:ident, $l:ident, $r:ident) => ({
match bin_op {
Add => $v($l + $r),
Sub => $v($l - $r),
Mul => $v($l * $r),
Div => $v($l / $r),
Rem => $v($l % $r),

// invalid float ops
BitXor => unreachable!(),
BitAnd => unreachable!(),
BitOr => unreachable!(),
Shl => unreachable!(),
Shr => unreachable!(),

Eq => Bool($l == $r),
Ne => Bool($l != $r),
Lt => Bool($l < $r),
Le => Bool($l <= $r),
Gt => Bool($l > $r),
Ge => Bool($l >= $r),
}
})
}

fn unrelated_ptr_ops<'tcx>(bin_op: mir::BinOp) -> EvalResult<'tcx, PrimVal> {
use rustc::mir::repr::BinOp::*;
match bin_op {
Expand Down Expand Up @@ -128,6 +156,8 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
(U16(l), U16(r)) => int_binops!(U16, l, r),
(U32(l), U32(r)) => int_binops!(U32, l, r),
(U64(l), U64(r)) => int_binops!(U64, l, r),
(F32(l), F32(r)) => float_binops!(F32, l, r),
(F64(l), F64(r)) => float_binops!(F64, l, r),
(Char(l), Char(r)) => match bin_op {
Eq => Bool(l == r),
Ne => Bool(l != r),
Expand Down Expand Up @@ -211,6 +241,9 @@ pub fn unary_op<'tcx>(un_op: mir::UnOp, val: PrimVal) -> EvalResult<'tcx, PrimVa
(Not, U16(n)) => Ok(U16(!n)),
(Not, U32(n)) => Ok(U32(!n)),
(Not, U64(n)) => Ok(U64(!n)),

(Neg, F64(n)) => Ok(F64(-n)),
(Neg, F32(n)) => Ok(F32(-n)),
_ => Err(EvalError::Unimplemented(format!("unimplemented unary op: {:?}, {:?}", un_op, val))),
}
}
11 changes: 11 additions & 0 deletions tests/run-pass/floats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

fn main() {
assert_eq!(6.0_f32*6.0_f32, 36.0_f32);
assert_eq!(6.0_f64*6.0_f64, 36.0_f64);
assert_eq!(-{5.0_f32}, -5.0_f32);
assert!((5.0_f32/0.0).is_infinite());
assert!((-5.0_f32).sqrt().is_nan());
let x: u64 = unsafe { std::mem::transmute(42.0_f64) };
let y: f64 = unsafe { std::mem::transmute(x) };
assert_eq!(y, 42.0_f64);
}