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.
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.
11 pub use self::Integer::*;
12 pub use self::Primitive::*;
17 use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive};
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 {
30 pub i128_align: Align,
33 pub pointer_size: Size,
34 pub pointer_align: Align,
35 pub aggregate_align: Align,
37 /// Alignments for vector types.
38 pub vector_align: Vec<(Size, Align)>
41 impl Default for TargetDataLayout {
42 /// Creates an instance of `TargetDataLayout`.
43 fn default() -> TargetDataLayout {
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(),
58 (Size::from_bits(64), Align::from_bits(64, 64).unwrap()),
59 (Size::from_bits(128), Align::from_bits(128, 128).unwrap())
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\": {}",
75 // Parse a size string.
76 let size = |s: &str, cause: &str| {
77 parse_bits(s, "size", cause).map(Size::from_bits)
80 // Parse an alignment string.
81 let align = |s: &[&str], cause: &str| {
83 return Err(format!("missing alignment for `{}` in \"data-layout\"", cause));
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\": {}",
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)?;
106 &[s, ref a..] if s.starts_with("i") => {
107 let bits = match s[1..].parse::<u64>() {
110 size(&s[1..], "i")?; // For the user error.
114 let a = align(a, s)?;
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,
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;
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) {
137 // No existing entry, add a new one.
138 dl.vector_align.push((v_size, a));
140 _ => {} // Ignore everything else.
144 // Perform consistency checks against the Target information.
145 let endian_str = match dl.endian {
146 Endian::Little => "little",
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));
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));
164 /// Return exclusive upper bound on object size.
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.
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() {
180 bits => panic!("obj_size_bound: unknown pointer bit size {}", bits)
184 pub fn ptr_sized_integer(&self) -> Integer {
185 match self.pointer_size.bits() {
189 bits => panic!("ptr_sized_integer: unknown pointer bit size {}", bits)
193 pub fn vector_align(&self, vec_size: Size) -> Align {
194 for &(size, align) in &self.vector_align {
195 if size == vec_size {
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()
206 pub trait HasDataLayout: Copy {
207 fn data_layout(&self) -> &TargetDataLayout;
210 impl<'a> HasDataLayout for &'a TargetDataLayout {
211 fn data_layout(&self) -> &TargetDataLayout {
216 /// Endianness of the target, which must match cfg(target-endian).
217 #[derive(Copy, Clone)]
223 /// Size of a type in bytes.
224 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
230 pub fn from_bits(bits: u64) -> Size {
231 // Avoid potential overflow from `bits + 7`.
232 Size::from_bytes(bits / 8 + ((bits % 8) + 7) / 8)
235 pub fn from_bytes(bytes: u64) -> Size {
236 if bytes >= (1 << 61) {
237 panic!("Size::from_bytes: {} bytes in bits doesn't fit in u64", bytes)
244 pub fn bytes(self) -> u64 {
248 pub fn bits(self) -> u64 {
252 pub fn abi_align(self, align: Align) -> Size {
253 let mask = align.abi() - 1;
254 Size::from_bytes((self.bytes() + mask) & !mask)
257 pub fn is_abi_aligned(self, align: Align) -> bool {
258 let mask = align.abi() - 1;
259 self.bytes() & mask == 0
262 pub fn checked_add<C: HasDataLayout>(self, offset: Size, cx: C) -> Option<Size> {
263 let dl = cx.data_layout();
265 // Each Size is less than dl.obj_size_bound(), so the sum is
266 // also less than 1 << 62 (and therefore can't overflow).
267 let bytes = self.bytes() + offset.bytes();
269 if bytes < dl.obj_size_bound() {
270 Some(Size::from_bytes(bytes))
276 pub fn checked_mul<C: HasDataLayout>(self, count: u64, cx: C) -> Option<Size> {
277 let dl = cx.data_layout();
279 match self.bytes().checked_mul(count) {
280 Some(bytes) if bytes < dl.obj_size_bound() => {
281 Some(Size::from_bytes(bytes))
288 // Panicking addition, subtraction and multiplication for convenience.
289 // Avoid during layout computation, return `LayoutError` instead.
293 fn add(self, other: Size) -> Size {
294 // Each Size is less than 1 << 61, so the sum is
295 // less than 1 << 62 (and therefore can't overflow).
296 Size::from_bytes(self.bytes() + other.bytes())
302 fn sub(self, other: Size) -> Size {
303 // Each Size is less than 1 << 61, so an underflow
304 // would result in a value larger than 1 << 61,
305 // which Size::from_bytes will catch for us.
306 Size::from_bytes(self.bytes() - other.bytes())
310 impl Mul<u64> for Size {
312 fn mul(self, count: u64) -> Size {
313 match self.bytes().checked_mul(count) {
314 Some(bytes) => Size::from_bytes(bytes),
316 panic!("Size::mul: {} * {} doesn't fit in u64", self.bytes(), count)
322 impl AddAssign for Size {
323 fn add_assign(&mut self, other: Size) {
324 *self = *self + other;
328 /// Alignment of a type in bytes, both ABI-mandated and preferred.
329 /// Each field is a power of two, giving the alignment a maximum value of
330 /// 2<sup>(2<sup>8</sup> - 1)</sup>, which is limited by LLVM to a i32,
331 /// with a maximum capacity of 2<sup>31</sup> - 1 or 2147483647.
332 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
339 pub fn from_bits(abi: u64, pref: u64) -> Result<Align, String> {
340 Align::from_bytes(Size::from_bits(abi).bytes(),
341 Size::from_bits(pref).bytes())
344 pub fn from_bytes(abi: u64, pref: u64) -> Result<Align, String> {
345 let log2 = |align: u64| {
346 // Treat an alignment of 0 bytes like 1-byte alignment.
351 let mut bytes = align;
353 while (bytes & 1) == 0 {
358 Err(format!("`{}` is not a power of 2", align))
360 Err(format!("`{}` is too large", align))
367 abi_pow2: log2(abi)?,
368 pref_pow2: log2(pref)?,
372 pub fn abi(self) -> u64 {
376 pub fn pref(self) -> u64 {
380 pub fn abi_bits(self) -> u64 {
384 pub fn pref_bits(self) -> u64 {
388 pub fn min(self, other: Align) -> Align {
390 abi_pow2: cmp::min(self.abi_pow2, other.abi_pow2),
391 pref_pow2: cmp::min(self.pref_pow2, other.pref_pow2),
395 pub fn max(self, other: Align) -> Align {
397 abi_pow2: cmp::max(self.abi_pow2, other.abi_pow2),
398 pref_pow2: cmp::max(self.pref_pow2, other.pref_pow2),
403 /// Integers, also used for enum discriminants.
404 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
414 pub fn size(&self) -> Size {
416 I8 => Size::from_bytes(1),
417 I16 => Size::from_bytes(2),
418 I32 => Size::from_bytes(4),
419 I64 => Size::from_bytes(8),
420 I128 => Size::from_bytes(16),
424 pub fn align<C: HasDataLayout>(&self, cx: C) -> Align {
425 let dl = cx.data_layout();
432 I128 => dl.i128_align,
436 /// Find the smallest Integer type which can represent the signed value.
437 pub fn fit_signed(x: i128) -> Integer {
439 -0x0000_0000_0000_0080...0x0000_0000_0000_007f => I8,
440 -0x0000_0000_0000_8000...0x0000_0000_0000_7fff => I16,
441 -0x0000_0000_8000_0000...0x0000_0000_7fff_ffff => I32,
442 -0x8000_0000_0000_0000...0x7fff_ffff_ffff_ffff => I64,
447 /// Find the smallest Integer type which can represent the unsigned value.
448 pub fn fit_unsigned(x: u128) -> Integer {
450 0...0x0000_0000_0000_00ff => I8,
451 0...0x0000_0000_0000_ffff => I16,
452 0...0x0000_0000_ffff_ffff => I32,
453 0...0xffff_ffff_ffff_ffff => I64,
458 /// Find the smallest integer with the given alignment.
459 pub fn for_abi_align<C: HasDataLayout>(cx: C, align: Align) -> Option<Integer> {
460 let dl = cx.data_layout();
462 let wanted = align.abi();
463 for &candidate in &[I8, I16, I32, I64, I128] {
464 if wanted == candidate.align(dl).abi() && wanted == candidate.size().bytes() {
465 return Some(candidate);
471 /// Find the largest integer with the given alignment or less.
472 pub fn approximate_abi_align<C: HasDataLayout>(cx: C, align: Align) -> Integer {
473 let dl = cx.data_layout();
475 let wanted = align.abi();
476 // FIXME(eddyb) maybe include I128 in the future, when it works everywhere.
477 for &candidate in &[I64, I32, I16] {
478 if wanted >= candidate.align(dl).abi() && wanted >= candidate.size().bytes() {
486 /// Fundamental unit of memory access and layout.
487 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
489 /// The `bool` is the signedness of the `Integer` type.
491 /// One would think we would not care about such details this low down,
492 /// but some ABIs are described in terms of C types and ISAs where the
493 /// integer arithmetic is done on {sign,zero}-extended registers, e.g.
494 /// a negative integer passed by zero-extension will appear positive in
495 /// the callee, and most operations on it will produce the wrong values.
502 impl<'a, 'tcx> Primitive {
503 pub fn size<C: HasDataLayout>(self, cx: C) -> Size {
504 let dl = cx.data_layout();
507 Int(i, _) => i.size(),
508 F32 => Size::from_bits(32),
509 F64 => Size::from_bits(64),
510 Pointer => dl.pointer_size
514 pub fn align<C: HasDataLayout>(self, cx: C) -> Align {
515 let dl = cx.data_layout();
518 Int(i, _) => i.align(dl),
521 Pointer => dl.pointer_align
526 /// Information about one scalar component of a Rust type.
527 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
529 pub value: Primitive,
531 /// Inclusive wrap-around range of valid values, that is, if
532 /// min > max, it represents min..=u128::MAX followed by 0..=max.
533 // FIXME(eddyb) always use the shortest range, e.g. by finding
534 // the largest space between two consecutive valid values and
535 // taking everything else as the (shortest) valid range.
536 pub valid_range: RangeInclusive<u128>,
540 pub fn is_bool(&self) -> bool {
541 if let Int(I8, _) = self.value {
542 self.valid_range == (0..=1)
548 /// Returns the valid range as a `x..y` range.
550 /// If `x` and `y` are equal, the range is full, not empty.
551 pub fn valid_range_exclusive<C: HasDataLayout>(&self, cx: C) -> Range<u128> {
552 // For a (max) value of -1, max will be `-1 as usize`, which overflows.
553 // However, that is fine here (it would still represent the full range),
554 // i.e., if the range is everything.
555 let bits = self.value.size(cx).bits();
556 assert!(bits <= 128);
557 let mask = !0u128 >> (128 - bits);
558 let start = self.valid_range.start;
559 let end = self.valid_range.end;
560 assert_eq!(start, start & mask);
561 assert_eq!(end, end & mask);
562 start..(end.wrapping_add(1) & mask)
566 /// Describes how the fields of a type are located in memory.
567 #[derive(PartialEq, Eq, Hash, Debug)]
568 pub enum FieldPlacement {
569 /// All fields start at no offset. The `usize` is the field count.
572 /// Array/vector-like placement, with all fields of identical types.
578 /// Struct-like placement, with precomputed offsets.
580 /// Fields are guaranteed to not overlap, but note that gaps
581 /// before, between and after all the fields are NOT always
582 /// padding, and as such their contents may not be discarded.
583 /// For example, enum variants leave a gap at the start,
584 /// where the discriminant field in the enum layout goes.
586 /// Offsets for the first byte of each field,
587 /// ordered to match the source definition order.
588 /// This vector does not go in increasing order.
589 // FIXME(eddyb) use small vector optimization for the common case.
592 /// Maps source order field indices to memory order indices,
593 /// depending how fields were permuted.
594 // FIXME(camlorn) also consider small vector optimization here.
595 memory_index: Vec<u32>
599 impl FieldPlacement {
600 pub fn count(&self) -> usize {
602 FieldPlacement::Union(count) => count,
603 FieldPlacement::Array { count, .. } => {
604 let usize_count = count as usize;
605 assert_eq!(usize_count as u64, count);
608 FieldPlacement::Arbitrary { ref offsets, .. } => offsets.len()
612 pub fn offset(&self, i: usize) -> Size {
614 FieldPlacement::Union(_) => Size::from_bytes(0),
615 FieldPlacement::Array { stride, count } => {
620 FieldPlacement::Arbitrary { ref offsets, .. } => offsets[i]
624 pub fn memory_index(&self, i: usize) -> usize {
626 FieldPlacement::Union(_) |
627 FieldPlacement::Array { .. } => i,
628 FieldPlacement::Arbitrary { ref memory_index, .. } => {
629 let r = memory_index[i];
630 assert_eq!(r as usize as u32, r);
636 /// Get source indices of the fields by increasing offsets.
638 pub fn index_by_increasing_offset<'a>(&'a self) -> impl Iterator<Item=usize>+'a {
639 let mut inverse_small = [0u8; 64];
640 let mut inverse_big = vec![];
641 let use_small = self.count() <= inverse_small.len();
643 // We have to write this logic twice in order to keep the array small.
644 if let FieldPlacement::Arbitrary { ref memory_index, .. } = *self {
646 for i in 0..self.count() {
647 inverse_small[memory_index[i] as usize] = i as u8;
650 inverse_big = vec![0; self.count()];
651 for i in 0..self.count() {
652 inverse_big[memory_index[i] as usize] = i as u32;
657 (0..self.count()).map(move |i| {
659 FieldPlacement::Union(_) |
660 FieldPlacement::Array { .. } => i,
661 FieldPlacement::Arbitrary { .. } => {
662 if use_small { inverse_small[i] as usize }
663 else { inverse_big[i] as usize }
670 /// Describes how values of the type are passed by target ABIs,
671 /// in terms of categories of C types there are ABI rules for.
672 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
676 ScalarPair(Scalar, Scalar),
682 /// If true, the size is exact, otherwise it's only a lower bound.
688 /// Returns true if the layout corresponds to an unsized type.
689 pub fn is_unsized(&self) -> bool {
693 Abi::ScalarPair(..) |
694 Abi::Vector { .. } => false,
695 Abi::Aggregate { sized } => !sized
699 /// Returns true if this is a single signed integer scalar
700 pub fn is_signed(&self) -> bool {
702 Abi::Scalar(ref scal) => match scal.value {
703 Primitive::Int(_, signed) => signed,
711 #[derive(PartialEq, Eq, Hash, Debug)]
713 /// Single enum variants, structs/tuples, unions, and all non-ADTs.
718 /// General-case enums: for each case there is a struct, and they all have
719 /// all space reserved for the discriminant, and their first field starts
720 /// at a non-0 offset, after where the discriminant would go.
723 variants: Vec<LayoutDetails>,
726 /// Multiple cases distinguished by a niche (values invalid for a type):
727 /// the variant `dataful_variant` contains a niche at an arbitrary
728 /// offset (field 0 of the enum), which for a variant with discriminant
729 /// `d` is set to `(d - niche_variants.start).wrapping_add(niche_start)`.
731 /// For example, `Option<(usize, &T)>` is represented such that
732 /// `None` has a null pointer for the second tuple field, and
733 /// `Some` is the identity function (with a non-null reference).
735 dataful_variant: usize,
736 niche_variants: RangeInclusive<usize>,
739 variants: Vec<LayoutDetails>,
743 #[derive(PartialEq, Eq, Hash, Debug)]
744 pub struct LayoutDetails {
745 pub variants: Variants,
746 pub fields: FieldPlacement,
753 pub fn scalar<C: HasDataLayout>(cx: C, scalar: Scalar) -> Self {
754 let size = scalar.value.size(cx);
755 let align = scalar.value.align(cx);
757 variants: Variants::Single { index: 0 },
758 fields: FieldPlacement::Union(0),
759 abi: Abi::Scalar(scalar),
765 pub fn uninhabited(field_count: usize) -> Self {
766 let align = Align::from_bytes(1, 1).unwrap();
768 variants: Variants::Single { index: 0 },
769 fields: FieldPlacement::Union(field_count),
770 abi: Abi::Uninhabited,
772 size: Size::from_bytes(0)
777 /// The details of the layout of a type, alongside the type itself.
778 /// Provides various type traversal APIs (e.g. recursing into fields).
780 /// Note that the details are NOT guaranteed to always be identical
781 /// to those obtained from `layout_of(ty)`, as we need to produce
782 /// layouts for which Rust types do not exist, such as enum variants
783 /// or synthetic fields of enums (i.e. discriminants) and fat pointers.
784 #[derive(Copy, Clone, Debug)]
785 pub struct TyLayout<'a, Ty> {
787 pub details: &'a LayoutDetails
790 impl<'a, Ty> Deref for TyLayout<'a, Ty> {
791 type Target = &'a LayoutDetails;
792 fn deref(&self) -> &&'a LayoutDetails {
801 fn layout_of(self, ty: Self::Ty) -> Self::TyLayout;
804 pub trait TyLayoutMethods<'a, C: LayoutOf<Ty = Self>>: Sized {
805 fn for_variant(this: TyLayout<'a, Self>, cx: C, variant_index: usize) -> TyLayout<'a, Self>;
806 fn field(this: TyLayout<'a, Self>, cx: C, i: usize) -> C::TyLayout;
809 impl<'a, Ty> TyLayout<'a, Ty> {
810 pub fn for_variant<C>(self, cx: C, variant_index: usize) -> Self
811 where Ty: TyLayoutMethods<'a, C>, C: LayoutOf<Ty = Ty> {
812 Ty::for_variant(self, cx, variant_index)
814 pub fn field<C>(self, cx: C, i: usize) -> C::TyLayout
815 where Ty: TyLayoutMethods<'a, C>, C: LayoutOf<Ty = Ty> {
816 Ty::field(self, cx, i)
820 impl<'a, Ty> TyLayout<'a, Ty> {
821 /// Returns true if the layout corresponds to an unsized type.
822 pub fn is_unsized(&self) -> bool {
823 self.abi.is_unsized()
826 /// Returns true if the type is a ZST and not unsized.
827 pub fn is_zst(&self) -> bool {
829 Abi::Uninhabited => true,
831 Abi::ScalarPair(..) |
832 Abi::Vector { .. } => false,
833 Abi::Aggregate { sized } => sized && self.size.bytes() == 0
837 pub fn size_and_align(&self) -> (Size, Align) {
838 (self.size, self.align)