]> git.lizzy.rs Git - rust.git/blob - src/librustc_target/abi/mod.rs
Rollup merge of #54967 - holmgr:master, r=estebank
[rust.git] / src / librustc_target / abi / mod.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 pub use self::Integer::*;
12 pub use self::Primitive::*;
13
14 use spec::Target;
15
16 use std::{cmp, fmt};
17 use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive};
18
19 pub mod call;
20
21 /// Parsed [Data layout](http://llvm.org/docs/LangRef.html#data-layout)
22 /// for a target, which contains everything needed to compute layouts.
23 pub struct TargetDataLayout {
24     pub endian: Endian,
25     pub i1_align: Align,
26     pub i8_align: Align,
27     pub i16_align: Align,
28     pub i32_align: Align,
29     pub i64_align: Align,
30     pub i128_align: Align,
31     pub f32_align: Align,
32     pub f64_align: Align,
33     pub pointer_size: Size,
34     pub pointer_align: Align,
35     pub aggregate_align: Align,
36
37     /// Alignments for vector types.
38     pub vector_align: Vec<(Size, Align)>
39 }
40
41 impl Default for TargetDataLayout {
42     /// Creates an instance of `TargetDataLayout`.
43     fn default() -> TargetDataLayout {
44         TargetDataLayout {
45             endian: Endian::Big,
46             i1_align: Align::from_bits(8, 8).unwrap(),
47             i8_align: Align::from_bits(8, 8).unwrap(),
48             i16_align: Align::from_bits(16, 16).unwrap(),
49             i32_align: Align::from_bits(32, 32).unwrap(),
50             i64_align: Align::from_bits(32, 64).unwrap(),
51             i128_align: Align::from_bits(32, 64).unwrap(),
52             f32_align: Align::from_bits(32, 32).unwrap(),
53             f64_align: Align::from_bits(64, 64).unwrap(),
54             pointer_size: Size::from_bits(64),
55             pointer_align: Align::from_bits(64, 64).unwrap(),
56             aggregate_align: Align::from_bits(0, 64).unwrap(),
57             vector_align: vec![
58                 (Size::from_bits(64), Align::from_bits(64, 64).unwrap()),
59                 (Size::from_bits(128), Align::from_bits(128, 128).unwrap())
60             ]
61         }
62     }
63 }
64
65 impl TargetDataLayout {
66     pub fn parse(target: &Target) -> Result<TargetDataLayout, String> {
67         // Parse a bit count from a string.
68         let parse_bits = |s: &str, kind: &str, cause: &str| {
69             s.parse::<u64>().map_err(|err| {
70                 format!("invalid {} `{}` for `{}` in \"data-layout\": {}",
71                         kind, s, cause, err)
72             })
73         };
74
75         // Parse a size string.
76         let size = |s: &str, cause: &str| {
77             parse_bits(s, "size", cause).map(Size::from_bits)
78         };
79
80         // Parse an alignment string.
81         let align = |s: &[&str], cause: &str| {
82             if s.is_empty() {
83                 return Err(format!("missing alignment for `{}` in \"data-layout\"", cause));
84             }
85             let abi = parse_bits(s[0], "alignment", cause)?;
86             let pref = s.get(1).map_or(Ok(abi), |pref| parse_bits(pref, "alignment", cause))?;
87             Align::from_bits(abi, pref).map_err(|err| {
88                 format!("invalid alignment for `{}` in \"data-layout\": {}",
89                         cause, err)
90             })
91         };
92
93         let mut dl = TargetDataLayout::default();
94         let mut i128_align_src = 64;
95         for spec in target.data_layout.split('-') {
96             match spec.split(':').collect::<Vec<_>>()[..] {
97                 ["e"] => dl.endian = Endian::Little,
98                 ["E"] => dl.endian = Endian::Big,
99                 ["a", ref a..] => dl.aggregate_align = align(a, "a")?,
100                 ["f32", ref a..] => dl.f32_align = align(a, "f32")?,
101                 ["f64", ref a..] => dl.f64_align = align(a, "f64")?,
102                 [p @ "p", s, ref a..] | [p @ "p0", s, ref a..] => {
103                     dl.pointer_size = size(s, p)?;
104                     dl.pointer_align = align(a, p)?;
105                 }
106                 [s, ref a..] if s.starts_with("i") => {
107                     let bits = match s[1..].parse::<u64>() {
108                         Ok(bits) => bits,
109                         Err(_) => {
110                             size(&s[1..], "i")?; // For the user error.
111                             continue;
112                         }
113                     };
114                     let a = align(a, s)?;
115                     match bits {
116                         1 => dl.i1_align = a,
117                         8 => dl.i8_align = a,
118                         16 => dl.i16_align = a,
119                         32 => dl.i32_align = a,
120                         64 => dl.i64_align = a,
121                         _ => {}
122                     }
123                     if bits >= i128_align_src && bits <= 128 {
124                         // Default alignment for i128 is decided by taking the alignment of
125                         // largest-sized i{64...128}.
126                         i128_align_src = bits;
127                         dl.i128_align = a;
128                     }
129                 }
130                 [s, ref a..] if s.starts_with("v") => {
131                     let v_size = size(&s[1..], "v")?;
132                     let a = align(a, s)?;
133                     if let Some(v) = dl.vector_align.iter_mut().find(|v| v.0 == v_size) {
134                         v.1 = a;
135                         continue;
136                     }
137                     // No existing entry, add a new one.
138                     dl.vector_align.push((v_size, a));
139                 }
140                 _ => {} // Ignore everything else.
141             }
142         }
143
144         // Perform consistency checks against the Target information.
145         let endian_str = match dl.endian {
146             Endian::Little => "little",
147             Endian::Big => "big"
148         };
149         if endian_str != target.target_endian {
150             return Err(format!("inconsistent target specification: \"data-layout\" claims \
151                                 architecture is {}-endian, while \"target-endian\" is `{}`",
152                                endian_str, target.target_endian));
153         }
154
155         if dl.pointer_size.bits().to_string() != target.target_pointer_width {
156             return Err(format!("inconsistent target specification: \"data-layout\" claims \
157                                 pointers are {}-bit, while \"target-pointer-width\" is `{}`",
158                                dl.pointer_size.bits(), target.target_pointer_width));
159         }
160
161         Ok(dl)
162     }
163
164     /// Return exclusive upper bound on object size.
165     ///
166     /// The theoretical maximum object size is defined as the maximum positive `isize` value.
167     /// This ensures that the `offset` semantics remain well-defined by allowing it to correctly
168     /// index every address within an object along with one byte past the end, along with allowing
169     /// `isize` to store the difference between any two pointers into an object.
170     ///
171     /// The upper bound on 64-bit currently needs to be lower because LLVM uses a 64-bit integer
172     /// to represent object size in bits. It would need to be 1 << 61 to account for this, but is
173     /// currently conservatively bounded to 1 << 47 as that is enough to cover the current usable
174     /// address space on 64-bit ARMv8 and x86_64.
175     pub fn obj_size_bound(&self) -> u64 {
176         match self.pointer_size.bits() {
177             16 => 1 << 15,
178             32 => 1 << 31,
179             64 => 1 << 47,
180             bits => panic!("obj_size_bound: unknown pointer bit size {}", bits)
181         }
182     }
183
184     pub fn ptr_sized_integer(&self) -> Integer {
185         match self.pointer_size.bits() {
186             16 => I16,
187             32 => I32,
188             64 => I64,
189             bits => panic!("ptr_sized_integer: unknown pointer bit size {}", bits)
190         }
191     }
192
193     pub fn vector_align(&self, vec_size: Size) -> Align {
194         for &(size, align) in &self.vector_align {
195             if size == vec_size {
196                 return align;
197             }
198         }
199         // Default to natural alignment, which is what LLVM does.
200         // That is, use the size, rounded up to a power of 2.
201         let align = vec_size.bytes().next_power_of_two();
202         Align::from_bytes(align, align).unwrap()
203     }
204 }
205
206 pub trait HasDataLayout: Copy {
207     fn data_layout(&self) -> &TargetDataLayout;
208 }
209
210 impl<'a> HasDataLayout for &'a TargetDataLayout {
211     fn data_layout(&self) -> &TargetDataLayout {
212         self
213     }
214 }
215
216 /// Endianness of the target, which must match cfg(target-endian).
217 #[derive(Copy, Clone)]
218 pub enum Endian {
219     Little,
220     Big
221 }
222
223 /// Size of a type in bytes.
224 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)]
225 pub struct Size {
226     raw: u64
227 }
228
229 impl Size {
230     pub const ZERO: Size = Self::from_bytes(0);
231
232     #[inline]
233     pub fn from_bits(bits: u64) -> Size {
234         // Avoid potential overflow from `bits + 7`.
235         Size::from_bytes(bits / 8 + ((bits % 8) + 7) / 8)
236     }
237
238     #[inline]
239     pub const fn from_bytes(bytes: u64) -> Size {
240         Size {
241             raw: bytes
242         }
243     }
244
245     #[inline]
246     pub fn bytes(self) -> u64 {
247         self.raw
248     }
249
250     #[inline]
251     pub fn bits(self) -> u64 {
252         self.bytes().checked_mul(8).unwrap_or_else(|| {
253             panic!("Size::bits: {} bytes in bits doesn't fit in u64", self.bytes())
254         })
255     }
256
257     #[inline]
258     pub fn abi_align(self, align: Align) -> Size {
259         let mask = align.abi() - 1;
260         Size::from_bytes((self.bytes() + mask) & !mask)
261     }
262
263     #[inline]
264     pub fn is_abi_aligned(self, align: Align) -> bool {
265         let mask = align.abi() - 1;
266         self.bytes() & mask == 0
267     }
268
269     #[inline]
270     pub fn checked_add<C: HasDataLayout>(self, offset: Size, cx: C) -> Option<Size> {
271         let dl = cx.data_layout();
272
273         let bytes = self.bytes().checked_add(offset.bytes())?;
274
275         if bytes < dl.obj_size_bound() {
276             Some(Size::from_bytes(bytes))
277         } else {
278             None
279         }
280     }
281
282     #[inline]
283     pub fn checked_mul<C: HasDataLayout>(self, count: u64, cx: C) -> Option<Size> {
284         let dl = cx.data_layout();
285
286         let bytes = self.bytes().checked_mul(count)?;
287         if bytes < dl.obj_size_bound() {
288             Some(Size::from_bytes(bytes))
289         } else {
290             None
291         }
292     }
293 }
294
295 // Panicking addition, subtraction and multiplication for convenience.
296 // Avoid during layout computation, return `LayoutError` instead.
297
298 impl Add for Size {
299     type Output = Size;
300     #[inline]
301     fn add(self, other: Size) -> Size {
302         Size::from_bytes(self.bytes().checked_add(other.bytes()).unwrap_or_else(|| {
303             panic!("Size::add: {} + {} doesn't fit in u64", self.bytes(), other.bytes())
304         }))
305     }
306 }
307
308 impl Sub for Size {
309     type Output = Size;
310     #[inline]
311     fn sub(self, other: Size) -> Size {
312         Size::from_bytes(self.bytes().checked_sub(other.bytes()).unwrap_or_else(|| {
313             panic!("Size::sub: {} - {} would result in negative size", self.bytes(), other.bytes())
314         }))
315     }
316 }
317
318 impl Mul<Size> for u64 {
319     type Output = Size;
320     #[inline]
321     fn mul(self, size: Size) -> Size {
322         size * self
323     }
324 }
325
326 impl Mul<u64> for Size {
327     type Output = Size;
328     #[inline]
329     fn mul(self, count: u64) -> Size {
330         match self.bytes().checked_mul(count) {
331             Some(bytes) => Size::from_bytes(bytes),
332             None => {
333                 panic!("Size::mul: {} * {} doesn't fit in u64", self.bytes(), count)
334             }
335         }
336     }
337 }
338
339 impl AddAssign for Size {
340     #[inline]
341     fn add_assign(&mut self, other: Size) {
342         *self = *self + other;
343     }
344 }
345
346 /// Alignment of a type in bytes, both ABI-mandated and preferred.
347 /// Each field is a power of two, giving the alignment a maximum value
348 /// of 2<sup>(2<sup>8</sup> - 1)</sup>, which is limited by LLVM to a
349 /// maximum capacity of 2<sup>29</sup> or 536870912.
350 #[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Debug, RustcEncodable, RustcDecodable)]
351 pub struct Align {
352     abi_pow2: u8,
353     pref_pow2: u8,
354 }
355
356 impl Align {
357     pub fn from_bits(abi: u64, pref: u64) -> Result<Align, String> {
358         Align::from_bytes(Size::from_bits(abi).bytes(),
359                           Size::from_bits(pref).bytes())
360     }
361
362     pub fn from_bytes(abi: u64, pref: u64) -> Result<Align, String> {
363         let log2 = |align: u64| {
364             // Treat an alignment of 0 bytes like 1-byte alignment.
365             if align == 0 {
366                 return Ok(0);
367             }
368
369             let mut bytes = align;
370             let mut pow: u8 = 0;
371             while (bytes & 1) == 0 {
372                 pow += 1;
373                 bytes >>= 1;
374             }
375             if bytes != 1 {
376                 Err(format!("`{}` is not a power of 2", align))
377             } else if pow > 29 {
378                 Err(format!("`{}` is too large", align))
379             } else {
380                 Ok(pow)
381             }
382         };
383
384         Ok(Align {
385             abi_pow2: log2(abi)?,
386             pref_pow2: log2(pref)?,
387         })
388     }
389
390     pub fn abi(self) -> u64 {
391         1 << self.abi_pow2
392     }
393
394     pub fn pref(self) -> u64 {
395         1 << self.pref_pow2
396     }
397
398     pub fn abi_bits(self) -> u64 {
399         self.abi() * 8
400     }
401
402     pub fn pref_bits(self) -> u64 {
403         self.pref() * 8
404     }
405
406     pub fn min(self, other: Align) -> Align {
407         Align {
408             abi_pow2: cmp::min(self.abi_pow2, other.abi_pow2),
409             pref_pow2: cmp::min(self.pref_pow2, other.pref_pow2),
410         }
411     }
412
413     pub fn max(self, other: Align) -> Align {
414         Align {
415             abi_pow2: cmp::max(self.abi_pow2, other.abi_pow2),
416             pref_pow2: cmp::max(self.pref_pow2, other.pref_pow2),
417         }
418     }
419
420     /// Compute the best alignment possible for the given offset
421     /// (the largest power of two that the offset is a multiple of).
422     ///
423     /// NB: for an offset of `0`, this happens to return `2^64`.
424     pub fn max_for_offset(offset: Size) -> Align {
425         let pow2 = offset.bytes().trailing_zeros() as u8;
426         Align {
427             abi_pow2: pow2,
428             pref_pow2: pow2,
429         }
430     }
431
432     /// Lower the alignment, if necessary, such that the given offset
433     /// is aligned to it (the offset is a multiple of the aligment).
434     pub fn restrict_for_offset(self, offset: Size) -> Align {
435         self.min(Align::max_for_offset(offset))
436     }
437 }
438
439 /// Integers, also used for enum discriminants.
440 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
441 pub enum Integer {
442     I8,
443     I16,
444     I32,
445     I64,
446     I128,
447 }
448
449 impl Integer {
450     pub fn size(self) -> Size {
451         match self {
452             I8 => Size::from_bytes(1),
453             I16 => Size::from_bytes(2),
454             I32 => Size::from_bytes(4),
455             I64  => Size::from_bytes(8),
456             I128  => Size::from_bytes(16),
457         }
458     }
459
460     pub fn align<C: HasDataLayout>(self, cx: C) -> Align {
461         let dl = cx.data_layout();
462
463         match self {
464             I8 => dl.i8_align,
465             I16 => dl.i16_align,
466             I32 => dl.i32_align,
467             I64 => dl.i64_align,
468             I128 => dl.i128_align,
469         }
470     }
471
472     /// Find the smallest Integer type which can represent the signed value.
473     pub fn fit_signed(x: i128) -> Integer {
474         match x {
475             -0x0000_0000_0000_0080..=0x0000_0000_0000_007f => I8,
476             -0x0000_0000_0000_8000..=0x0000_0000_0000_7fff => I16,
477             -0x0000_0000_8000_0000..=0x0000_0000_7fff_ffff => I32,
478             -0x8000_0000_0000_0000..=0x7fff_ffff_ffff_ffff => I64,
479             _ => I128
480         }
481     }
482
483     /// Find the smallest Integer type which can represent the unsigned value.
484     pub fn fit_unsigned(x: u128) -> Integer {
485         match x {
486             0..=0x0000_0000_0000_00ff => I8,
487             0..=0x0000_0000_0000_ffff => I16,
488             0..=0x0000_0000_ffff_ffff => I32,
489             0..=0xffff_ffff_ffff_ffff => I64,
490             _ => I128,
491         }
492     }
493
494     /// Find the smallest integer with the given alignment.
495     pub fn for_abi_align<C: HasDataLayout>(cx: C, align: Align) -> Option<Integer> {
496         let dl = cx.data_layout();
497
498         let wanted = align.abi();
499         for &candidate in &[I8, I16, I32, I64, I128] {
500             if wanted == candidate.align(dl).abi() && wanted == candidate.size().bytes() {
501                 return Some(candidate);
502             }
503         }
504         None
505     }
506
507     /// Find the largest integer with the given alignment or less.
508     pub fn approximate_abi_align<C: HasDataLayout>(cx: C, align: Align) -> Integer {
509         let dl = cx.data_layout();
510
511         let wanted = align.abi();
512         // FIXME(eddyb) maybe include I128 in the future, when it works everywhere.
513         for &candidate in &[I64, I32, I16] {
514             if wanted >= candidate.align(dl).abi() && wanted >= candidate.size().bytes() {
515                 return candidate;
516             }
517         }
518         I8
519     }
520 }
521
522
523 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy,
524          PartialOrd, Ord)]
525 pub enum FloatTy {
526     F32,
527     F64,
528 }
529
530 impl fmt::Debug for FloatTy {
531     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
532         fmt::Display::fmt(self, f)
533     }
534 }
535
536 impl fmt::Display for FloatTy {
537     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
538         write!(f, "{}", self.ty_to_string())
539     }
540 }
541
542 impl FloatTy {
543     pub fn ty_to_string(self) -> &'static str {
544         match self {
545             FloatTy::F32 => "f32",
546             FloatTy::F64 => "f64",
547         }
548     }
549
550     pub fn bit_width(self) -> usize {
551         match self {
552             FloatTy::F32 => 32,
553             FloatTy::F64 => 64,
554         }
555     }
556 }
557
558 /// Fundamental unit of memory access and layout.
559 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
560 pub enum Primitive {
561     /// The `bool` is the signedness of the `Integer` type.
562     ///
563     /// One would think we would not care about such details this low down,
564     /// but some ABIs are described in terms of C types and ISAs where the
565     /// integer arithmetic is done on {sign,zero}-extended registers, e.g.
566     /// a negative integer passed by zero-extension will appear positive in
567     /// the callee, and most operations on it will produce the wrong values.
568     Int(Integer, bool),
569     Float(FloatTy),
570     Pointer
571 }
572
573 impl<'a, 'tcx> Primitive {
574     pub fn size<C: HasDataLayout>(self, cx: C) -> Size {
575         let dl = cx.data_layout();
576
577         match self {
578             Int(i, _) => i.size(),
579             Float(FloatTy::F32) => Size::from_bits(32),
580             Float(FloatTy::F64) => Size::from_bits(64),
581             Pointer => dl.pointer_size
582         }
583     }
584
585     pub fn align<C: HasDataLayout>(self, cx: C) -> Align {
586         let dl = cx.data_layout();
587
588         match self {
589             Int(i, _) => i.align(dl),
590             Float(FloatTy::F32) => dl.f32_align,
591             Float(FloatTy::F64) => dl.f64_align,
592             Pointer => dl.pointer_align
593         }
594     }
595
596     pub fn is_float(self) -> bool {
597         match self {
598             Float(_) => true,
599             _ => false
600         }
601     }
602
603     pub fn is_int(self) -> bool {
604         match self {
605             Int(..) => true,
606             _ => false,
607         }
608     }
609 }
610
611 /// Information about one scalar component of a Rust type.
612 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
613 pub struct Scalar {
614     pub value: Primitive,
615
616     /// Inclusive wrap-around range of valid values, that is, if
617     /// start > end, it represents `start..=max_value()`,
618     /// followed by `0..=end`.
619     ///
620     /// That is, for an i8 primitive, a range of `254..=2` means following
621     /// sequence:
622     ///
623     ///    254 (-2), 255 (-1), 0, 1, 2
624     ///
625     /// This is intended specifically to mirror LLVM’s `!range` metadata,
626     /// semantics.
627     // FIXME(eddyb) always use the shortest range, e.g. by finding
628     // the largest space between two consecutive valid values and
629     // taking everything else as the (shortest) valid range.
630     pub valid_range: RangeInclusive<u128>,
631 }
632
633 impl Scalar {
634     pub fn is_bool(&self) -> bool {
635         if let Int(I8, _) = self.value {
636             self.valid_range == (0..=1)
637         } else {
638             false
639         }
640     }
641
642     /// Returns the valid range as a `x..y` range.
643     ///
644     /// If `x` and `y` are equal, the range is full, not empty.
645     pub fn valid_range_exclusive<C: HasDataLayout>(&self, cx: C) -> Range<u128> {
646         // For a (max) value of -1, max will be `-1 as usize`, which overflows.
647         // However, that is fine here (it would still represent the full range),
648         // i.e., if the range is everything.
649         let bits = self.value.size(cx).bits();
650         assert!(bits <= 128);
651         let mask = !0u128 >> (128 - bits);
652         let start = *self.valid_range.start();
653         let end = *self.valid_range.end();
654         assert_eq!(start, start & mask);
655         assert_eq!(end, end & mask);
656         start..(end.wrapping_add(1) & mask)
657     }
658 }
659
660 /// Describes how the fields of a type are located in memory.
661 #[derive(PartialEq, Eq, Hash, Debug)]
662 pub enum FieldPlacement {
663     /// All fields start at no offset. The `usize` is the field count.
664     ///
665     /// In the case of primitives the number of fields is `0`.
666     Union(usize),
667
668     /// Array/vector-like placement, with all fields of identical types.
669     Array {
670         stride: Size,
671         count: u64
672     },
673
674     /// Struct-like placement, with precomputed offsets.
675     ///
676     /// Fields are guaranteed to not overlap, but note that gaps
677     /// before, between and after all the fields are NOT always
678     /// padding, and as such their contents may not be discarded.
679     /// For example, enum variants leave a gap at the start,
680     /// where the discriminant field in the enum layout goes.
681     Arbitrary {
682         /// Offsets for the first byte of each field,
683         /// ordered to match the source definition order.
684         /// This vector does not go in increasing order.
685         // FIXME(eddyb) use small vector optimization for the common case.
686         offsets: Vec<Size>,
687
688         /// Maps source order field indices to memory order indices,
689         /// depending how fields were permuted.
690         // FIXME(camlorn) also consider small vector  optimization here.
691         memory_index: Vec<u32>
692     }
693 }
694
695 impl FieldPlacement {
696     pub fn count(&self) -> usize {
697         match *self {
698             FieldPlacement::Union(count) => count,
699             FieldPlacement::Array { count, .. } => {
700                 let usize_count = count as usize;
701                 assert_eq!(usize_count as u64, count);
702                 usize_count
703             }
704             FieldPlacement::Arbitrary { ref offsets, .. } => offsets.len()
705         }
706     }
707
708     pub fn offset(&self, i: usize) -> Size {
709         match *self {
710             FieldPlacement::Union(_) => Size::ZERO,
711             FieldPlacement::Array { stride, count } => {
712                 let i = i as u64;
713                 assert!(i < count);
714                 stride * i
715             }
716             FieldPlacement::Arbitrary { ref offsets, .. } => offsets[i]
717         }
718     }
719
720     pub fn memory_index(&self, i: usize) -> usize {
721         match *self {
722             FieldPlacement::Union(_) |
723             FieldPlacement::Array { .. } => i,
724             FieldPlacement::Arbitrary { ref memory_index, .. } => {
725                 let r = memory_index[i];
726                 assert_eq!(r as usize as u32, r);
727                 r as usize
728             }
729         }
730     }
731
732     /// Get source indices of the fields by increasing offsets.
733     #[inline]
734     pub fn index_by_increasing_offset<'a>(&'a self) -> impl Iterator<Item=usize>+'a {
735         let mut inverse_small = [0u8; 64];
736         let mut inverse_big = vec![];
737         let use_small = self.count() <= inverse_small.len();
738
739         // We have to write this logic twice in order to keep the array small.
740         if let FieldPlacement::Arbitrary { ref memory_index, .. } = *self {
741             if use_small {
742                 for i in 0..self.count() {
743                     inverse_small[memory_index[i] as usize] = i as u8;
744                 }
745             } else {
746                 inverse_big = vec![0; self.count()];
747                 for i in 0..self.count() {
748                     inverse_big[memory_index[i] as usize] = i as u32;
749                 }
750             }
751         }
752
753         (0..self.count()).map(move |i| {
754             match *self {
755                 FieldPlacement::Union(_) |
756                 FieldPlacement::Array { .. } => i,
757                 FieldPlacement::Arbitrary { .. } => {
758                     if use_small { inverse_small[i] as usize }
759                     else { inverse_big[i] as usize }
760                 }
761             }
762         })
763     }
764 }
765
766 /// Describes how values of the type are passed by target ABIs,
767 /// in terms of categories of C types there are ABI rules for.
768 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
769 pub enum Abi {
770     Uninhabited,
771     Scalar(Scalar),
772     ScalarPair(Scalar, Scalar),
773     Vector {
774         element: Scalar,
775         count: u64
776     },
777     Aggregate {
778         /// If true, the size is exact, otherwise it's only a lower bound.
779         sized: bool,
780     }
781 }
782
783 impl Abi {
784     /// Returns true if the layout corresponds to an unsized type.
785     pub fn is_unsized(&self) -> bool {
786         match *self {
787             Abi::Uninhabited |
788             Abi::Scalar(_) |
789             Abi::ScalarPair(..) |
790             Abi::Vector { .. } => false,
791             Abi::Aggregate { sized } => !sized
792         }
793     }
794
795     /// Returns true if this is a single signed integer scalar
796     pub fn is_signed(&self) -> bool {
797         match *self {
798             Abi::Scalar(ref scal) => match scal.value {
799                 Primitive::Int(_, signed) => signed,
800                 _ => false,
801             },
802             _ => false,
803         }
804     }
805
806     /// Returns true if this is an uninhabited type
807     pub fn is_uninhabited(&self) -> bool {
808         match *self {
809             Abi::Uninhabited => true,
810             _ => false,
811         }
812     }
813 }
814
815 #[derive(PartialEq, Eq, Hash, Debug)]
816 pub enum Variants {
817     /// Single enum variants, structs/tuples, unions, and all non-ADTs.
818     Single {
819         index: usize
820     },
821
822     /// General-case enums: for each case there is a struct, and they all have
823     /// all space reserved for the tag, and their first field starts
824     /// at a non-0 offset, after where the tag would go.
825     Tagged {
826         tag: Scalar,
827         variants: Vec<LayoutDetails>,
828     },
829
830     /// Multiple cases distinguished by a niche (values invalid for a type):
831     /// the variant `dataful_variant` contains a niche at an arbitrary
832     /// offset (field 0 of the enum), which for a variant with discriminant
833     /// `d` is set to `(d - niche_variants.start).wrapping_add(niche_start)`.
834     ///
835     /// For example, `Option<(usize, &T)>`  is represented such that
836     /// `None` has a null pointer for the second tuple field, and
837     /// `Some` is the identity function (with a non-null reference).
838     NicheFilling {
839         dataful_variant: usize,
840         niche_variants: RangeInclusive<usize>,
841         niche: Scalar,
842         niche_start: u128,
843         variants: Vec<LayoutDetails>,
844     }
845 }
846
847 #[derive(PartialEq, Eq, Hash, Debug)]
848 pub struct LayoutDetails {
849     pub variants: Variants,
850     pub fields: FieldPlacement,
851     pub abi: Abi,
852     pub align: Align,
853     pub size: Size
854 }
855
856 impl LayoutDetails {
857     pub fn scalar<C: HasDataLayout>(cx: C, scalar: Scalar) -> Self {
858         let size = scalar.value.size(cx);
859         let align = scalar.value.align(cx);
860         LayoutDetails {
861             variants: Variants::Single { index: 0 },
862             fields: FieldPlacement::Union(0),
863             abi: Abi::Scalar(scalar),
864             size,
865             align,
866         }
867     }
868 }
869
870 /// The details of the layout of a type, alongside the type itself.
871 /// Provides various type traversal APIs (e.g. recursing into fields).
872 ///
873 /// Note that the details are NOT guaranteed to always be identical
874 /// to those obtained from `layout_of(ty)`, as we need to produce
875 /// layouts for which Rust types do not exist, such as enum variants
876 /// or synthetic fields of enums (i.e. discriminants) and fat pointers.
877 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
878 pub struct TyLayout<'a, Ty> {
879     pub ty: Ty,
880     pub details: &'a LayoutDetails
881 }
882
883 impl<'a, Ty> Deref for TyLayout<'a, Ty> {
884     type Target = &'a LayoutDetails;
885     fn deref(&self) -> &&'a LayoutDetails {
886         &self.details
887     }
888 }
889
890 pub trait LayoutOf {
891     type Ty;
892     type TyLayout;
893
894     fn layout_of(self, ty: Self::Ty) -> Self::TyLayout;
895 }
896
897 pub trait TyLayoutMethods<'a, C: LayoutOf<Ty = Self>>: Sized {
898     fn for_variant(this: TyLayout<'a, Self>, cx: C, variant_index: usize) -> TyLayout<'a, Self>;
899     fn field(this: TyLayout<'a, Self>, cx: C, i: usize) -> C::TyLayout;
900 }
901
902 impl<'a, Ty> TyLayout<'a, Ty> {
903     pub fn for_variant<C>(self, cx: C, variant_index: usize) -> Self
904     where Ty: TyLayoutMethods<'a, C>, C: LayoutOf<Ty = Ty> {
905         Ty::for_variant(self, cx, variant_index)
906     }
907     pub fn field<C>(self, cx: C, i: usize) -> C::TyLayout
908     where Ty: TyLayoutMethods<'a, C>, C: LayoutOf<Ty = Ty> {
909         Ty::field(self, cx, i)
910     }
911 }
912
913 impl<'a, Ty> TyLayout<'a, Ty> {
914     /// Returns true if the layout corresponds to an unsized type.
915     pub fn is_unsized(&self) -> bool {
916         self.abi.is_unsized()
917     }
918
919     /// Returns true if the type is a ZST and not unsized.
920     pub fn is_zst(&self) -> bool {
921         match self.abi {
922             Abi::Scalar(_) |
923             Abi::ScalarPair(..) |
924             Abi::Vector { .. } => false,
925             Abi::Uninhabited => self.size.bytes() == 0,
926             Abi::Aggregate { sized } => sized && self.size.bytes() == 0
927         }
928     }
929
930     pub fn size_and_align(&self) -> (Size, Align) {
931         (self.size, self.align)
932     }
933 }