提交 b0d7338d 编写于 作者: B Björn Steinbrink

[MIR trans] Add support for SwitchInt

上级 86069e45
...@@ -50,8 +50,15 @@ pub fn trans_block(&mut self, bb: mir::BasicBlock) { ...@@ -50,8 +50,15 @@ pub fn trans_block(&mut self, bb: mir::BasicBlock) {
unimplemented!() unimplemented!()
} }
mir::Terminator::SwitchInt { .. } => { mir::Terminator::SwitchInt { ref discr, switch_ty, ref values, ref targets } => {
unimplemented!() let (otherwise, targets) = targets.split_last().unwrap();
let discr = build::Load(bcx, self.trans_lvalue(bcx, discr).llval);
let switch = build::Switch(bcx, discr, self.llblock(*otherwise), values.len());
for (value, target) in values.iter().zip(targets) {
let llval = self.trans_constval(bcx, value, switch_ty);
let llbb = self.llblock(*target);
build::AddCase(switch, llval, llbb)
}
} }
mir::Terminator::Diverge => { mir::Terminator::Diverge => {
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
// except according to those terms. // except according to those terms.
use llvm::ValueRef; use llvm::ValueRef;
use middle::ty::Ty;
use rustc::middle::const_eval::ConstVal; use rustc::middle::const_eval::ConstVal;
use rustc_mir::repr as mir; use rustc_mir::repr as mir;
use trans::consts::{self, TrueConst}; use trans::consts::{self, TrueConst};
...@@ -18,20 +19,15 @@ ...@@ -18,20 +19,15 @@
use super::MirContext; use super::MirContext;
impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
pub fn trans_constant(&mut self, pub fn trans_constval(&mut self,
bcx: Block<'bcx, 'tcx>, bcx: Block<'bcx, 'tcx>,
constant: &mir::Constant<'tcx>) cv: &ConstVal,
ty: Ty<'tcx>)
-> ValueRef -> ValueRef
{ {
let ccx = bcx.ccx(); let ccx = bcx.ccx();
let constant_ty = bcx.monomorphize(&constant.ty); let llty = type_of::type_of(ccx, ty);
let llty = type_of::type_of(ccx, constant_ty); match *cv {
match constant.literal {
mir::Literal::Item { .. } => {
unimplemented!()
}
mir::Literal::Value { ref value } => {
match *value {
ConstVal::Float(v) => common::C_floating_f64(v, llty), ConstVal::Float(v) => common::C_floating_f64(v, llty),
ConstVal::Bool(v) => common::C_bool(ccx, v), ConstVal::Bool(v) => common::C_bool(ccx, v),
ConstVal::Int(v) => common::C_integral(llty, v as u64, true), ConstVal::Int(v) => common::C_integral(llty, v as u64, true),
...@@ -58,6 +54,20 @@ pub fn trans_constant(&mut self, ...@@ -58,6 +54,20 @@ pub fn trans_constant(&mut self,
} }
} }
} }
pub fn trans_constant(&mut self,
bcx: Block<'bcx, 'tcx>,
constant: &mir::Constant<'tcx>)
-> ValueRef
{
let constant_ty = bcx.monomorphize(&constant.ty);
match constant.literal {
mir::Literal::Item { .. } => {
unimplemented!()
}
mir::Literal::Value { ref value } => {
self.trans_constval(bcx, value, constant_ty)
}
} }
} }
} }
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(rustc_attrs)]
#[rustc_mir]
pub fn foo(x: i8) -> i32 {
match x {
1 => 0,
_ => 1,
}
}
fn main() {
assert_eq!(foo(0), 1);
assert_eq!(foo(1), 0);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册