@@ -9,7 +9,6 @@ use crate::MemFlags;
9
9
10
10
use rustc_hir as hir;
11
11
use rustc_middle:: mir;
12
- use rustc_middle:: ty:: cast:: { CastTy , IntTy } ;
13
12
use rustc_middle:: ty:: layout:: { HasTyCtxt , LayoutOf , TyAndLayout } ;
14
13
use rustc_middle:: ty:: { self , adjustment:: PointerCoercion , Instance , Ty , TyCtxt } ;
15
14
use rustc_middle:: { bug, span_bug} ;
@@ -239,21 +238,21 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
239
238
}
240
239
}
241
240
OperandValue :: Immediate ( imm) => {
242
- let OperandValueKind :: Immediate ( in_scalar ) = operand_kind else {
241
+ let OperandValueKind :: Immediate ( from_scalar ) = operand_kind else {
243
242
bug ! ( "Found {operand_kind:?} for operand {operand:?}" ) ;
244
243
} ;
245
- if let OperandValueKind :: Immediate ( out_scalar ) = cast_kind
246
- && in_scalar . size ( self . cx ) == out_scalar . size ( self . cx )
244
+ if let OperandValueKind :: Immediate ( to_scalar ) = cast_kind
245
+ && from_scalar . size ( self . cx ) == to_scalar . size ( self . cx )
247
246
{
248
- let operand_bty = bx. backend_type ( operand. layout ) ;
249
- let cast_bty = bx. backend_type ( cast) ;
247
+ let from_backend_ty = bx. backend_type ( operand. layout ) ;
248
+ let to_backend_ty = bx. backend_type ( cast) ;
250
249
Some ( OperandValue :: Immediate ( self . transmute_immediate (
251
250
bx,
252
251
imm,
253
- in_scalar ,
254
- operand_bty ,
255
- out_scalar ,
256
- cast_bty ,
252
+ from_scalar ,
253
+ from_backend_ty ,
254
+ to_scalar ,
255
+ to_backend_ty ,
257
256
) ) )
258
257
} else {
259
258
None
@@ -282,6 +281,59 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
282
281
}
283
282
}
284
283
284
+ /// Cast one of the immediates from an [`OperandValue::Immediate`]
285
+ /// or an [`OperandValue::Pair`] to an immediate of the target type.
286
+ fn cast_immediate (
287
+ & self ,
288
+ bx : & mut Bx ,
289
+ mut imm : Bx :: Value ,
290
+ from_scalar : abi:: Scalar ,
291
+ from_backend_ty : Bx :: Type ,
292
+ to_scalar : abi:: Scalar ,
293
+ to_backend_ty : Bx :: Type ,
294
+ ) -> Option < Bx :: Value > {
295
+ use abi:: Primitive :: * ;
296
+
297
+ // When scalars are passed by value, there's no metadata recording their
298
+ // valid ranges. For example, `char`s are passed as just `i32`, with no
299
+ // way for LLVM to know that they're 0x10FFFF at most. Thus we assume
300
+ // the range of the input value too, not just the output range.
301
+ self . assume_scalar_range ( bx, imm, from_scalar, from_backend_ty) ;
302
+
303
+ imm = match ( from_scalar. primitive ( ) , to_scalar. primitive ( ) ) {
304
+ ( Int ( _, is_signed) , Int ( ..) ) => bx. intcast ( imm, to_backend_ty, is_signed) ,
305
+ ( Float ( _) , Float ( _) ) => {
306
+ let srcsz = bx. cx ( ) . float_width ( from_backend_ty) ;
307
+ let dstsz = bx. cx ( ) . float_width ( to_backend_ty) ;
308
+ if dstsz > srcsz {
309
+ bx. fpext ( imm, to_backend_ty)
310
+ } else if srcsz > dstsz {
311
+ bx. fptrunc ( imm, to_backend_ty)
312
+ } else {
313
+ imm
314
+ }
315
+ }
316
+ ( Int ( _, is_signed) , Float ( _) ) => {
317
+ if is_signed {
318
+ bx. sitofp ( imm, to_backend_ty)
319
+ } else {
320
+ bx. uitofp ( imm, to_backend_ty)
321
+ }
322
+ }
323
+ ( Pointer ( ..) , Pointer ( ..) ) => bx. pointercast ( imm, to_backend_ty) ,
324
+ ( Int ( _, is_signed) , Pointer ( ..) ) => {
325
+ let usize_imm = bx. intcast ( imm, bx. cx ( ) . type_isize ( ) , is_signed) ;
326
+ bx. inttoptr ( usize_imm, to_backend_ty)
327
+ }
328
+ ( Float ( _) , Int ( _, is_signed) ) => {
329
+ bx. cast_float_to_int ( is_signed, imm, to_backend_ty)
330
+ }
331
+ _ => return None ,
332
+ } ;
333
+ self . assume_scalar_range ( bx, imm, to_scalar, to_backend_ty) ;
334
+ Some ( imm)
335
+ }
336
+
285
337
/// Transmutes one of the immediates from an [`OperandValue::Immediate`]
286
338
/// or an [`OperandValue::Pair`] to an immediate of the target type.
287
339
///
@@ -508,62 +560,33 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
508
560
| mir:: CastKind :: IntToFloat
509
561
| mir:: CastKind :: PtrToPtr
510
562
| mir:: CastKind :: FnPtrToPtr
511
-
512
563
// Since int2ptr can have arbitrary integer types as input (so we have to do
513
564
// sign extension and all that), it is currently best handled in the same code
514
565
// path as the other integer-to-X casts.
515
566
| mir:: CastKind :: PointerWithExposedProvenance => {
567
+ let imm = operand. immediate ( ) ;
568
+ let operand_kind = self . value_kind ( operand. layout ) ;
569
+ let OperandValueKind :: Immediate ( from_scalar) = operand_kind else {
570
+ bug ! ( "Found {operand_kind:?} for operand {operand:?}" ) ;
571
+ } ;
572
+ let from_backend_ty = bx. cx ( ) . immediate_backend_type ( operand. layout ) ;
573
+
516
574
assert ! ( bx. cx( ) . is_backend_immediate( cast) ) ;
517
- let ll_t_out = bx. cx ( ) . immediate_backend_type ( cast) ;
575
+ let to_backend_ty = bx. cx ( ) . immediate_backend_type ( cast) ;
518
576
if operand. layout . abi . is_uninhabited ( ) {
519
- let val = OperandValue :: Immediate ( bx. cx ( ) . const_poison ( ll_t_out ) ) ;
577
+ let val = OperandValue :: Immediate ( bx. cx ( ) . const_poison ( to_backend_ty ) ) ;
520
578
return OperandRef { val, layout : cast } ;
521
579
}
522
- let r_t_in =
523
- CastTy :: from_ty ( operand. layout . ty ) . expect ( "bad input type for cast" ) ;
524
- let r_t_out = CastTy :: from_ty ( cast. ty ) . expect ( "bad output type for cast" ) ;
525
- let ll_t_in = bx. cx ( ) . immediate_backend_type ( operand. layout ) ;
526
- let llval = operand. immediate ( ) ;
527
-
528
- let newval = match ( r_t_in, r_t_out) {
529
- ( CastTy :: Int ( i) , CastTy :: Int ( _) ) => {
530
- bx. intcast ( llval, ll_t_out, i. is_signed ( ) )
531
- }
532
- ( CastTy :: Float , CastTy :: Float ) => {
533
- let srcsz = bx. cx ( ) . float_width ( ll_t_in) ;
534
- let dstsz = bx. cx ( ) . float_width ( ll_t_out) ;
535
- if dstsz > srcsz {
536
- bx. fpext ( llval, ll_t_out)
537
- } else if srcsz > dstsz {
538
- bx. fptrunc ( llval, ll_t_out)
539
- } else {
540
- llval
541
- }
542
- }
543
- ( CastTy :: Int ( i) , CastTy :: Float ) => {
544
- if i. is_signed ( ) {
545
- bx. sitofp ( llval, ll_t_out)
546
- } else {
547
- bx. uitofp ( llval, ll_t_out)
548
- }
549
- }
550
- ( CastTy :: Ptr ( _) | CastTy :: FnPtr , CastTy :: Ptr ( _) ) => {
551
- bx. pointercast ( llval, ll_t_out)
552
- }
553
- ( CastTy :: Int ( i) , CastTy :: Ptr ( _) ) => {
554
- let usize_llval =
555
- bx. intcast ( llval, bx. cx ( ) . type_isize ( ) , i. is_signed ( ) ) ;
556
- bx. inttoptr ( usize_llval, ll_t_out)
557
- }
558
- ( CastTy :: Float , CastTy :: Int ( IntTy :: I ) ) => {
559
- bx. cast_float_to_int ( true , llval, ll_t_out)
560
- }
561
- ( CastTy :: Float , CastTy :: Int ( _) ) => {
562
- bx. cast_float_to_int ( false , llval, ll_t_out)
563
- }
564
- _ => bug ! ( "unsupported cast: {:?} to {:?}" , operand. layout. ty, cast. ty) ,
580
+ let cast_kind = self . value_kind ( cast) ;
581
+ let OperandValueKind :: Immediate ( to_scalar) = cast_kind else {
582
+ bug ! ( "Found {cast_kind:?} for operand {cast:?}" ) ;
565
583
} ;
566
- OperandValue :: Immediate ( newval)
584
+
585
+ self . cast_immediate ( bx, imm, from_scalar, from_backend_ty, to_scalar, to_backend_ty)
586
+ . map ( OperandValue :: Immediate )
587
+ . unwrap_or_else ( || {
588
+ bug ! ( "Unsupported cast of {operand:?} to {cast:?}" ) ;
589
+ } )
567
590
}
568
591
mir:: CastKind :: Transmute => {
569
592
self . codegen_transmute_operand ( bx, operand, cast) . unwrap_or_else ( || {
0 commit comments