diff options
| author | Felix S. Klock II <pnkfelix@pnkfx.org> | 2015-04-15 02:19:26 +0200 |
|---|---|---|
| committer | Felix S. Klock II <pnkfelix@pnkfx.org> | 2015-04-17 14:45:14 +0200 |
| commit | 9a6d7fb9b068a0c35e469975d4b00992e0f9b8a8 (patch) | |
| tree | ec40996bc85875c779aa17b1edcc929887ced2ac | |
| parent | b7fb57529aded92c4f470568e6b5ea7a5a28f6a4 (diff) | |
| download | rust-9a6d7fb9b068a0c35e469975d4b00992e0f9b8a8.tar.gz rust-9a6d7fb9b068a0c35e469975d4b00992e0f9b8a8.zip | |
factor out useful helper.
| -rw-r--r-- | src/librustc_trans/trans/base.rs | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/src/librustc_trans/trans/base.rs b/src/librustc_trans/trans/base.rs index 93eb24a47de..023f9e0bda1 100644 --- a/src/librustc_trans/trans/base.rs +++ b/src/librustc_trans/trans/base.rs @@ -566,6 +566,25 @@ fn cast_shift_rhs<F, G>(op: ast::BinOp_, } } +pub fn llty_and_min_for_signed_ty<'blk, 'tcx>(cx: Block<'blk, 'tcx>, + val_t: Ty<'tcx>) -> (Type, u64) { + match val_t.sty { + ty::ty_int(t) => { + let llty = Type::int_from_ty(cx.ccx(), t); + let min = match t { + ast::TyIs if llty == Type::i32(cx.ccx()) => i32::MIN as u64, + ast::TyIs => i64::MIN as u64, + ast::TyI8 => i8::MIN as u64, + ast::TyI16 => i16::MIN as u64, + ast::TyI32 => i32::MIN as u64, + ast::TyI64 => i64::MIN as u64, + }; + (llty, min) + } + _ => unreachable!(), + } +} + pub fn fail_if_zero_or_overflows<'blk, 'tcx>( cx: Block<'blk, 'tcx>, call_info: NodeIdAndSpan, @@ -620,21 +639,7 @@ pub fn fail_if_zero_or_overflows<'blk, 'tcx>( // signed division/remainder which would trigger overflow. For unsigned // integers, no action beyond checking for zero need be taken. if is_signed { - let (llty, min) = match rhs_t.sty { - ty::ty_int(t) => { - let llty = Type::int_from_ty(cx.ccx(), t); - let min = match t { - ast::TyIs if llty == Type::i32(cx.ccx()) => i32::MIN as u64, - ast::TyIs => i64::MIN as u64, - ast::TyI8 => i8::MIN as u64, - ast::TyI16 => i16::MIN as u64, - ast::TyI32 => i32::MIN as u64, - ast::TyI64 => i64::MIN as u64, - }; - (llty, min) - } - _ => unreachable!(), - }; + let (llty, min) = llty_and_min_for_signed_ty(cx, rhs_t); let minus_one = ICmp(bcx, llvm::IntEQ, rhs, C_integral(llty, !0, false), debug_loc); with_cond(bcx, minus_one, |bcx| { |
