@@ -175,17 +175,24 @@ fn insert_rust_realloc(asm: &mut Assembly, patcher: &mut MissingMethodPatcher, u
175
175
Box :: new ( super :: cilnode:: PtrCastRes :: Ptr ( void_idx) ) ,
176
176
) ) ;
177
177
let align = asm. alloc_node ( CILNode :: LdArg ( 2 ) ) ;
178
- let new_size = asm . alloc_node ( CILNode :: LdArg ( 3 ) ) ;
178
+
179
179
let align = asm. alloc_node ( CILNode :: IntCast {
180
180
input : align,
181
181
target : Int :: USize ,
182
182
extend : super :: cilnode:: ExtendKind :: ZeroExtend ,
183
183
} ) ;
184
+ let new_size = asm. alloc_node ( CILNode :: LdArg ( 3 ) ) ;
184
185
let new_size = asm. alloc_node ( CILNode :: IntCast {
185
186
input : new_size,
186
187
target : Int :: USize ,
187
188
extend : super :: cilnode:: ExtendKind :: ZeroExtend ,
188
189
} ) ;
190
+ let old_size = asm. alloc_node ( CILNode :: LdArg ( 1 ) ) ;
191
+ let old_size = asm. alloc_node ( CILNode :: IntCast {
192
+ input : old_size,
193
+ target : Int :: USize ,
194
+ extend : super :: cilnode:: ExtendKind :: ZeroExtend ,
195
+ } ) ;
189
196
let void_ptr = asm. nptr ( Type :: Void ) ;
190
197
let mm_malloc_sig = asm. sig ( [ Type :: Int ( Int :: USize ) , Type :: Int ( Int :: USize ) ] , void_ptr) ;
191
198
// 1. call _mm_malloc
@@ -205,7 +212,7 @@ fn insert_rust_realloc(asm: &mut Assembly, patcher: &mut MissingMethodPatcher, u
205
212
let call_mm_malloc = asm. alloc_root ( CILRoot :: StLoc ( 0 , _mm_malloc) ) ;
206
213
// 2. memcpy the buffer.
207
214
let buff = asm. alloc_node ( CILNode :: LdLoc ( 0 ) ) ;
208
- let copy = asm. alloc_root ( CILRoot :: CpBlk ( Box :: new ( ( buff, ptr, new_size ) ) ) ) ;
215
+ let copy = asm. alloc_root ( CILRoot :: CpBlk ( Box :: new ( ( buff, ptr, old_size ) ) ) ) ;
209
216
// 3. free the old buffer
210
217
let aligned_free = asm. alloc_string ( "_mm_free" ) ;
211
218
let mm_free_sig = asm. sig ( [ void_ptr] , Type :: Void ) ;
@@ -545,20 +552,37 @@ pub fn transmute(asm: &mut Assembly, patcher: &mut MissingMethodPatcher) {
545
552
let name = asm. alloc_string ( "transmute" ) ;
546
553
let generator = move |mref : MethodRefIdx , asm : & mut Assembly | {
547
554
let target = * asm[ asm[ mref] . sig ( ) ] . output ( ) ;
555
+ let source = asm[ asm[ mref] . sig ( ) ] . inputs ( ) [ 0 ] ;
556
+ let source = asm. alloc_type ( source) ;
548
557
let target_idx = asm. alloc_type ( target) ;
549
558
let addr = asm. alloc_node ( CILNode :: LdArgA ( 0 ) ) ;
550
- let ptr = asm. alloc_node ( CILNode :: RefToPtr ( addr) ) ;
551
- let ptr = asm. alloc_node ( CILNode :: PtrCast ( ptr, Box :: new ( PtrCastRes :: Ptr ( target_idx) ) ) ) ;
552
- let valuetype = asm. alloc_node ( CILNode :: LdInd {
553
- addr : ptr,
554
- tpe : target_idx,
555
- volatile : false ,
556
- } ) ;
557
- let ret = asm. alloc_root ( CILRoot :: Ret ( valuetype) ) ;
558
- MethodImpl :: MethodBody {
559
- blocks : vec ! [ BasicBlock :: new( vec![ ret] , 0 , None ) ] ,
560
- locals : vec ! [ ] ,
559
+ if asm. alignof_type ( source) >= asm. alignof_type ( target_idx) {
560
+ let ptr = asm. alloc_node ( CILNode :: RefToPtr ( addr) ) ;
561
+ let ptr = asm. alloc_node ( CILNode :: PtrCast ( ptr, Box :: new ( PtrCastRes :: Ptr ( target_idx) ) ) ) ;
562
+ let valuetype = asm. alloc_node ( CILNode :: LdInd {
563
+ addr : ptr,
564
+ tpe : target_idx,
565
+ volatile : false ,
566
+ } ) ;
567
+ let ret = asm. alloc_root ( CILRoot :: Ret ( valuetype) ) ;
568
+ MethodImpl :: MethodBody {
569
+ blocks : vec ! [ BasicBlock :: new( vec![ ret] , 0 , None ) ] ,
570
+ locals : vec ! [ ] ,
571
+ }
572
+ } else {
573
+ let dst = asm. alloc_node ( CILNode :: LdLocA ( 0 ) ) ;
574
+ let size = asm. alloc_node ( CILNode :: SizeOf ( source) ) ;
575
+ let load = asm. alloc_root ( CILRoot :: CpBlk ( Box :: new ( ( dst, addr, size) ) ) ) ;
576
+ let ret = asm. alloc_node ( CILNode :: LdLoc ( 0 ) ) ;
577
+ let ret = asm. alloc_root ( CILRoot :: Ret ( ret) ) ;
578
+ MethodImpl :: MethodBody {
579
+ blocks : vec ! [ BasicBlock :: new( vec![ load, ret] , 0 , None ) ] ,
580
+ locals : vec ! [ ( None , target_idx) ] ,
581
+ }
582
+
561
583
}
584
+
585
+
562
586
} ;
563
587
patcher. insert ( name, Box :: new ( generator) ) ;
564
588
}
0 commit comments