Skip to content

Commit b0d7338

Browse files
committed
[MIR trans] Add support for SwitchInt
1 parent 86069e4 commit b0d7338

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

src/librustc_trans/trans/mir/block.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,15 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
5050
unimplemented!()
5151
}
5252

53-
mir::Terminator::SwitchInt { .. } => {
54-
unimplemented!()
53+
mir::Terminator::SwitchInt { ref discr, switch_ty, ref values, ref targets } => {
54+
let (otherwise, targets) = targets.split_last().unwrap();
55+
let discr = build::Load(bcx, self.trans_lvalue(bcx, discr).llval);
56+
let switch = build::Switch(bcx, discr, self.llblock(*otherwise), values.len());
57+
for (value, target) in values.iter().zip(targets) {
58+
let llval = self.trans_constval(bcx, value, switch_ty);
59+
let llbb = self.llblock(*target);
60+
build::AddCase(switch, llval, llbb)
61+
}
5562
}
5663

5764
mir::Terminator::Diverge => {

src/librustc_trans/trans/mir/constant.rs

+38-28
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use llvm::ValueRef;
12+
use middle::ty::Ty;
1213
use rustc::middle::const_eval::ConstVal;
1314
use rustc_mir::repr as mir;
1415
use trans::consts::{self, TrueConst};
@@ -18,45 +19,54 @@ use trans::type_of;
1819
use super::MirContext;
1920

2021
impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
22+
pub fn trans_constval(&mut self,
23+
bcx: Block<'bcx, 'tcx>,
24+
cv: &ConstVal,
25+
ty: Ty<'tcx>)
26+
-> ValueRef
27+
{
28+
let ccx = bcx.ccx();
29+
let llty = type_of::type_of(ccx, ty);
30+
match *cv {
31+
ConstVal::Float(v) => common::C_floating_f64(v, llty),
32+
ConstVal::Bool(v) => common::C_bool(ccx, v),
33+
ConstVal::Int(v) => common::C_integral(llty, v as u64, true),
34+
ConstVal::Uint(v) => common::C_integral(llty, v, false),
35+
ConstVal::Str(ref v) => common::C_str_slice(ccx, v.clone()),
36+
ConstVal::ByteStr(ref v) => consts::addr_of(ccx,
37+
common::C_bytes(ccx, v),
38+
1,
39+
"byte_str"),
40+
ConstVal::Struct(id) | ConstVal::Tuple(id) => {
41+
let expr = bcx.tcx().map.expect_expr(id);
42+
let (llval, _) = match consts::const_expr(ccx,
43+
expr,
44+
bcx.fcx.param_substs,
45+
None,
46+
TrueConst::Yes) {
47+
Ok(v) => v,
48+
Err(_) => panic!("constant eval failure"),
49+
};
50+
llval
51+
}
52+
ConstVal::Function(_) => {
53+
unimplemented!()
54+
}
55+
}
56+
}
57+
2158
pub fn trans_constant(&mut self,
2259
bcx: Block<'bcx, 'tcx>,
2360
constant: &mir::Constant<'tcx>)
2461
-> ValueRef
2562
{
26-
let ccx = bcx.ccx();
2763
let constant_ty = bcx.monomorphize(&constant.ty);
28-
let llty = type_of::type_of(ccx, constant_ty);
2964
match constant.literal {
3065
mir::Literal::Item { .. } => {
3166
unimplemented!()
3267
}
3368
mir::Literal::Value { ref value } => {
34-
match *value {
35-
ConstVal::Float(v) => common::C_floating_f64(v, llty),
36-
ConstVal::Bool(v) => common::C_bool(ccx, v),
37-
ConstVal::Int(v) => common::C_integral(llty, v as u64, true),
38-
ConstVal::Uint(v) => common::C_integral(llty, v, false),
39-
ConstVal::Str(ref v) => common::C_str_slice(ccx, v.clone()),
40-
ConstVal::ByteStr(ref v) => consts::addr_of(ccx,
41-
common::C_bytes(ccx, v),
42-
1,
43-
"byte_str"),
44-
ConstVal::Struct(id) | ConstVal::Tuple(id) => {
45-
let expr = bcx.tcx().map.expect_expr(id);
46-
let (llval, _) = match consts::const_expr(ccx,
47-
expr,
48-
bcx.fcx.param_substs,
49-
None,
50-
TrueConst::Yes) {
51-
Ok(v) => v,
52-
Err(_) => panic!("constant eval failure"),
53-
};
54-
llval
55-
}
56-
ConstVal::Function(_) => {
57-
unimplemented!()
58-
}
59-
}
69+
self.trans_constval(bcx, value, constant_ty)
6070
}
6171
}
6272
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(rustc_attrs)]
12+
13+
#[rustc_mir]
14+
pub fn foo(x: i8) -> i32 {
15+
match x {
16+
1 => 0,
17+
_ => 1,
18+
}
19+
}
20+
21+
fn main() {
22+
assert_eq!(foo(0), 1);
23+
assert_eq!(foo(1), 0);
24+
}

0 commit comments

Comments
 (0)