]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/value.rs
Add riscv64gc-unknown-none-elf target
[rust.git] / src / librustc / mir / interpret / value.rs
1 use std::fmt;
2
3 use crate::ty::{Ty, layout::{HasDataLayout, Size}};
4
5 use super::{EvalResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate};
6
7 /// Represents the result of a raw const operation, pre-validation.
8 #[derive(Copy, Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable, Hash)]
9 pub struct RawConst<'tcx> {
10     // the value lives here, at offset 0, and that allocation definitely is a `AllocKind::Memory`
11     // (so you can use `AllocMap::unwrap_memory`).
12     pub alloc_id: AllocId,
13     pub ty: Ty<'tcx>,
14 }
15
16 /// Represents a constant value in Rust. Scalar and ScalarPair are optimizations which
17 /// matches the LocalState optimizations for easy conversions between Value and ConstValue.
18 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, RustcEncodable, RustcDecodable, Hash)]
19 pub enum ConstValue<'tcx> {
20     /// Used only for types with layout::abi::Scalar ABI and ZSTs
21     ///
22     /// Not using the enum `Value` to encode that this must not be `Undef`
23     Scalar(Scalar),
24
25     /// Used only for slices and strings (`&[T]`, `&str`, `*const [T]`, `*mut str`, `Box<str>`, ...)
26     ///
27     /// Empty slices don't necessarily have an address backed by an `AllocId`, thus we also need to
28     /// enable integer pointers. The `Scalar` type covers exactly those two cases. While we could
29     /// create dummy-`AllocId`s, the additional code effort for the conversions doesn't seem worth
30     /// it.
31     Slice(Scalar, u64),
32
33     /// An allocation + offset into the allocation.
34     /// Invariant: The AllocId matches the allocation.
35     ByRef(AllocId, &'tcx Allocation, Size),
36 }
37
38 #[cfg(target_arch = "x86_64")]
39 static_assert!(CONST_SIZE: ::std::mem::size_of::<ConstValue<'static>>() == 40);
40
41 impl<'tcx> ConstValue<'tcx> {
42     #[inline]
43     pub fn try_to_scalar(&self) -> Option<Scalar> {
44         match *self {
45             ConstValue::ByRef(..) |
46             ConstValue::Slice(..) => None,
47             ConstValue::Scalar(val) => Some(val),
48         }
49     }
50
51     #[inline]
52     pub fn try_to_bits(&self, size: Size) -> Option<u128> {
53         self.try_to_scalar()?.to_bits(size).ok()
54     }
55
56     #[inline]
57     pub fn try_to_ptr(&self) -> Option<Pointer> {
58         self.try_to_scalar()?.to_ptr().ok()
59     }
60
61     #[inline]
62     pub fn new_slice(
63         val: Scalar,
64         len: u64,
65     ) -> Self {
66         ConstValue::Slice(val, len)
67     }
68 }
69
70 /// A `Scalar` represents an immediate, primitive value existing outside of a
71 /// `memory::Allocation`. It is in many ways like a small chunk of a `Allocation`, up to 8 bytes in
72 /// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
73 /// of a simple value or a pointer into another `Allocation`
74 #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
75 pub enum Scalar<Tag=(), Id=AllocId> {
76     /// The raw bytes of a simple value.
77     Bits {
78         /// The first `size` bytes are the value.
79         /// Do not try to read less or more bytes than that. The remaining bytes must be 0.
80         size: u8,
81         bits: u128,
82     },
83
84     /// A pointer into an `Allocation`. An `Allocation` in the `memory` module has a list of
85     /// relocations, but a `Scalar` is only large enough to contain one, so we just represent the
86     /// relocation and its associated offset together as a `Pointer` here.
87     Ptr(Pointer<Tag, Id>),
88 }
89
90 #[cfg(target_arch = "x86_64")]
91 static_assert!(SCALAR_SIZE: ::std::mem::size_of::<Scalar>() == 24);
92
93 impl<Tag> fmt::Display for Scalar<Tag> {
94     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95         match self {
96             Scalar::Ptr(_) => write!(f, "a pointer"),
97             Scalar::Bits { bits, .. } => write!(f, "{}", bits),
98         }
99     }
100 }
101
102 impl<'tcx> Scalar<()> {
103     #[inline]
104     pub fn with_default_tag<Tag>(self) -> Scalar<Tag>
105         where Tag: Default
106     {
107         match self {
108             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.with_default_tag()),
109             Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
110         }
111     }
112 }
113
114 impl<'tcx, Tag> Scalar<Tag> {
115     #[inline]
116     pub fn erase_tag(self) -> Scalar {
117         match self {
118             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.erase_tag()),
119             Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
120         }
121     }
122
123     #[inline]
124     pub fn with_tag(self, new_tag: Tag) -> Self {
125         match self {
126             Scalar::Ptr(ptr) => Scalar::Ptr(Pointer { tag: new_tag, ..ptr }),
127             Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
128         }
129     }
130
131     #[inline]
132     pub fn ptr_null(cx: &impl HasDataLayout) -> Self {
133         Scalar::Bits {
134             bits: 0,
135             size: cx.data_layout().pointer_size.bytes() as u8,
136         }
137     }
138
139     #[inline]
140     pub fn zst() -> Self {
141         Scalar::Bits { bits: 0, size: 0 }
142     }
143
144     #[inline]
145     pub fn ptr_offset(self, i: Size, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
146         let dl = cx.data_layout();
147         match self {
148             Scalar::Bits { bits, size } => {
149                 assert_eq!(size as u64, dl.pointer_size.bytes());
150                 Ok(Scalar::Bits {
151                     bits: dl.offset(bits as u64, i.bytes())? as u128,
152                     size,
153                 })
154             }
155             Scalar::Ptr(ptr) => ptr.offset(i, dl).map(Scalar::Ptr),
156         }
157     }
158
159     #[inline]
160     pub fn ptr_wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
161         let dl = cx.data_layout();
162         match self {
163             Scalar::Bits { bits, size } => {
164                 assert_eq!(size as u64, dl.pointer_size.bytes());
165                 Scalar::Bits {
166                     bits: dl.overflowing_offset(bits as u64, i.bytes()).0 as u128,
167                     size,
168                 }
169             }
170             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_offset(i, dl)),
171         }
172     }
173
174     #[inline]
175     pub fn ptr_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
176         let dl = cx.data_layout();
177         match self {
178             Scalar::Bits { bits, size } => {
179                 assert_eq!(size as u64, dl.pointer_size().bytes());
180                 Ok(Scalar::Bits {
181                     bits: dl.signed_offset(bits as u64, i)? as u128,
182                     size,
183                 })
184             }
185             Scalar::Ptr(ptr) => ptr.signed_offset(i, dl).map(Scalar::Ptr),
186         }
187     }
188
189     #[inline]
190     pub fn ptr_wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
191         let dl = cx.data_layout();
192         match self {
193             Scalar::Bits { bits, size } => {
194                 assert_eq!(size as u64, dl.pointer_size.bytes());
195                 Scalar::Bits {
196                     bits: dl.overflowing_signed_offset(bits as u64, i128::from(i)).0 as u128,
197                     size,
198                 }
199             }
200             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_signed_offset(i, dl)),
201         }
202     }
203
204     /// Returns this pointers offset from the allocation base, or from NULL (for
205     /// integer pointers).
206     #[inline]
207     pub fn get_ptr_offset(self, cx: &impl HasDataLayout) -> Size {
208         match self {
209             Scalar::Bits { bits, size } => {
210                 assert_eq!(size as u64, cx.pointer_size().bytes());
211                 Size::from_bytes(bits as u64)
212             }
213             Scalar::Ptr(ptr) => ptr.offset,
214         }
215     }
216
217     #[inline]
218     pub fn is_null_ptr(self, cx: &impl HasDataLayout) -> bool {
219         match self {
220             Scalar::Bits { bits, size } => {
221                 assert_eq!(size as u64, cx.data_layout().pointer_size.bytes());
222                 bits == 0
223             },
224             Scalar::Ptr(_) => false,
225         }
226     }
227
228     #[inline]
229     pub fn from_bool(b: bool) -> Self {
230         Scalar::Bits { bits: b as u128, size: 1 }
231     }
232
233     #[inline]
234     pub fn from_char(c: char) -> Self {
235         Scalar::Bits { bits: c as u128, size: 4 }
236     }
237
238     #[inline]
239     pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
240         let i = i.into();
241         debug_assert_eq!(truncate(i, size), i,
242                          "Unsigned value {} does not fit in {} bits", i, size.bits());
243         Scalar::Bits { bits: i, size: size.bytes() as u8 }
244     }
245
246     #[inline]
247     pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
248         let i = i.into();
249         // `into` performed sign extension, we have to truncate
250         let truncated = truncate(i as u128, size);
251         debug_assert_eq!(sign_extend(truncated, size) as i128, i,
252                          "Signed value {} does not fit in {} bits", i, size.bits());
253         Scalar::Bits { bits: truncated, size: size.bytes() as u8 }
254     }
255
256     #[inline]
257     pub fn from_f32(f: f32) -> Self {
258         Scalar::Bits { bits: f.to_bits() as u128, size: 4 }
259     }
260
261     #[inline]
262     pub fn from_f64(f: f64) -> Self {
263         Scalar::Bits { bits: f.to_bits() as u128, size: 8 }
264     }
265
266     #[inline]
267     pub fn to_bits(self, target_size: Size) -> EvalResult<'tcx, u128> {
268         match self {
269             Scalar::Bits { bits, size } => {
270                 assert_eq!(target_size.bytes(), size as u64);
271                 assert_ne!(size, 0, "to_bits cannot be used with zsts");
272                 Ok(bits)
273             }
274             Scalar::Ptr(_) => err!(ReadPointerAsBytes),
275         }
276     }
277
278     #[inline]
279     pub fn to_ptr(self) -> EvalResult<'tcx, Pointer<Tag>> {
280         match self {
281             Scalar::Bits { bits: 0, .. } => err!(InvalidNullPointerUsage),
282             Scalar::Bits { .. } => err!(ReadBytesAsPointer),
283             Scalar::Ptr(p) => Ok(p),
284         }
285     }
286
287     #[inline]
288     pub fn is_bits(self) -> bool {
289         match self {
290             Scalar::Bits { .. } => true,
291             _ => false,
292         }
293     }
294
295     #[inline]
296     pub fn is_ptr(self) -> bool {
297         match self {
298             Scalar::Ptr(_) => true,
299             _ => false,
300         }
301     }
302
303     pub fn to_bool(self) -> EvalResult<'tcx, bool> {
304         match self {
305             Scalar::Bits { bits: 0, size: 1 } => Ok(false),
306             Scalar::Bits { bits: 1, size: 1 } => Ok(true),
307             _ => err!(InvalidBool),
308         }
309     }
310
311     pub fn to_char(self) -> EvalResult<'tcx, char> {
312         let val = self.to_u32()?;
313         match ::std::char::from_u32(val) {
314             Some(c) => Ok(c),
315             None => err!(InvalidChar(val as u128)),
316         }
317     }
318
319     pub fn to_u8(self) -> EvalResult<'static, u8> {
320         let sz = Size::from_bits(8);
321         let b = self.to_bits(sz)?;
322         assert_eq!(b as u8 as u128, b);
323         Ok(b as u8)
324     }
325
326     pub fn to_u32(self) -> EvalResult<'static, u32> {
327         let sz = Size::from_bits(32);
328         let b = self.to_bits(sz)?;
329         assert_eq!(b as u32 as u128, b);
330         Ok(b as u32)
331     }
332
333     pub fn to_u64(self) -> EvalResult<'static, u64> {
334         let sz = Size::from_bits(64);
335         let b = self.to_bits(sz)?;
336         assert_eq!(b as u64 as u128, b);
337         Ok(b as u64)
338     }
339
340     pub fn to_usize(self, cx: &impl HasDataLayout) -> EvalResult<'static, u64> {
341         let b = self.to_bits(cx.data_layout().pointer_size)?;
342         assert_eq!(b as u64 as u128, b);
343         Ok(b as u64)
344     }
345
346     pub fn to_i8(self) -> EvalResult<'static, i8> {
347         let sz = Size::from_bits(8);
348         let b = self.to_bits(sz)?;
349         let b = sign_extend(b, sz) as i128;
350         assert_eq!(b as i8 as i128, b);
351         Ok(b as i8)
352     }
353
354     pub fn to_i32(self) -> EvalResult<'static, i32> {
355         let sz = Size::from_bits(32);
356         let b = self.to_bits(sz)?;
357         let b = sign_extend(b, sz) as i128;
358         assert_eq!(b as i32 as i128, b);
359         Ok(b as i32)
360     }
361
362     pub fn to_i64(self) -> EvalResult<'static, i64> {
363         let sz = Size::from_bits(64);
364         let b = self.to_bits(sz)?;
365         let b = sign_extend(b, sz) as i128;
366         assert_eq!(b as i64 as i128, b);
367         Ok(b as i64)
368     }
369
370     pub fn to_isize(self, cx: &impl HasDataLayout) -> EvalResult<'static, i64> {
371         let b = self.to_bits(cx.data_layout().pointer_size)?;
372         let b = sign_extend(b, cx.data_layout().pointer_size) as i128;
373         assert_eq!(b as i64 as i128, b);
374         Ok(b as i64)
375     }
376
377     #[inline]
378     pub fn to_f32(self) -> EvalResult<'static, f32> {
379         Ok(f32::from_bits(self.to_u32()?))
380     }
381
382     #[inline]
383     pub fn to_f64(self) -> EvalResult<'static, f64> {
384         Ok(f64::from_bits(self.to_u64()?))
385     }
386 }
387
388 impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
389     #[inline(always)]
390     fn from(ptr: Pointer<Tag>) -> Self {
391         Scalar::Ptr(ptr)
392     }
393 }
394
395 #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
396 pub enum ScalarMaybeUndef<Tag=(), Id=AllocId> {
397     Scalar(Scalar<Tag, Id>),
398     Undef,
399 }
400
401 impl<Tag> From<Scalar<Tag>> for ScalarMaybeUndef<Tag> {
402     #[inline(always)]
403     fn from(s: Scalar<Tag>) -> Self {
404         ScalarMaybeUndef::Scalar(s)
405     }
406 }
407
408 impl<Tag> fmt::Display for ScalarMaybeUndef<Tag> {
409     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
410         match self {
411             ScalarMaybeUndef::Undef => write!(f, "uninitialized bytes"),
412             ScalarMaybeUndef::Scalar(s) => write!(f, "{}", s),
413         }
414     }
415 }
416
417 impl<'tcx> ScalarMaybeUndef<()> {
418     #[inline]
419     pub fn with_default_tag<Tag>(self) -> ScalarMaybeUndef<Tag>
420         where Tag: Default
421     {
422         match self {
423             ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.with_default_tag()),
424             ScalarMaybeUndef::Undef => ScalarMaybeUndef::Undef,
425         }
426     }
427 }
428
429 impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
430     #[inline]
431     pub fn erase_tag(self) -> ScalarMaybeUndef
432     {
433         match self {
434             ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.erase_tag()),
435             ScalarMaybeUndef::Undef => ScalarMaybeUndef::Undef,
436         }
437     }
438
439     #[inline]
440     pub fn not_undef(self) -> EvalResult<'static, Scalar<Tag>> {
441         match self {
442             ScalarMaybeUndef::Scalar(scalar) => Ok(scalar),
443             ScalarMaybeUndef::Undef => err!(ReadUndefBytes(Size::from_bytes(0))),
444         }
445     }
446
447     #[inline(always)]
448     pub fn to_ptr(self) -> EvalResult<'tcx, Pointer<Tag>> {
449         self.not_undef()?.to_ptr()
450     }
451
452     #[inline(always)]
453     pub fn to_bits(self, target_size: Size) -> EvalResult<'tcx, u128> {
454         self.not_undef()?.to_bits(target_size)
455     }
456
457     #[inline(always)]
458     pub fn to_bool(self) -> EvalResult<'tcx, bool> {
459         self.not_undef()?.to_bool()
460     }
461
462     #[inline(always)]
463     pub fn to_char(self) -> EvalResult<'tcx, char> {
464         self.not_undef()?.to_char()
465     }
466
467     #[inline(always)]
468     pub fn to_f32(self) -> EvalResult<'tcx, f32> {
469         self.not_undef()?.to_f32()
470     }
471
472     #[inline(always)]
473     pub fn to_f64(self) -> EvalResult<'tcx, f64> {
474         self.not_undef()?.to_f64()
475     }
476
477     #[inline(always)]
478     pub fn to_u8(self) -> EvalResult<'tcx, u8> {
479         self.not_undef()?.to_u8()
480     }
481
482     #[inline(always)]
483     pub fn to_u32(self) -> EvalResult<'tcx, u32> {
484         self.not_undef()?.to_u32()
485     }
486
487     #[inline(always)]
488     pub fn to_u64(self) -> EvalResult<'tcx, u64> {
489         self.not_undef()?.to_u64()
490     }
491
492     #[inline(always)]
493     pub fn to_usize(self, cx: &impl HasDataLayout) -> EvalResult<'tcx, u64> {
494         self.not_undef()?.to_usize(cx)
495     }
496
497     #[inline(always)]
498     pub fn to_i8(self) -> EvalResult<'tcx, i8> {
499         self.not_undef()?.to_i8()
500     }
501
502     #[inline(always)]
503     pub fn to_i32(self) -> EvalResult<'tcx, i32> {
504         self.not_undef()?.to_i32()
505     }
506
507     #[inline(always)]
508     pub fn to_i64(self) -> EvalResult<'tcx, i64> {
509         self.not_undef()?.to_i64()
510     }
511
512     #[inline(always)]
513     pub fn to_isize(self, cx: &impl HasDataLayout) -> EvalResult<'tcx, i64> {
514         self.not_undef()?.to_isize(cx)
515     }
516 }
517
518 impl_stable_hash_for!(enum crate::mir::interpret::ScalarMaybeUndef {
519     Scalar(v),
520     Undef
521 });