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