Skip to content

Commit

Permalink
Merge pull request rust-lang#67 from oli-obk/master
Browse files Browse the repository at this point in the history
fix multi field enum variants and some intrinsics + rustup
  • Loading branch information
solson authored Oct 3, 2016
2 parents 2080fae + de38015 commit a08f061
Show file tree
Hide file tree
Showing 11 changed files with 455 additions and 86 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ log = "0.3.6"
log_settings = "0.1.1"

[dev-dependencies]
compiletest_rs = "0.2"
compiletest_rs = "0.2.3"
2 changes: 1 addition & 1 deletion src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,5 @@ fn main() {
args.push(find_sysroot());
}

rustc_driver::run_compiler(&args, &mut MiriCompilerCalls);
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls, None, None);
}
72 changes: 25 additions & 47 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,13 @@ pub struct Frame<'a, 'tcx: 'a> {
// Return pointer and local allocations
////////////////////////////////////////////////////////////////////////////////

/// A pointer for writing the return value of the current call if it's not a diverging call.
pub return_ptr: Option<Pointer>,

/// The block to return to when returning from the current stack frame
pub return_to_block: StackPopCleanup,

/// The list of locals for the current function, stored in order as
/// `[arguments..., variables..., temporaries...]`. The variables begin at `self.var_offset`
/// and the temporaries at `self.temp_offset`.
/// `[return_ptr, arguments..., variables..., temporaries...]`.
pub locals: Vec<Pointer>,

/// The offset of the first variable in `self.locals`.
pub var_offset: usize,

/// The offset of the first temporary in `self.locals`.
pub temp_offset: usize,

////////////////////////////////////////////////////////////////////////////////
// Current position within the function
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -336,32 +326,26 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
span: codemap::Span,
mir: CachedMir<'a, 'tcx>,
substs: &'tcx Substs<'tcx>,
return_ptr: Option<Pointer>,
return_ptr: Pointer,
return_to_block: StackPopCleanup,
) -> EvalResult<'tcx, ()> {
let arg_tys = mir.arg_decls.iter().map(|a| a.ty);
let var_tys = mir.var_decls.iter().map(|v| v.ty);
let temp_tys = mir.temp_decls.iter().map(|t| t.ty);

let num_args = mir.arg_decls.len();
let num_vars = mir.var_decls.len();
let local_tys = mir.local_decls.iter().map(|a| a.ty);

::log_settings::settings().indentation += 1;

let locals: EvalResult<'tcx, Vec<Pointer>> = arg_tys.chain(var_tys).chain(temp_tys).map(|ty| {
// directly change the first allocation (the return value) to *be* the allocation where the
// caller stores the result
let locals: EvalResult<'tcx, Vec<Pointer>> = iter::once(Ok(return_ptr)).chain(local_tys.skip(1).map(|ty| {
let size = self.type_size_with_substs(ty, substs);
let align = self.type_align_with_substs(ty, substs);
self.memory.allocate(size, align)
}).collect();
})).collect();

self.stack.push(Frame {
mir: mir.clone(),
block: mir::START_BLOCK,
return_ptr: return_ptr,
return_to_block: return_to_block,
locals: locals?,
var_offset: num_args,
temp_offset: num_args + num_vars,
span: span,
def_id: def_id,
substs: substs,
Expand Down Expand Up @@ -793,11 +777,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
fn eval_lvalue(&mut self, lvalue: &mir::Lvalue<'tcx>) -> EvalResult<'tcx, Lvalue> {
use rustc::mir::repr::Lvalue::*;
let ptr = match *lvalue {
ReturnPointer => self.frame().return_ptr
.expect("ReturnPointer used in a function with no return value"),
Arg(i) => self.frame().locals[i.index()],
Var(i) => self.frame().locals[self.frame().var_offset + i.index()],
Temp(i) => self.frame().locals[self.frame().temp_offset + i.index()],
Local(i) => self.frame().locals[i.index()],

Static(def_id) => {
let substs = subst::Substs::empty(self.tcx);
Expand All @@ -819,11 +799,13 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
Field(field, field_ty) => {
let field_ty = self.monomorphize(field_ty, self.substs());
use rustc::ty::layout::Layout::*;
let variant = match *base_layout {
Univariant { ref variant, .. } => variant,
let field = field.index();
let offset = match *base_layout {
Univariant { ref variant, .. } => variant.field_offset(field),
General { ref variants, .. } => {
if let LvalueExtra::DowncastVariant(variant_idx) = base.extra {
&variants[variant_idx]
// +1 for the discriminant, which is field 0
variants[variant_idx].field_offset(field + 1)
} else {
bug!("field access on enum had no variant index");
}
Expand All @@ -832,14 +814,11 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
assert_eq!(field.index(), 0);
return Ok(base);
}
StructWrappedNullablePointer { ref nonnull, .. } => nonnull,
StructWrappedNullablePointer { ref nonnull, .. } => nonnull.field_offset(field),
_ => bug!("field access on non-product type: {:?}", base_layout),
};

let offset = variant.field_offset(field.index()).bytes();
let ptr = base.ptr.offset(offset as isize);
trace!("{:?}", base);
trace!("{:?}", field_ty);
let ptr = base.ptr.offset(offset.bytes() as isize);
if self.type_is_sized(field_ty) {
ptr
} else {
Expand All @@ -859,9 +838,9 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
Downcast(_, variant) => {
use rustc::ty::layout::Layout::*;
match *base_layout {
General { ref variants, .. } => {
General { .. } => {
return Ok(Lvalue {
ptr: base.ptr.offset(variants[variant].field_offset(1).bytes() as isize),
ptr: base.ptr,
extra: LvalueExtra::DowncastVariant(variant),
});
}
Expand Down Expand Up @@ -1220,18 +1199,17 @@ pub fn eval_main<'a, 'tcx: 'a>(
let return_ptr = ecx.alloc_ret_ptr(mir.return_ty, substs)
.expect("should at least be able to allocate space for the main function's return value");

ecx.push_stack_frame(def_id, mir.span, CachedMir::Ref(mir), substs, Some(return_ptr), StackPopCleanup::None)
ecx.push_stack_frame(def_id, mir.span, CachedMir::Ref(mir), substs, return_ptr, StackPopCleanup::None)
.expect("could not allocate first stack frame");

if mir.arg_decls.len() == 2 {
// FIXME: this is a horrible and wrong way to detect the start function, but overwriting the first two locals shouldn't do much
if mir.local_decls.len() > 2 {
// start function
let ptr_size = ecx.memory().pointer_size();
let nargs = ecx.memory_mut().allocate(ptr_size, ptr_size).expect("can't allocate memory for nargs");
ecx.memory_mut().write_usize(nargs, 0).unwrap();
let args = ecx.memory_mut().allocate(ptr_size, ptr_size).expect("can't allocate memory for arg pointer");
ecx.memory_mut().write_usize(args, 0).unwrap();
ecx.frame_mut().locals[0] = nargs;
ecx.frame_mut().locals[1] = args;
let nargs = ecx.frame_mut().locals[1];
let args = ecx.frame_mut().locals[2];
// ignore errors, if the locals are too small this is not the start function
let _ = ecx.memory_mut().write_usize(nargs, 0);
let _ = ecx.memory_mut().write_usize(args, 0);
}

for _ in 0..step_limit {
Expand Down
4 changes: 2 additions & 2 deletions src/interpreter/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<'a, 'b, 'tcx> ConstantExtractor<'a, 'b, 'tcx> {
} else {
StackPopCleanup::None
};
this.ecx.push_stack_frame(def_id, span, mir, substs, Some(ptr), cleanup)
this.ecx.push_stack_frame(def_id, span, mir, substs, ptr, cleanup)
});
}
fn try<F: FnOnce(&mut Self) -> EvalResult<'tcx, ()>>(&mut self, f: F) {
Expand Down Expand Up @@ -183,7 +183,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ConstantExtractor<'a, 'b, 'tcx> {
constant.span,
mir,
this.substs,
Some(return_ptr),
return_ptr,
StackPopCleanup::Freeze(return_ptr.alloc_id))
});
}
Expand Down
84 changes: 60 additions & 24 deletions src/interpreter/terminator/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
let f32 = self.tcx.types.f32;
let f64 = self.tcx.types.f64;

match &self.tcx.item_name(def_id).as_str()[..] {
let intrinsic_name = &self.tcx.item_name(def_id).as_str()[..];
match intrinsic_name {
"add_with_overflow" => self.intrinsic_with_overflow(mir::BinOp::Add, &args[0], &args[1], dest, dest_layout)?,
"sub_with_overflow" => self.intrinsic_with_overflow(mir::BinOp::Sub, &args[0], &args[1], dest, dest_layout)?,
"mul_with_overflow" => self.intrinsic_with_overflow(mir::BinOp::Mul, &args[0], &args[1], dest, dest_layout)?,
Expand Down Expand Up @@ -64,30 +65,14 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
self.memory.copy(src, dest, count as usize * elem_size, elem_align)?;
}

"ctpop" => {
"ctpop" |
"cttz" |
"ctlz" |
"bswap" => {
let elem_ty = substs.type_at(0);
let elem_size = self.type_size(elem_ty);
let num = self.value_to_primval(args_ptrs[2], elem_ty)?.expect_int("ctpop second arg not integral");
let num = num.count_ones();
self.memory.write_uint(dest, num.into(), elem_size)?;
}

"ctlz" => {
let elem_ty = substs.type_at(0);
let elem_size = self.type_size(elem_ty);
let num = self.value_to_primval(args_ptrs[2], elem_ty)?;
let num = match num {
PrimVal::I8(i) => i.leading_zeros(),
PrimVal::U8(i) => i.leading_zeros(),
PrimVal::I16(i) => i.leading_zeros(),
PrimVal::U16(i) => i.leading_zeros(),
PrimVal::I32(i) => i.leading_zeros(),
PrimVal::U32(i) => i.leading_zeros(),
PrimVal::I64(i) => i.leading_zeros(),
PrimVal::U64(i) => i.leading_zeros(),
_ => bug!("ctlz called with non-integer type"),
};
self.memory.write_uint(dest, num.into(), elem_size)?;
let num = self.value_to_primval(args_ptrs[0], elem_ty)?;
let num = numeric_intrinsic(intrinsic_name, num);
self.memory.write_primval(dest, num)?;
}

"discriminant_value" => {
Expand Down Expand Up @@ -350,3 +335,54 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
self.tcx.normalize_associated_type(&f.ty(self.tcx, param_substs))
}
}

fn numeric_intrinsic(name: &str, val: PrimVal) -> PrimVal {
use primval::PrimVal::*;
match name {
"ctpop" => match val {
I8(i) => I8(i.count_ones() as i8),
U8(i) => U8(i.count_ones() as u8),
I16(i) => I16(i.count_ones() as i16),
U16(i) => U16(i.count_ones() as u16),
I32(i) => I32(i.count_ones() as i32),
U32(i) => U32(i.count_ones() as u32),
I64(i) => I64(i.count_ones() as i64),
U64(i) => U64(i.count_ones() as u64),
other => bug!("invalid `ctpop` argument: {:?}", other),
},
"cttz" => match val {
I8(i) => I8(i.trailing_zeros() as i8),
U8(i) => U8(i.trailing_zeros() as u8),
I16(i) => I16(i.trailing_zeros() as i16),
U16(i) => U16(i.trailing_zeros() as u16),
I32(i) => I32(i.trailing_zeros() as i32),
U32(i) => U32(i.trailing_zeros() as u32),
I64(i) => I64(i.trailing_zeros() as i64),
U64(i) => U64(i.trailing_zeros() as u64),
other => bug!("invalid `cttz` argument: {:?}", other),
},
"ctlz" => match val {
I8(i) => I8(i.leading_zeros() as i8),
U8(i) => U8(i.leading_zeros() as u8),
I16(i) => I16(i.leading_zeros() as i16),
U16(i) => U16(i.leading_zeros() as u16),
I32(i) => I32(i.leading_zeros() as i32),
U32(i) => U32(i.leading_zeros() as u32),
I64(i) => I64(i.leading_zeros() as i64),
U64(i) => U64(i.leading_zeros() as u64),
other => bug!("invalid `ctlz` argument: {:?}", other),
},
"bswap" => match val {
I8(i) => I8(i.swap_bytes() as i8),
U8(i) => U8(i.swap_bytes() as u8),
I16(i) => I16(i.swap_bytes() as i16),
U16(i) => U16(i.swap_bytes() as u16),
I32(i) => I32(i.swap_bytes() as i32),
U32(i) => U32(i.swap_bytes() as u32),
I64(i) => I64(i.swap_bytes() as i64),
U64(i) => U64(i.swap_bytes() as u64),
other => bug!("invalid `bswap` argument: {:?}", other),
},
_ => bug!("not a numeric intrinsic: {}", name),
}
}
7 changes: 4 additions & 3 deletions src/interpreter/terminator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,14 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {

let mir = self.load_mir(resolved_def_id)?;
let (return_ptr, return_to_block) = match destination {
Some((ptr, block)) => (Some(ptr), StackPopCleanup::Goto(block)),
None => (None, StackPopCleanup::None),
Some((ptr, block)) => (ptr, StackPopCleanup::Goto(block)),
None => (Pointer::never_ptr(), StackPopCleanup::None),
};
self.push_stack_frame(resolved_def_id, span, mir, resolved_substs, return_ptr, return_to_block)?;

for (i, (arg_val, arg_ty)) in args.into_iter().enumerate() {
let dest = self.frame().locals[i];
// argument start at index 1, since index 0 is reserved for the return allocation
let dest = self.frame().locals[i + 1];
self.write_value(arg_val, dest, arg_ty)?;
}

Expand Down
17 changes: 12 additions & 5 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ impl Pointer {
self.alloc_id == ZST_ALLOC_ID
}
pub fn to_int<'tcx>(&self) -> EvalResult<'tcx, usize> {
if self.points_to_zst() {
Ok(self.offset)
} else {
Err(EvalError::ReadPointerAsBytes)
match self.alloc_id {
NEVER_ALLOC_ID |
ZST_ALLOC_ID => Ok(self.offset),
_ => Err(EvalError::ReadPointerAsBytes),
}
}
pub fn from_int(i: usize) -> Self {
Expand All @@ -74,6 +74,12 @@ impl Pointer {
offset: 0,
}
}
pub fn never_ptr() -> Self {
Pointer {
alloc_id: NEVER_ALLOC_ID,
offset: 0,
}
}
}

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
Expand Down Expand Up @@ -115,14 +121,15 @@ pub struct Memory<'a, 'tcx> {
}

const ZST_ALLOC_ID: AllocId = AllocId(0);
const NEVER_ALLOC_ID: AllocId = AllocId(1);

impl<'a, 'tcx> Memory<'a, 'tcx> {
pub fn new(layout: &'a TargetDataLayout, max_memory: usize) -> Self {
Memory {
alloc_map: HashMap::new(),
functions: HashMap::new(),
function_alloc_cache: HashMap::new(),
next_id: AllocId(1),
next_id: AllocId(2),
layout: layout,
memory_size: max_memory,
memory_usage: 0,
Expand Down
Loading

0 comments on commit a08f061

Please sign in to comment.