@@ -168,12 +168,20 @@ pub(crate) fn codegen_int_binop<'tcx>(
168
168
BinOp :: BitXor => b. bxor ( lhs, rhs) ,
169
169
BinOp :: BitAnd => b. band ( lhs, rhs) ,
170
170
BinOp :: BitOr => b. bor ( lhs, rhs) ,
171
- BinOp :: Shl => fx. bcx . ins ( ) . ishl ( lhs, rhs) ,
171
+ BinOp :: Shl => {
172
+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
173
+ let actual_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
174
+ let actual_shift = clif_intcast ( fx, actual_shift, types:: I8 , false ) ;
175
+ fx. bcx . ins ( ) . ishl ( lhs, actual_shift)
176
+ }
172
177
BinOp :: Shr => {
178
+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
179
+ let actual_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
180
+ let actual_shift = clif_intcast ( fx, actual_shift, types:: I8 , false ) ;
173
181
if signed {
174
- fx. bcx . ins ( ) . sshr ( lhs, rhs )
182
+ fx. bcx . ins ( ) . sshr ( lhs, actual_shift )
175
183
} else {
176
- fx. bcx . ins ( ) . ushr ( lhs, rhs )
184
+ fx. bcx . ins ( ) . ushr ( lhs, actual_shift )
177
185
}
178
186
}
179
187
// Compare binops handles by `codegen_binop`.
@@ -295,7 +303,10 @@ pub(crate) fn codegen_checked_int_binop<'tcx>(
295
303
}
296
304
}
297
305
BinOp :: Shl => {
298
- let val = fx. bcx . ins ( ) . ishl ( lhs, rhs) ;
306
+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
307
+ let actual_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
308
+ let actual_shift = clif_intcast ( fx, actual_shift, types:: I8 , false ) ;
309
+ let val = fx. bcx . ins ( ) . ishl ( lhs, actual_shift) ;
299
310
let ty = fx. bcx . func . dfg . value_type ( val) ;
300
311
let max_shift = i64:: from ( ty. bits ( ) ) - 1 ;
301
312
let has_overflow = fx
@@ -305,10 +316,13 @@ pub(crate) fn codegen_checked_int_binop<'tcx>(
305
316
( val, has_overflow)
306
317
}
307
318
BinOp :: Shr => {
319
+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
320
+ let actual_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
321
+ let actual_shift = clif_intcast ( fx, actual_shift, types:: I8 , false ) ;
308
322
let val = if !signed {
309
- fx. bcx . ins ( ) . ushr ( lhs, rhs )
323
+ fx. bcx . ins ( ) . ushr ( lhs, actual_shift )
310
324
} else {
311
- fx. bcx . ins ( ) . sshr ( lhs, rhs )
325
+ fx. bcx . ins ( ) . sshr ( lhs, actual_shift )
312
326
} ;
313
327
let ty = fx. bcx . func . dfg . value_type ( val) ;
314
328
let max_shift = i64:: from ( ty. bits ( ) ) - 1 ;
0 commit comments