2013-04-10 07:47:22 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-25 17:21:16 -05:00
|
|
|
// Information concerning the machine representation of various types.
|
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
|
2015-03-14 21:01:57 -05:00
|
|
|
use llvm::{self, ValueRef};
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::common::*;
|
2012-09-25 17:21:16 -05:00
|
|
|
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::type_::Type;
|
2013-06-16 05:52:44 -05:00
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
pub type llbits = u64;
|
|
|
|
pub type llsize = u64;
|
|
|
|
pub type llalign = u32;
|
|
|
|
|
2012-09-25 17:21:16 -05:00
|
|
|
// ______________________________________________________________________
|
|
|
|
// compute sizeof / alignof
|
|
|
|
|
|
|
|
// Returns the number of bytes clobbered by a Store to this type.
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn llsize_of_store(cx: &CrateContext, ty: Type) -> llsize {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2015-07-16 17:48:16 -05:00
|
|
|
return llvm::LLVMStoreSizeOfType(cx.td(), ty.to_ref());
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-09-25 17:21:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the number of bytes between successive elements of type T in an
|
|
|
|
// array of T. This is the "ABI" size. It includes any ABI-mandated padding.
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn llsize_of_alloc(cx: &CrateContext, ty: Type) -> llsize {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2015-07-16 17:48:16 -05:00
|
|
|
return llvm::LLVMABISizeOfType(cx.td(), ty.to_ref());
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-09-25 17:21:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns, as near as we can figure, the "real" size of a type. As in, the
|
|
|
|
// bits in this number of bytes actually carry data related to the datum
|
2015-01-05 20:18:32 -06:00
|
|
|
// with the type. Not junk, accidentally-damaged words, or whatever.
|
|
|
|
// Note that padding of the type will be included for structs, but not for the
|
|
|
|
// other types (i.e. SIMD types).
|
|
|
|
// Rounds up to the nearest byte though, so if you have a 1-bit
|
2012-12-05 22:21:29 -06:00
|
|
|
// value, we return 1 here, not 0. Most of rustc works in bytes. Be warned
|
|
|
|
// that LLVM *does* distinguish between e.g. a 1-bit value and an 8-bit value
|
|
|
|
// at the codegen level! In general you should prefer `llbitsize_of_real`
|
|
|
|
// below.
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn llsize_of_real(cx: &CrateContext, ty: Type) -> llsize {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2015-07-16 17:48:16 -05:00
|
|
|
let nbits = llvm::LLVMSizeOfTypeInBits(cx.td(), ty.to_ref());
|
2014-01-30 16:19:19 -06:00
|
|
|
if nbits & 7 != 0 {
|
2013-01-10 23:23:07 -06:00
|
|
|
// Not an even number of bytes, spills into "next" byte.
|
2014-01-30 16:19:19 -06:00
|
|
|
1 + (nbits >> 3)
|
2013-01-10 23:23:07 -06:00
|
|
|
} else {
|
|
|
|
nbits >> 3
|
|
|
|
}
|
2012-09-25 17:21:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 22:21:29 -06:00
|
|
|
/// Returns the "real" size of the type in bits.
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn llbitsize_of_real(cx: &CrateContext, ty: Type) -> llbits {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2015-07-16 17:48:16 -05:00
|
|
|
llvm::LLVMSizeOfTypeInBits(cx.td(), ty.to_ref())
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-12-05 22:21:29 -06:00
|
|
|
}
|
|
|
|
|
2013-02-08 03:52:47 -06:00
|
|
|
/// Returns the size of the type as an LLVM constant integer value.
|
2013-06-15 22:45:48 -05:00
|
|
|
pub fn llsize_of(cx: &CrateContext, ty: Type) -> ValueRef {
|
2013-02-08 03:52:47 -06:00
|
|
|
// Once upon a time, this called LLVMSizeOf, which does a
|
|
|
|
// getelementptr(1) on a null pointer and casts to an int, in
|
|
|
|
// order to obtain the type size as a value without requiring the
|
|
|
|
// target data layout. But we have the target data layout, so
|
|
|
|
// there's no need for that contrivance. The instruction
|
|
|
|
// selection DAG generator would flatten that GEP(1) node into a
|
|
|
|
// constant of the type's alloc size, so let's save it some work.
|
2014-10-14 15:36:11 -05:00
|
|
|
return C_uint(cx, llsize_of_alloc(cx, ty));
|
2012-09-25 17:21:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the preferred alignment of the given type for the current target.
|
2013-05-08 12:34:47 -05:00
|
|
|
// The preferred alignment may be larger than the alignment used when
|
2012-09-25 17:21:16 -05:00
|
|
|
// packing the type into structs. This will be used for things like
|
|
|
|
// allocations inside a stack frame, which LLVM has a free hand in.
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn llalign_of_pref(cx: &CrateContext, ty: Type) -> llalign {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2015-07-16 17:48:16 -05:00
|
|
|
return llvm::LLVMPreferredAlignmentOfType(cx.td(), ty.to_ref());
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-09-25 17:21:16 -05:00
|
|
|
}
|
|
|
|
|
2013-05-08 12:34:47 -05:00
|
|
|
// Returns the minimum alignment of a type required by the platform.
|
2012-09-25 17:21:16 -05:00
|
|
|
// This is the alignment that will be used for struct fields, arrays,
|
|
|
|
// and similar ABI-mandated things.
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn llalign_of_min(cx: &CrateContext, ty: Type) -> llalign {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2015-07-16 17:48:16 -05:00
|
|
|
return llvm::LLVMABIAlignmentOfType(cx.td(), ty.to_ref());
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-09-25 17:21:16 -05:00
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
pub fn llelement_offset(cx: &CrateContext, struct_ty: Type, element: usize) -> u64 {
|
2013-06-26 09:00:42 -05:00
|
|
|
unsafe {
|
2015-07-16 17:48:16 -05:00
|
|
|
return llvm::LLVMOffsetOfElement(cx.td(),
|
2015-04-19 17:52:26 -05:00
|
|
|
struct_ty.to_ref(),
|
2014-10-14 15:36:11 -05:00
|
|
|
element as u32);
|
2013-06-26 09:00:42 -05:00
|
|
|
}
|
|
|
|
}
|