]> git.proxmox.com Git - rustc.git/blame - src/librustc_trans/machine.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / librustc_trans / machine.rs
CommitLineData
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 15use llvm::{self, ValueRef};
54a0048b 16use common::*;
1a4d82fc 17
54a0048b 18use type_::Type;
1a4d82fc
JJ
19
20pub type llbits = u64;
21pub type llsize = u64;
22pub type llalign = u32;
970d7e83 23
223e47cc
LB
24// ______________________________________________________________________
25// compute sizeof / alignof
26
223e47cc
LB
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.
1a4d82fc 29pub fn llsize_of_alloc(cx: &CrateContext, ty: Type) -> llsize {
223e47cc 30 unsafe {
c1a9b12d 31 return llvm::LLVMABISizeOfType(cx.td(), ty.to_ref());
223e47cc
LB
32 }
33}
34
223e47cc 35/// Returns the "real" size of the type in bits.
1a4d82fc 36pub fn llbitsize_of_real(cx: &CrateContext, ty: Type) -> llbits {
223e47cc 37 unsafe {
c1a9b12d 38 llvm::LLVMSizeOfTypeInBits(cx.td(), ty.to_ref())
223e47cc
LB
39 }
40}
41
42/// Returns the size of the type as an LLVM constant integer value.
970d7e83 43pub fn llsize_of(cx: &CrateContext, ty: Type) -> ValueRef {
223e47cc
LB
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.
970d7e83 51 return C_uint(cx, llsize_of_alloc(cx, ty));
223e47cc
LB
52}
53
223e47cc 54// Returns the preferred alignment of the given type for the current target.
970d7e83 55// The preferred alignment may be larger than the alignment used when
223e47cc
LB
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.
1a4d82fc 58pub fn llalign_of_pref(cx: &CrateContext, ty: Type) -> llalign {
223e47cc 59 unsafe {
c1a9b12d 60 return llvm::LLVMPreferredAlignmentOfType(cx.td(), ty.to_ref());
223e47cc
LB
61 }
62}
63
970d7e83 64// Returns the minimum alignment of a type required by the platform.
223e47cc
LB
65// This is the alignment that will be used for struct fields, arrays,
66// and similar ABI-mandated things.
1a4d82fc 67pub fn llalign_of_min(cx: &CrateContext, ty: Type) -> llalign {
223e47cc 68 unsafe {
c1a9b12d 69 return llvm::LLVMABIAlignmentOfType(cx.td(), ty.to_ref());
223e47cc
LB
70 }
71}
72
c34b1796 73pub fn llelement_offset(cx: &CrateContext, struct_ty: Type, element: usize) -> u64 {
1a4d82fc 74 unsafe {
c1a9b12d 75 return llvm::LLVMOffsetOfElement(cx.td(),
d9579d0f 76 struct_ty.to_ref(),
1a4d82fc 77 element as u32);
223e47cc
LB
78 }
79}