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