@@ -39,7 +39,18 @@ fn apply_arg_attrs_to_abi_param(mut param: AbiParam, arg_attrs: ArgAttributes) -
3939 param
4040}
4141
42- fn cast_target_to_abi_params ( cast : & CastTarget ) -> SmallVec < [ AbiParam ; 2 ] > {
42+ fn cast_target_to_abi_params ( cast : & CastTarget ) -> SmallVec < [ ( Size , AbiParam ) ; 2 ] > {
43+ if let Some ( offset_from_start) = cast. rest_offset {
44+ assert ! ( cast. prefix[ 1 ..] . iter( ) . all( |p| p. is_none( ) ) ) ;
45+ assert_eq ! ( cast. rest. unit. size, cast. rest. total) ;
46+ let first = cast. prefix [ 0 ] . unwrap ( ) ;
47+ let second = cast. rest . unit ;
48+ return smallvec ! [
49+ ( Size :: ZERO , reg_to_abi_param( first) ) ,
50+ ( offset_from_start, reg_to_abi_param( second) )
51+ ] ;
52+ }
53+
4354 let ( rest_count, rem_bytes) = if cast. rest . unit . size . bytes ( ) == 0 {
4455 ( 0 , 0 )
4556 } else {
@@ -54,25 +65,32 @@ fn cast_target_to_abi_params(cast: &CastTarget) -> SmallVec<[AbiParam; 2]> {
5465 // different types in Cranelift IR. Instead a single array of primitive types is used.
5566
5667 // Create list of fields in the main structure
57- let mut args = cast
68+ let args = cast
5869 . prefix
5970 . iter ( )
6071 . flatten ( )
6172 . map ( |& reg| reg_to_abi_param ( reg) )
62- . chain ( ( 0 ..rest_count) . map ( |_| reg_to_abi_param ( cast. rest . unit ) ) )
63- . collect :: < SmallVec < _ > > ( ) ;
73+ . chain ( ( 0 ..rest_count) . map ( |_| reg_to_abi_param ( cast. rest . unit ) ) ) ;
74+
75+ let mut res = SmallVec :: new ( ) ;
76+ let mut offset = Size :: ZERO ;
77+
78+ for arg in args {
79+ res. push ( ( offset, arg) ) ;
80+ offset += Size :: from_bytes ( arg. value_type . bytes ( ) ) ;
81+ }
6482
6583 // Append final integer
6684 if rem_bytes != 0 {
6785 // Only integers can be really split further.
6886 assert_eq ! ( cast. rest. unit. kind, RegKind :: Integer ) ;
69- args . push ( reg_to_abi_param ( Reg {
70- kind : RegKind :: Integer ,
71- size : Size :: from_bytes ( rem_bytes) ,
72- } ) ) ;
87+ res . push ( (
88+ offset ,
89+ reg_to_abi_param ( Reg { kind : RegKind :: Integer , size : Size :: from_bytes ( rem_bytes) } ) ,
90+ ) ) ;
7391 }
7492
75- args
93+ res
7694}
7795
7896impl < ' tcx > ArgAbiExt < ' tcx > for ArgAbi < ' tcx , Ty < ' tcx > > {
@@ -103,7 +121,7 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
103121 } ,
104122 PassMode :: Cast { ref cast, pad_i32 } => {
105123 assert ! ( !pad_i32, "padding support not yet implemented" ) ;
106- cast_target_to_abi_params ( cast)
124+ cast_target_to_abi_params ( cast) . into_iter ( ) . map ( | ( _ , param ) | param ) . collect ( )
107125 }
108126 PassMode :: Indirect { attrs, meta_attrs : None , on_stack } => {
109127 if on_stack {
@@ -149,9 +167,10 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
149167 }
150168 _ => unreachable ! ( "{:?}" , self . layout. backend_repr) ,
151169 } ,
152- PassMode :: Cast { ref cast, .. } => {
153- ( None , cast_target_to_abi_params ( cast) . into_iter ( ) . collect ( ) )
154- }
170+ PassMode :: Cast { ref cast, .. } => (
171+ None ,
172+ cast_target_to_abi_params ( cast) . into_iter ( ) . map ( |( _, param) | param) . collect ( ) ,
173+ ) ,
155174 PassMode :: Indirect { attrs : _, meta_attrs : None , on_stack } => {
156175 assert ! ( !on_stack) ;
157176 ( Some ( AbiParam :: special ( pointer_ty ( tcx) , ArgumentPurpose :: StructReturn ) ) , vec ! [ ] )
@@ -170,12 +189,14 @@ pub(super) fn to_casted_value<'tcx>(
170189) -> SmallVec < [ Value ; 2 ] > {
171190 let ( ptr, meta) = arg. force_stack ( fx) ;
172191 assert ! ( meta. is_none( ) ) ;
173- let mut offset = 0 ;
174192 cast_target_to_abi_params ( cast)
175193 . into_iter ( )
176- . map ( |param| {
177- let val = ptr. offset_i64 ( fx, offset) . load ( fx, param. value_type , MemFlags :: new ( ) ) ;
178- offset += i64:: from ( param. value_type . bytes ( ) ) ;
194+ . map ( |( offset, param) | {
195+ let val = ptr. offset_i64 ( fx, offset. bytes ( ) as i64 ) . load (
196+ fx,
197+ param. value_type ,
198+ MemFlags :: new ( ) ,
199+ ) ;
179200 val
180201 } )
181202 . collect ( )
@@ -188,7 +209,7 @@ pub(super) fn from_casted_value<'tcx>(
188209 cast : & CastTarget ,
189210) -> CValue < ' tcx > {
190211 let abi_params = cast_target_to_abi_params ( cast) ;
191- let abi_param_size: u32 = abi_params. iter ( ) . map ( |param| param. value_type . bytes ( ) ) . sum ( ) ;
212+ let abi_param_size: u32 = abi_params. iter ( ) . map ( |( _ , param) | param. value_type . bytes ( ) ) . sum ( ) ;
192213 let layout_size = u32:: try_from ( layout. size . bytes ( ) ) . unwrap ( ) ;
193214 let ptr = fx. create_stack_slot (
194215 // Stack slot size may be bigger for example `[u8; 3]` which is packed into an `i32`.
@@ -197,16 +218,13 @@ pub(super) fn from_casted_value<'tcx>(
197218 std:: cmp:: max ( abi_param_size, layout_size) ,
198219 u32:: try_from ( layout. align . abi . bytes ( ) ) . unwrap ( ) ,
199220 ) ;
200- let mut offset = 0 ;
201221 let mut block_params_iter = block_params. iter ( ) . copied ( ) ;
202- for param in abi_params {
203- let val = ptr. offset_i64 ( fx, offset) . store (
222+ for ( offset , _ ) in abi_params {
223+ ptr. offset_i64 ( fx, offset. bytes ( ) as i64 ) . store (
204224 fx,
205225 block_params_iter. next ( ) . unwrap ( ) ,
206226 MemFlags :: new ( ) ,
207- ) ;
208- offset += i64:: from ( param. value_type . bytes ( ) ) ;
209- val
227+ )
210228 }
211229 assert_eq ! ( block_params_iter. next( ) , None , "Leftover block param" ) ;
212230 CValue :: by_ref ( ptr, layout)
0 commit comments