Support SwitchInt for integer types.

This commit is contained in:
Scott Olson 2016-03-07 08:22:18 -06:00
parent e41af43dbf
commit 0cb7e3fae0
3 changed files with 43 additions and 34 deletions

View File

@ -171,8 +171,26 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> {
If { ref cond, targets: (then_target, else_target) } => {
let cond_ptr = try!(self.operand_to_ptr(cond));
let cond = try!(self.memory.read_bool(cond_ptr));
current_block = if cond { then_target } else { else_target };
let cond_val = try!(self.memory.read_bool(cond_ptr));
current_block = if cond_val { then_target } else { else_target };
}
SwitchInt { ref discr, ref values, ref targets, .. } => {
// FIXME(tsion): Handle non-integer switch types.
let discr_ptr = try!(self.lvalue_to_ptr(discr));
let discr_val = try!(self.memory.read_int(discr_ptr));
// Branch to the `otherwise` case by default, if no match is found.
current_block = targets[targets.len() - 1];
for (index, val_const) in values.iter().enumerate() {
let ptr = try!(self.const_to_ptr(val_const));
let val = try!(self.memory.read_int(ptr));
if discr_val == val {
current_block = targets[index];
break;
}
}
}
// Call { ref func, ref args, ref destination, .. } => {
@ -203,15 +221,6 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> {
// }
// }
// SwitchInt { ref discr, ref values, ref targets, .. } => {
// let discr_val = self.read_lvalue(discr);
// let index = values.iter().position(|v| discr_val == self.const_to_ptr(v))
// .expect("discriminant matched no values");
// current_block = targets[index];
// }
// Switch { ref discr, ref targets, .. } => {
// let discr_val = self.read_lvalue(discr);

View File

@ -159,7 +159,7 @@ impl Repr {
Repr::Aggregate { size: size, fields: fields }
},
_ => unimplemented!(),
ref t => panic!("can't convert type to repr: {:?}", t),
}
}

View File

@ -70,18 +70,18 @@ fn if_true() -> i32 {
// increment(1)
// }
// // #[miri_run(expected = "Int(3628800)")]
// // fn factorial_loop() -> i32 {
// // let mut product = 1;
// // let mut i = 1;
// #[miri_run(expected = "Int(3628800)")]
// fn factorial_loop() -> i32 {
// let mut product = 1;
// let mut i = 1;
// // while i <= 10 {
// // product *= i;
// // i += 1;
// // }
// while i <= 10 {
// product *= i;
// i += 1;
// }
// // product
// // }
// product
// }
// #[miri_run(expected = "Int(3628800)")]
// fn factorial_recursive() -> i32 {
@ -96,7 +96,7 @@ fn if_true() -> i32 {
// fact(10)
// }
// #[miri_run(expected = "Int(1)")]
// #[miri_run]
// fn match_bool() -> i32 {
// let b = true;
// match b {
@ -105,17 +105,17 @@ fn if_true() -> i32 {
// }
// }
// #[miri_run(expected = "Int(20)")]
// fn match_int() -> i32 {
// let n = 2;
// match n {
// 0 => 0,
// 1 => 10,
// 2 => 20,
// 3 => 30,
// _ => 100,
// }
// }
#[miri_run]
fn match_int() -> i32 {
let n = 2;
match n {
0 => 0,
1 => 10,
2 => 20,
3 => 30,
_ => 100,
}
}
// #[miri_run(expected = "Int(1)")]
// fn one_line_ref() -> i32 {