@@ -241,7 +241,7 @@ fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>)
241
241
} else {
242
242
fx. bcx . ins ( ) . brz ( cond, target, & [ ] ) ;
243
243
} ;
244
- trap_panic ( fx, format ! ( "[panic] Assert {:?} failed." , msg) ) ;
244
+ trap_panic ( fx, format ! ( "[panic] Assert {:?} failed at {:?} ." , msg, bb_data . terminator ( ) . source_info . span ) ) ;
245
245
}
246
246
247
247
TerminatorKind :: SwitchInt {
@@ -948,17 +948,62 @@ pub fn trans_checked_int_binop<'a, 'tcx: 'a>(
948
948
949
949
let lhs = in_lhs. load_scalar ( fx) ;
950
950
let rhs = in_rhs. load_scalar ( fx) ;
951
- let res = match bin_op {
952
- BinOp :: Add => fx. bcx . ins ( ) . iadd ( lhs, rhs) ,
953
- BinOp :: Sub => fx. bcx . ins ( ) . isub ( lhs, rhs) ,
954
- BinOp :: Mul => fx. bcx . ins ( ) . imul ( lhs, rhs) ,
955
- BinOp :: Shl => fx. bcx . ins ( ) . ishl ( lhs, rhs) ,
951
+ let ( res, has_overflow) = match bin_op {
952
+ BinOp :: Add => {
953
+ /*let (val, c_out) = fx.bcx.ins().iadd_cout(lhs, rhs);
954
+ (val, c_out)*/
955
+ // FIXME(CraneStation/cranelift#849) legalize iadd_cout for i8 and i16
956
+ let val = fx. bcx . ins ( ) . iadd ( lhs, rhs) ;
957
+ let has_overflow = if !signed {
958
+ fx. bcx . ins ( ) . icmp ( IntCC :: UnsignedLessThan , val, lhs)
959
+ } else {
960
+ let rhs_is_negative = fx. bcx . ins ( ) . icmp_imm ( IntCC :: SignedLessThan , rhs, 0 ) ;
961
+ let slt = fx. bcx . ins ( ) . icmp ( IntCC :: SignedLessThan , val, lhs) ;
962
+ fx. bcx . ins ( ) . bxor ( rhs_is_negative, slt)
963
+ } ;
964
+ ( val, has_overflow)
965
+ }
966
+ BinOp :: Sub => {
967
+ /*let (val, b_out) = fx.bcx.ins().isub_bout(lhs, rhs);
968
+ (val, b_out)*/
969
+ // FIXME(CraneStation/cranelift#849) legalize isub_bout for i8 and i16
970
+ let val = fx. bcx . ins ( ) . isub ( lhs, rhs) ;
971
+ let has_overflow = if !signed {
972
+ fx. bcx . ins ( ) . icmp ( IntCC :: UnsignedGreaterThan , val, lhs)
973
+ } else {
974
+ let rhs_is_negative = fx. bcx . ins ( ) . icmp_imm ( IntCC :: SignedLessThan , rhs, 0 ) ;
975
+ let sgt = fx. bcx . ins ( ) . icmp ( IntCC :: SignedGreaterThan , val, lhs) ;
976
+ fx. bcx . ins ( ) . bxor ( rhs_is_negative, sgt)
977
+ } ;
978
+ ( val, has_overflow)
979
+ }
980
+ BinOp :: Mul => {
981
+ let val = fx. bcx . ins ( ) . imul ( lhs, rhs) ;
982
+ /*let val_hi = if !signed {
983
+ fx.bcx.ins().umulhi(lhs, rhs)
984
+ } else {
985
+ fx.bcx.ins().smulhi(lhs, rhs)
986
+ };
987
+ let has_overflow = fx.bcx.ins().icmp_imm(IntCC::NotEqual, val_hi, 0);*/
988
+ // TODO: check for overflow
989
+ let has_overflow = fx. bcx . ins ( ) . bconst ( types:: B1 , false ) ;
990
+ ( val, has_overflow)
991
+ }
992
+ BinOp :: Shl => {
993
+ let val = fx. bcx . ins ( ) . ishl ( lhs, rhs) ;
994
+ // TODO: check for overflow
995
+ let has_overflow = fx. bcx . ins ( ) . bconst ( types:: B1 , false ) ;
996
+ ( val, has_overflow)
997
+ }
956
998
BinOp :: Shr => {
957
- if !signed {
999
+ let val = if !signed {
958
1000
fx. bcx . ins ( ) . ushr ( lhs, rhs)
959
1001
} else {
960
1002
fx. bcx . ins ( ) . sshr ( lhs, rhs)
961
- }
1003
+ } ;
1004
+ // TODO: check for overflow
1005
+ let has_overflow = fx. bcx . ins ( ) . bconst ( types:: B1 , false ) ;
1006
+ ( val, has_overflow)
962
1007
}
963
1008
_ => bug ! (
964
1009
"binop {:?} on checked int/uint lhs: {:?} rhs: {:?}" ,
@@ -968,9 +1013,7 @@ pub fn trans_checked_int_binop<'a, 'tcx: 'a>(
968
1013
) ,
969
1014
} ;
970
1015
971
- // TODO: check for overflow
972
- let has_overflow = fx. bcx . ins ( ) . iconst ( types:: I8 , 0 ) ;
973
-
1016
+ let has_overflow = fx. bcx . ins ( ) . bint ( types:: I8 , has_overflow) ;
974
1017
let out_place = CPlace :: new_stack_slot ( fx, out_ty) ;
975
1018
let out_layout = out_place. layout ( ) ;
976
1019
out_place. write_cvalue ( fx, CValue :: by_val_pair ( res, has_overflow, out_layout) ) ;
0 commit comments