]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/machine.rs
Fix checking for missing stability annotations
[rust.git] / src / librustc_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::{self, ValueRef};
16 use common::*;
17
18 use type_::Type;
19
20 pub type llbits = u64;
21 pub type llsize = u64;
22 pub type llalign = u32;
23
24 // ______________________________________________________________________
25 // compute sizeof / alignof
26
27 // Returns the number of bytes between successive elements of type T in an
28 // array of T. This is the "ABI" size. It includes any ABI-mandated padding.
29 pub fn llsize_of_alloc(cx: &CrateContext, ty: Type) -> llsize {
30     unsafe {
31         return llvm::LLVMABISizeOfType(cx.td(), ty.to_ref());
32     }
33 }
34
35 /// Returns the "real" size of the type in bits.
36 pub fn llbitsize_of_real(cx: &CrateContext, ty: Type) -> llbits {
37     unsafe {
38         llvm::LLVMSizeOfTypeInBits(cx.td(), ty.to_ref())
39     }
40 }
41
42 /// Returns the size of the type as an LLVM constant integer value.
43 pub fn llsize_of(cx: &CrateContext, ty: Type) -> ValueRef {
44     // Once upon a time, this called LLVMSizeOf, which does a
45     // getelementptr(1) on a null pointer and casts to an int, in
46     // order to obtain the type size as a value without requiring the
47     // target data layout.  But we have the target data layout, so
48     // there's no need for that contrivance.  The instruction
49     // selection DAG generator would flatten that GEP(1) node into a
50     // constant of the type's alloc size, so let's save it some work.
51     return C_uint(cx, llsize_of_alloc(cx, ty));
52 }
53
54 // Returns the preferred alignment of the given type for the current target.
55 // The preferred alignment may be larger than the alignment used when
56 // packing the type into structs. This will be used for things like
57 // allocations inside a stack frame, which LLVM has a free hand in.
58 pub fn llalign_of_pref(cx: &CrateContext, ty: Type) -> llalign {
59     unsafe {
60         return llvm::LLVMPreferredAlignmentOfType(cx.td(), ty.to_ref());
61     }
62 }
63
64 // Returns the minimum alignment of a type required by the platform.
65 // This is the alignment that will be used for struct fields, arrays,
66 // and similar ABI-mandated things.
67 pub fn llalign_of_min(cx: &CrateContext, ty: Type) -> llalign {
68     unsafe {
69         return llvm::LLVMABIAlignmentOfType(cx.td(), ty.to_ref());
70     }
71 }
72
73 pub fn llelement_offset(cx: &CrateContext, struct_ty: Type, element: usize) -> u64 {
74     unsafe {
75         return llvm::LLVMOffsetOfElement(cx.td(),
76                                          struct_ty.to_ref(),
77                                          element as u32);
78     }
79 }