Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created April 21, 2019 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/d8436786c63df53dfb5d9f2c995b7012 to your computer and use it in GitHub Desktop.
Save rust-play/d8436786c63df53dfb5d9f2c995b7012 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn derive_switch_int(&mut self, generator: NodeId, discr: Operand<'tcx>, switch_ty: Ty, values: Cow<'tcx,[u128]>, targets: Vec<BasicBlock>, sir: &mut Sir) -> Vec<Frame<'tcx>> {
let new_memory = self.current_memory.clone();
let compare_to = self.process_operand(discr, generator, sir);
let otherwise_target = targets.pop().unwrap();
let all_other = targets.iter();
let (expressions, frames): (Vec<Expr>, Vec<Option<Frame>>) = values.iter().zip(all_other).map(|(desired_val, target)| {
let comp = Expr::Value(SymTy::from_scalar(*desired_val, switch_ty));
let prec = Expr::BinOp(Rator::Eq, Box::new(comp), Box::new(compare_to));
(prec, self.block_to_frame(generator, *target, Some(prec)))
}).unzip();
let init_val = expressions.pop().unwrap();
let otherwise_expr = expressions.into_iter().fold(init_val, |x,y| Expr::UnOp(Rator::Not, Box::new(Expr::BinOp(Rator::And, Box::new(x), Box::new(y)))));
frames.push(self.block_to_frame(generator, otherwise_target, Some(otherwise_expr)));
frames.into_iter().filter_map(|x| if let Some(val) = x {
Some(val)
} else {
None
}).collect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment