@@ -152,7 +152,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
152
152
match output_ty {
153
153
ty:: FnConverging ( ty) => {
154
154
let size = self . type_size_with_substs ( ty, substs) ;
155
- self . memory . allocate ( size) . map ( Some )
155
+ let align = self . type_align_with_substs ( ty, substs) ;
156
+ self . memory . allocate ( size, align) . map ( Some )
156
157
}
157
158
ty:: FnDiverging => Ok ( None ) ,
158
159
}
@@ -176,19 +177,19 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
176
177
use rustc_const_math:: { ConstInt , ConstIsize , ConstUsize , ConstFloat } ;
177
178
macro_rules! i2p {
178
179
( $i: ident, $n: expr) => { {
179
- let ptr = self . memory. allocate( $n) ?;
180
+ let ptr = self . memory. allocate( $n, $n ) ?;
180
181
self . memory. write_int( ptr, $i as i64 , $n) ?;
181
182
Ok ( ptr)
182
183
} }
183
184
}
184
185
match * const_val {
185
186
Float ( ConstFloat :: F32 ( f) ) => {
186
- let ptr = self . memory . allocate ( 4 ) ?;
187
+ let ptr = self . memory . allocate ( 4 , 4 ) ?;
187
188
self . memory . write_f32 ( ptr, f) ?;
188
189
Ok ( ptr)
189
190
} ,
190
191
Float ( ConstFloat :: F64 ( f) ) => {
191
- let ptr = self . memory . allocate ( 8 ) ?;
192
+ let ptr = self . memory . allocate ( 8 , 8 ) ?;
192
193
self . memory . write_f64 ( ptr, f) ?;
193
194
Ok ( ptr)
194
195
} ,
@@ -197,42 +198,42 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
197
198
Integral ( ConstInt :: InferSigned ( _) ) => unreachable ! ( ) ,
198
199
Integral ( ConstInt :: I8 ( i) ) => i2p ! ( i, 1 ) ,
199
200
Integral ( ConstInt :: U8 ( i) ) => i2p ! ( i, 1 ) ,
201
+ Integral ( ConstInt :: Isize ( ConstIsize :: Is16 ( i) ) ) |
200
202
Integral ( ConstInt :: I16 ( i) ) => i2p ! ( i, 2 ) ,
203
+ Integral ( ConstInt :: Usize ( ConstUsize :: Us16 ( i) ) ) |
201
204
Integral ( ConstInt :: U16 ( i) ) => i2p ! ( i, 2 ) ,
205
+ Integral ( ConstInt :: Isize ( ConstIsize :: Is32 ( i) ) ) |
202
206
Integral ( ConstInt :: I32 ( i) ) => i2p ! ( i, 4 ) ,
207
+ Integral ( ConstInt :: Usize ( ConstUsize :: Us32 ( i) ) ) |
203
208
Integral ( ConstInt :: U32 ( i) ) => i2p ! ( i, 4 ) ,
209
+ Integral ( ConstInt :: Isize ( ConstIsize :: Is64 ( i) ) ) |
204
210
Integral ( ConstInt :: I64 ( i) ) => i2p ! ( i, 8 ) ,
211
+ Integral ( ConstInt :: Usize ( ConstUsize :: Us64 ( i) ) ) |
205
212
Integral ( ConstInt :: U64 ( i) ) => i2p ! ( i, 8 ) ,
206
- Integral ( ConstInt :: Isize ( ConstIsize :: Is16 ( i) ) ) => i2p ! ( i, 2 ) ,
207
- Integral ( ConstInt :: Isize ( ConstIsize :: Is32 ( i) ) ) => i2p ! ( i, 4 ) ,
208
- Integral ( ConstInt :: Isize ( ConstIsize :: Is64 ( i) ) ) => i2p ! ( i, 8 ) ,
209
- Integral ( ConstInt :: Usize ( ConstUsize :: Us16 ( i) ) ) => i2p ! ( i, 2 ) ,
210
- Integral ( ConstInt :: Usize ( ConstUsize :: Us32 ( i) ) ) => i2p ! ( i, 4 ) ,
211
- Integral ( ConstInt :: Usize ( ConstUsize :: Us64 ( i) ) ) => i2p ! ( i, 8 ) ,
212
213
Str ( ref s) => {
213
214
let psize = self . memory . pointer_size ( ) ;
214
- let static_ptr = self . memory . allocate ( s. len ( ) ) ?;
215
- let ptr = self . memory . allocate ( psize * 2 ) ?;
215
+ let static_ptr = self . memory . allocate ( s. len ( ) , 1 ) ?;
216
+ let ptr = self . memory . allocate ( psize * 2 , psize ) ?;
216
217
self . memory . write_bytes ( static_ptr, s. as_bytes ( ) ) ?;
217
218
self . memory . write_ptr ( ptr, static_ptr) ?;
218
219
self . memory . write_usize ( ptr. offset ( psize as isize ) , s. len ( ) as u64 ) ?;
219
220
Ok ( ptr)
220
221
}
221
222
ByteStr ( ref bs) => {
222
223
let psize = self . memory . pointer_size ( ) ;
223
- let static_ptr = self . memory . allocate ( bs. len ( ) ) ?;
224
- let ptr = self . memory . allocate ( psize) ?;
224
+ let static_ptr = self . memory . allocate ( bs. len ( ) , 1 ) ?;
225
+ let ptr = self . memory . allocate ( psize, psize ) ?;
225
226
self . memory . write_bytes ( static_ptr, bs) ?;
226
227
self . memory . write_ptr ( ptr, static_ptr) ?;
227
228
Ok ( ptr)
228
229
}
229
230
Bool ( b) => {
230
- let ptr = self . memory . allocate ( 1 ) ?;
231
+ let ptr = self . memory . allocate ( 1 , 1 ) ?;
231
232
self . memory . write_bool ( ptr, b) ?;
232
233
Ok ( ptr)
233
234
}
234
235
Char ( c) => {
235
- let ptr = self . memory . allocate ( 4 ) ?;
236
+ let ptr = self . memory . allocate ( 4 , 4 ) ?;
236
237
self . memory . write_uint ( ptr, c as u64 , 4 ) ?;
237
238
Ok ( ptr)
238
239
} ,
@@ -278,10 +279,18 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
278
279
self . type_size_with_substs ( ty, self . substs ( ) )
279
280
}
280
281
282
+ fn type_align ( & self , ty : Ty < ' tcx > ) -> usize {
283
+ self . type_align_with_substs ( ty, self . substs ( ) )
284
+ }
285
+
281
286
fn type_size_with_substs ( & self , ty : Ty < ' tcx > , substs : & ' tcx Substs < ' tcx > ) -> usize {
282
287
self . type_layout_with_substs ( ty, substs) . size ( & self . tcx . data_layout ) . bytes ( ) as usize
283
288
}
284
289
290
+ fn type_align_with_substs ( & self , ty : Ty < ' tcx > , substs : & ' tcx Substs < ' tcx > ) -> usize {
291
+ self . type_layout_with_substs ( ty, substs) . align ( & self . tcx . data_layout ) . abi ( ) as usize
292
+ }
293
+
285
294
fn type_layout ( & self , ty : Ty < ' tcx > ) -> & ' tcx Layout {
286
295
self . type_layout_with_substs ( ty, self . substs ( ) )
287
296
}
@@ -315,7 +324,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
315
324
316
325
let locals: EvalResult < ' tcx , Vec < Pointer > > = arg_tys. chain ( var_tys) . chain ( temp_tys) . map ( |ty| {
317
326
let size = self . type_size_with_substs ( ty, substs) ;
318
- self . memory . allocate ( size)
327
+ let align = self . type_align_with_substs ( ty, substs) ;
328
+ self . memory . allocate ( size, align)
319
329
} ) . collect ( ) ;
320
330
321
331
self . stack . push ( Frame {
@@ -519,15 +529,15 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
519
529
}
520
530
521
531
Repeat ( ref operand, _) => {
522
- let ( elem_size, length) = match dest_ty. sty {
523
- ty:: TyArray ( elem_ty, n) => ( self . type_size ( elem_ty) , n) ,
532
+ let ( elem_size, elem_align , length) = match dest_ty. sty {
533
+ ty:: TyArray ( elem_ty, n) => ( self . type_size ( elem_ty) , self . type_align ( elem_ty ) , n) ,
524
534
_ => panic ! ( "tried to assign array-repeat to non-array type {:?}" , dest_ty) ,
525
535
} ;
526
536
527
537
let src = self . eval_operand ( operand) ?;
528
538
for i in 0 ..length {
529
539
let elem_dest = dest. offset ( ( i * elem_size) as isize ) ;
530
- self . memory . copy ( src, elem_dest, elem_size) ?;
540
+ self . memory . copy ( src, elem_dest, elem_size, elem_align ) ?;
531
541
}
532
542
}
533
543
@@ -562,7 +572,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
562
572
563
573
Box ( ty) => {
564
574
let size = self . type_size ( ty) ;
565
- let ptr = self . memory . allocate ( size) ?;
575
+ let align = self . type_align ( ty) ;
576
+ let ptr = self . memory . allocate ( size, align) ?;
566
577
self . memory . write_ptr ( dest, ptr) ?;
567
578
}
568
579
@@ -593,13 +604,14 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
593
604
warn ! ( "misc cast from {:?} to {:?}" , src_ty, dest_ty) ;
594
605
let dest_size = self . type_size ( dest_ty) ;
595
606
let src_size = self . type_size ( src_ty) ;
607
+ let dest_align = self . type_align ( dest_ty) ;
596
608
597
609
// Hack to support fat pointer -> thin pointer casts to keep tests for
598
610
// other things passing for now.
599
611
let is_fat_ptr_cast = pointee_type ( src_ty) . map_or ( false , |ty| !self . type_is_sized ( ty) ) ;
600
612
601
613
if dest_size == src_size || is_fat_ptr_cast {
602
- self . memory . copy ( src, dest, dest_size) ?;
614
+ self . memory . copy ( src, dest, dest_size, dest_align ) ?;
603
615
} else {
604
616
return Err ( EvalError :: Unimplemented ( format ! ( "can't handle cast: {:?}" , rvalue) ) ) ;
605
617
}
@@ -710,7 +722,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
710
722
Item { def_id, substs } => {
711
723
if let ty:: TyFnDef ( ..) = ty. sty {
712
724
// function items are zero sized
713
- Ok ( self . memory . allocate ( 0 ) ?)
725
+ Ok ( self . memory . allocate ( 0 , 0 ) ?)
714
726
} else {
715
727
let cid = ConstantId {
716
728
def_id : def_id,
@@ -843,7 +855,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
843
855
844
856
fn move_ ( & mut self , src : Pointer , dest : Pointer , ty : Ty < ' tcx > ) -> EvalResult < ' tcx , ( ) > {
845
857
let size = self . type_size ( ty) ;
846
- self . memory . copy ( src, dest, size) ?;
858
+ let align = self . type_align ( ty) ;
859
+ self . memory . copy ( src, dest, size, align) ?;
847
860
Ok ( ( ) )
848
861
}
849
862
@@ -967,9 +980,9 @@ pub fn eval_main<'a, 'tcx: 'a>(
967
980
if mir. arg_decls . len ( ) == 2 {
968
981
// start function
969
982
let ptr_size = ecx. memory ( ) . pointer_size ( ) ;
970
- let nargs = ecx. memory_mut ( ) . allocate ( ptr_size) . expect ( "can't allocate memory for nargs" ) ;
983
+ let nargs = ecx. memory_mut ( ) . allocate ( ptr_size, ptr_size ) . expect ( "can't allocate memory for nargs" ) ;
971
984
ecx. memory_mut ( ) . write_usize ( nargs, 0 ) . unwrap ( ) ;
972
- let args = ecx. memory_mut ( ) . allocate ( ptr_size) . expect ( "can't allocate memory for arg pointer" ) ;
985
+ let args = ecx. memory_mut ( ) . allocate ( ptr_size, ptr_size ) . expect ( "can't allocate memory for arg pointer" ) ;
973
986
ecx. memory_mut ( ) . write_usize ( args, 0 ) . unwrap ( ) ;
974
987
ecx. frame_mut ( ) . locals [ 0 ] = nargs;
975
988
ecx. frame_mut ( ) . locals [ 1 ] = args;
0 commit comments