]>
Commit | Line | Data |
---|---|---|
970d7e83 | 1 | // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
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 | // Information concerning the machine representation of various types. | |
12 | ||
1a4d82fc | 13 | #![allow(non_camel_case_types)] |
223e47cc | 14 | |
c34b1796 | 15 | use llvm::{self, ValueRef}; |
1a4d82fc JJ |
16 | use trans::common::*; |
17 | ||
18 | use trans::type_::Type; | |
19 | ||
20 | pub type llbits = u64; | |
21 | pub type llsize = u64; | |
22 | pub type llalign = u32; | |
970d7e83 | 23 | |
223e47cc LB |
24 | // ______________________________________________________________________ |
25 | // compute sizeof / alignof | |
26 | ||
27 | // Returns the number of bytes clobbered by a Store to this type. | |
1a4d82fc | 28 | pub fn llsize_of_store(cx: &CrateContext, ty: Type) -> llsize { |
223e47cc | 29 | unsafe { |
c1a9b12d | 30 | return llvm::LLVMStoreSizeOfType(cx.td(), ty.to_ref()); |
223e47cc LB |
31 | } |
32 | } | |
33 | ||
34 | // Returns the number of bytes between successive elements of type T in an | |
35 | // array of T. This is the "ABI" size. It includes any ABI-mandated padding. | |
1a4d82fc | 36 | pub fn llsize_of_alloc(cx: &CrateContext, ty: Type) -> llsize { |
223e47cc | 37 | unsafe { |
c1a9b12d | 38 | return llvm::LLVMABISizeOfType(cx.td(), ty.to_ref()); |
223e47cc LB |
39 | } |
40 | } | |
41 | ||
42 | // Returns, as near as we can figure, the "real" size of a type. As in, the | |
43 | // bits in this number of bytes actually carry data related to the datum | |
1a4d82fc JJ |
44 | // with the type. Not junk, accidentally-damaged words, or whatever. |
45 | // Note that padding of the type will be included for structs, but not for the | |
46 | // other types (i.e. SIMD types). | |
47 | // Rounds up to the nearest byte though, so if you have a 1-bit | |
223e47cc LB |
48 | // value, we return 1 here, not 0. Most of rustc works in bytes. Be warned |
49 | // that LLVM *does* distinguish between e.g. a 1-bit value and an 8-bit value | |
50 | // at the codegen level! In general you should prefer `llbitsize_of_real` | |
51 | // below. | |
1a4d82fc | 52 | pub fn llsize_of_real(cx: &CrateContext, ty: Type) -> llsize { |
223e47cc | 53 | unsafe { |
c1a9b12d | 54 | let nbits = llvm::LLVMSizeOfTypeInBits(cx.td(), ty.to_ref()); |
1a4d82fc | 55 | if nbits & 7 != 0 { |
223e47cc | 56 | // Not an even number of bytes, spills into "next" byte. |
1a4d82fc | 57 | 1 + (nbits >> 3) |
223e47cc LB |
58 | } else { |
59 | nbits >> 3 | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
64 | /// Returns the "real" size of the type in bits. | |
1a4d82fc | 65 | pub fn llbitsize_of_real(cx: &CrateContext, ty: Type) -> llbits { |
223e47cc | 66 | unsafe { |
c1a9b12d | 67 | llvm::LLVMSizeOfTypeInBits(cx.td(), ty.to_ref()) |
223e47cc LB |
68 | } |
69 | } | |
70 | ||
71 | /// Returns the size of the type as an LLVM constant integer value. | |
970d7e83 | 72 | pub fn llsize_of(cx: &CrateContext, ty: Type) -> ValueRef { |
223e47cc LB |
73 | // Once upon a time, this called LLVMSizeOf, which does a |
74 | // getelementptr(1) on a null pointer and casts to an int, in | |
75 | // order to obtain the type size as a value without requiring the | |
76 | // target data layout. But we have the target data layout, so | |
77 | // there's no need for that contrivance. The instruction | |
78 | // selection DAG generator would flatten that GEP(1) node into a | |
79 | // constant of the type's alloc size, so let's save it some work. | |
970d7e83 | 80 | return C_uint(cx, llsize_of_alloc(cx, ty)); |
223e47cc LB |
81 | } |
82 | ||
223e47cc | 83 | // Returns the preferred alignment of the given type for the current target. |
970d7e83 | 84 | // The preferred alignment may be larger than the alignment used when |
223e47cc LB |
85 | // packing the type into structs. This will be used for things like |
86 | // allocations inside a stack frame, which LLVM has a free hand in. | |
1a4d82fc | 87 | pub fn llalign_of_pref(cx: &CrateContext, ty: Type) -> llalign { |
223e47cc | 88 | unsafe { |
c1a9b12d | 89 | return llvm::LLVMPreferredAlignmentOfType(cx.td(), ty.to_ref()); |
223e47cc LB |
90 | } |
91 | } | |
92 | ||
970d7e83 | 93 | // Returns the minimum alignment of a type required by the platform. |
223e47cc LB |
94 | // This is the alignment that will be used for struct fields, arrays, |
95 | // and similar ABI-mandated things. | |
1a4d82fc | 96 | pub fn llalign_of_min(cx: &CrateContext, ty: Type) -> llalign { |
223e47cc | 97 | unsafe { |
c1a9b12d | 98 | return llvm::LLVMABIAlignmentOfType(cx.td(), ty.to_ref()); |
223e47cc LB |
99 | } |
100 | } | |
101 | ||
c34b1796 | 102 | pub fn llelement_offset(cx: &CrateContext, struct_ty: Type, element: usize) -> u64 { |
1a4d82fc | 103 | unsafe { |
c1a9b12d | 104 | return llvm::LLVMOffsetOfElement(cx.td(), |
d9579d0f | 105 | struct_ty.to_ref(), |
1a4d82fc | 106 | element as u32); |
223e47cc LB |
107 | } |
108 | } |