@@ -10,7 +10,7 @@ mod value;
10
10
11
11
pub use self :: error:: { EvalError , EvalResult , EvalErrorKind , AssertMessage } ;
12
12
13
- pub use self :: value:: { PrimVal , PrimValKind , Value , Pointer } ;
13
+ pub use self :: value:: { PrimVal , PrimValKind , Value , Pointer , ConstValue } ;
14
14
15
15
use std:: collections:: BTreeMap ;
16
16
use std:: fmt;
@@ -20,8 +20,10 @@ use ty::{self, TyCtxt};
20
20
use ty:: layout:: { self , Align , HasDataLayout } ;
21
21
use middle:: region;
22
22
use std:: iter;
23
+ use std:: io;
23
24
use syntax:: ast:: Mutability ;
24
25
use rustc_serialize:: { Encoder , Decoder , Decodable , Encodable } ;
26
+ use byteorder:: { WriteBytesExt , ReadBytesExt , LittleEndian , BigEndian } ;
25
27
26
28
#[ derive( Clone , Debug , PartialEq , RustcEncodable , RustcDecodable ) ]
27
29
pub enum Lock {
@@ -235,7 +237,7 @@ impl fmt::Display for AllocId {
235
237
}
236
238
}
237
239
238
- #[ derive( Debug , Eq , PartialEq , Hash , RustcEncodable , RustcDecodable ) ]
240
+ #[ derive( Clone , Debug , Eq , PartialEq , Hash , RustcEncodable , RustcDecodable ) ]
239
241
pub struct Allocation {
240
242
/// The actual bytes of the allocation.
241
243
/// Note that the bytes of a pointer represent the offset of the pointer
@@ -254,17 +256,69 @@ pub struct Allocation {
254
256
}
255
257
256
258
impl Allocation {
257
- pub fn from_bytes ( slice : & [ u8 ] ) -> Self {
259
+ pub fn from_bytes ( slice : & [ u8 ] , align : Align ) -> Self {
258
260
let mut undef_mask = UndefMask :: new ( 0 ) ;
259
261
undef_mask. grow ( slice. len ( ) as u64 , true ) ;
260
262
Self {
261
263
bytes : slice. to_owned ( ) ,
262
264
relocations : BTreeMap :: new ( ) ,
263
265
undef_mask,
264
- align : Align :: from_bytes ( 1 , 1 ) . unwrap ( ) ,
266
+ align,
265
267
runtime_mutability : Mutability :: Immutable ,
266
268
}
267
269
}
270
+
271
+ pub fn from_byte_aligned_bytes ( slice : & [ u8 ] ) -> Self {
272
+ Allocation :: from_bytes ( slice, Align :: from_bytes ( 1 , 1 ) . unwrap ( ) )
273
+ }
274
+
275
+ pub fn undef ( size : u64 , align : Align ) -> Self {
276
+ assert_eq ! ( size as usize as u64 , size) ;
277
+ Allocation {
278
+ bytes : vec ! [ 0 ; size as usize ] ,
279
+ relocations : BTreeMap :: new ( ) ,
280
+ undef_mask : UndefMask :: new ( size) ,
281
+ align,
282
+ runtime_mutability : Mutability :: Immutable ,
283
+ }
284
+ }
285
+ }
286
+
287
+ impl < ' tcx > :: serialize:: UseSpecializedDecodable for & ' tcx Allocation { }
288
+
289
+ ////////////////////////////////////////////////////////////////////////////////
290
+ // Methods to access integers in the target endianness
291
+ ////////////////////////////////////////////////////////////////////////////////
292
+
293
+ pub fn write_target_uint (
294
+ endianness : layout:: Endian ,
295
+ mut target : & mut [ u8 ] ,
296
+ data : u128 ,
297
+ ) -> Result < ( ) , io:: Error > {
298
+ let len = target. len ( ) ;
299
+ match endianness {
300
+ layout:: Endian :: Little => target. write_uint128 :: < LittleEndian > ( data, len) ,
301
+ layout:: Endian :: Big => target. write_uint128 :: < BigEndian > ( data, len) ,
302
+ }
303
+ }
304
+
305
+ pub fn write_target_int (
306
+ endianness : layout:: Endian ,
307
+ mut target : & mut [ u8 ] ,
308
+ data : i128 ,
309
+ ) -> Result < ( ) , io:: Error > {
310
+ let len = target. len ( ) ;
311
+ match endianness {
312
+ layout:: Endian :: Little => target. write_int128 :: < LittleEndian > ( data, len) ,
313
+ layout:: Endian :: Big => target. write_int128 :: < BigEndian > ( data, len) ,
314
+ }
315
+ }
316
+
317
+ pub fn read_target_uint ( endianness : layout:: Endian , mut source : & [ u8 ] ) -> Result < u128 , io:: Error > {
318
+ match endianness {
319
+ layout:: Endian :: Little => source. read_uint128 :: < LittleEndian > ( source. len ( ) ) ,
320
+ layout:: Endian :: Big => source. read_uint128 :: < BigEndian > ( source. len ( ) ) ,
321
+ }
268
322
}
269
323
270
324
////////////////////////////////////////////////////////////////////////////////
0 commit comments