Implement shl and shr overflow checks

cc #6
This commit is contained in:
bjorn3 2019-09-14 12:13:23 +02:00
parent 6ea4cbdf1b
commit 8f12b87542

View file

@ -258,8 +258,10 @@ pub fn trans_checked_int_binop<'tcx>(
}
BinOp::Shl => {
let val = fx.bcx.ins().ishl(lhs, rhs);
// TODO: check for overflow
let has_overflow = fx.bcx.ins().bconst(types::B1, false);
let ty = fx.bcx.func.dfg.value_type(val);
let max_shift = i64::from(ty.bits()) - 1;
let has_overflow =
fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift);
(val, has_overflow)
}
BinOp::Shr => {
@ -268,8 +270,10 @@ pub fn trans_checked_int_binop<'tcx>(
} else {
fx.bcx.ins().sshr(lhs, rhs)
};
// TODO: check for overflow
let has_overflow = fx.bcx.ins().bconst(types::B1, false);
let ty = fx.bcx.func.dfg.value_type(val);
let max_shift = i64::from(ty.bits()) - 1;
let has_overflow =
fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift);
(val, has_overflow)
}
_ => bug!(