2010-03-31 04:11:27 +04:00
|
|
|
//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
|
2009-07-23 07:17:50 +04:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-03-31 04:11:27 +04:00
|
|
|
// Builder implementation for CGRecordLayout objects.
|
2009-07-23 07:17:50 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-03-31 02:26:10 +04:00
|
|
|
#include "CGRecordLayout.h"
|
2009-07-23 07:17:50 +04:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Attr.h"
|
2010-11-25 01:50:27 +03:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2009-07-23 07:17:50 +04:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/RecordLayout.h"
|
IRgen: Add a -fuse-register-sized-bitfield-access option, for testing.
- Changes bit-field access policy to try to use (aligned) register sized accesses.
The idea here is that by using larger accesses we expose more coalescing
potential to the backend when we have situations like adjacent bit-fields in the
same structure (which is common), and that the backend should be smart enough to
narrow the accesses down when no coalescing is done or when it is shown not to
be profitable.
--
$ clang -m32 -O3 -S -o - t.c
_f0: ## @f0
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movb (%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, (%eax)
movb 1(%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, 1(%eax)
movb 2(%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, 2(%eax)
movb 3(%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, 3(%eax)
popl %ebp
ret
$ clang -m32 -O3 -S -o - t.c -Xclang -fuse-register-sized-bitfield-access
_f0: ## @f0
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movl $-2139062144, %ecx ## imm = 0xFFFFFFFF80808080
andl (%eax), %ecx
orl $16843009, %ecx ## imm = 0x1010101
movl %ecx, (%eax)
popl %ebp
ret
--
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133532 91177308-0d34-0410-b5e6-96231b3b80d8
2011-06-21 22:54:46 +04:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2009-07-23 07:17:50 +04:00
|
|
|
#include "CodeGenTypes.h"
|
2010-08-23 01:01:12 +04:00
|
|
|
#include "CGCXXABI.h"
|
2009-07-23 07:17:50 +04:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2010-04-12 22:14:18 +04:00
|
|
|
#include "llvm/Type.h"
|
2010-04-21 23:10:49 +04:00
|
|
|
#include "llvm/Support/Debug.h"
|
2010-04-12 22:14:18 +04:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-07-23 07:17:50 +04:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2010-12-01 02:17:27 +03:00
|
|
|
namespace {
|
2010-03-31 04:11:27 +04:00
|
|
|
|
|
|
|
class CGRecordLayoutBuilder {
|
|
|
|
public:
|
|
|
|
/// FieldTypes - Holds the LLVM types that the struct is created from.
|
2011-02-15 09:40:56 +03:00
|
|
|
///
|
2011-07-23 14:55:15 +04:00
|
|
|
SmallVector<llvm::Type *, 16> FieldTypes;
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
/// BaseSubobjectType - Holds the LLVM type for the non-virtual part
|
2010-11-09 08:25:47 +03:00
|
|
|
/// of the struct. For example, consider:
|
|
|
|
///
|
|
|
|
/// struct A { int i; };
|
|
|
|
/// struct B { void *v; };
|
|
|
|
/// struct C : virtual A, B { };
|
|
|
|
///
|
|
|
|
/// The LLVM type of C will be
|
|
|
|
/// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
|
|
|
|
///
|
|
|
|
/// And the LLVM type of the non-virtual base struct will be
|
|
|
|
/// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
|
2011-02-15 09:40:56 +03:00
|
|
|
///
|
|
|
|
/// This only gets initialized if the base subobject type is
|
|
|
|
/// different from the complete-object type.
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::StructType *BaseSubobjectType;
|
2010-11-09 08:25:47 +03:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
/// FieldInfo - Holds a field and its corresponding LLVM field number.
|
|
|
|
llvm::DenseMap<const FieldDecl *, unsigned> Fields;
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
/// BitFieldInfo - Holds location and size information about a bit field.
|
|
|
|
llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
|
2010-11-25 01:50:27 +03:00
|
|
|
|
|
|
|
/// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
|
|
|
|
/// primary base classes for some other direct or indirect base class.
|
|
|
|
CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
|
|
|
|
|
2010-11-28 22:18:44 +03:00
|
|
|
/// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
|
|
|
|
/// avoid laying out virtual bases more than once.
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
|
|
|
|
|
2010-08-23 01:01:12 +04:00
|
|
|
/// IsZeroInitializable - Whether this struct can be C++
|
|
|
|
/// zero-initialized with an LLVM zeroinitializer.
|
|
|
|
bool IsZeroInitializable;
|
2011-02-15 09:40:56 +03:00
|
|
|
bool IsZeroInitializableAsBase;
|
2010-03-31 04:11:27 +04:00
|
|
|
|
|
|
|
/// Packed - Whether the resulting LLVM struct will be packed or not.
|
|
|
|
bool Packed;
|
2011-04-27 03:52:16 +04:00
|
|
|
|
|
|
|
/// IsMsStruct - Whether ms_struct is in effect or not
|
|
|
|
bool IsMsStruct;
|
2010-03-31 04:11:27 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
CodeGenTypes &Types;
|
|
|
|
|
2011-04-18 01:56:13 +04:00
|
|
|
/// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the
|
|
|
|
/// last base laid out. Used so that we can replace the last laid out base
|
|
|
|
/// type with an i8 array if needed.
|
|
|
|
struct LastLaidOutBaseInfo {
|
|
|
|
CharUnits Offset;
|
|
|
|
CharUnits NonVirtualSize;
|
|
|
|
|
|
|
|
bool isValid() const { return !NonVirtualSize.isZero(); }
|
|
|
|
void invalidate() { NonVirtualSize = CharUnits::Zero(); }
|
|
|
|
|
|
|
|
} LastLaidOutBase;
|
|
|
|
|
2010-03-31 04:11:27 +04:00
|
|
|
/// Alignment - Contains the alignment of the RecordDecl.
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits Alignment;
|
2010-03-31 04:11:27 +04:00
|
|
|
|
|
|
|
/// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
|
|
|
|
/// this will have the number of bits still available in the field.
|
|
|
|
char BitsAvailableInLastField;
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
/// NextFieldOffset - Holds the next field offset.
|
|
|
|
CharUnits NextFieldOffset;
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2010-04-18 00:49:27 +04:00
|
|
|
/// LayoutUnionField - Will layout a field in an union and return the type
|
|
|
|
/// that the field will have.
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *LayoutUnionField(const FieldDecl *Field,
|
|
|
|
const ASTRecordLayout &Layout);
|
2010-04-18 00:49:27 +04:00
|
|
|
|
2010-03-31 04:11:27 +04:00
|
|
|
/// LayoutUnion - Will layout a union RecordDecl.
|
|
|
|
void LayoutUnion(const RecordDecl *D);
|
|
|
|
|
|
|
|
/// LayoutField - try to layout all fields in the record decl.
|
|
|
|
/// Returns false if the operation failed because the struct is not packed.
|
|
|
|
bool LayoutFields(const RecordDecl *D);
|
|
|
|
|
2010-12-05 02:59:48 +03:00
|
|
|
/// Layout a single base, virtual or non-virtual
|
2011-02-16 01:21:29 +03:00
|
|
|
void LayoutBase(const CXXRecordDecl *base,
|
|
|
|
const CGRecordLayout &baseLayout,
|
|
|
|
CharUnits baseOffset);
|
2010-12-05 02:59:48 +03:00
|
|
|
|
2010-11-25 04:59:35 +03:00
|
|
|
/// LayoutVirtualBase - layout a single virtual base.
|
2011-02-16 01:21:29 +03:00
|
|
|
void LayoutVirtualBase(const CXXRecordDecl *base,
|
|
|
|
CharUnits baseOffset);
|
2010-11-25 04:59:35 +03:00
|
|
|
|
2010-11-28 22:18:44 +03:00
|
|
|
/// LayoutVirtualBases - layout the virtual bases of a record decl.
|
|
|
|
void LayoutVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
const ASTRecordLayout &Layout);
|
2011-11-08 08:01:03 +04:00
|
|
|
|
|
|
|
/// MSLayoutVirtualBases - layout the virtual bases of a record decl,
|
|
|
|
/// like MSVC.
|
|
|
|
void MSLayoutVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
const ASTRecordLayout &Layout);
|
2010-11-28 22:18:44 +03:00
|
|
|
|
2010-05-18 09:12:20 +04:00
|
|
|
/// LayoutNonVirtualBase - layout a single non-virtual base.
|
2011-02-16 01:21:29 +03:00
|
|
|
void LayoutNonVirtualBase(const CXXRecordDecl *base,
|
|
|
|
CharUnits baseOffset);
|
2010-05-18 09:12:20 +04:00
|
|
|
|
2010-11-28 22:18:44 +03:00
|
|
|
/// LayoutNonVirtualBases - layout the virtual bases of a record decl.
|
2010-05-18 09:12:20 +04:00
|
|
|
void LayoutNonVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
const ASTRecordLayout &Layout);
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2010-11-09 08:25:47 +03:00
|
|
|
/// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
|
2010-12-10 03:11:00 +03:00
|
|
|
bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
|
2010-11-09 08:25:47 +03:00
|
|
|
|
2010-03-31 04:11:27 +04:00
|
|
|
/// LayoutField - layout a single field. Returns false if the operation failed
|
|
|
|
/// because the current struct is not packed.
|
|
|
|
bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
|
|
|
|
|
|
|
|
/// LayoutBitField - layout a single bit field.
|
|
|
|
void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
|
|
|
|
|
|
|
|
/// AppendField - Appends a field with the given offset and type.
|
2011-07-09 21:41:47 +04:00
|
|
|
void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy);
|
2010-03-31 04:11:27 +04:00
|
|
|
|
|
|
|
/// AppendPadding - Appends enough padding bytes so that the total
|
|
|
|
/// struct size is a multiple of the field alignment.
|
2011-02-16 01:21:29 +03:00
|
|
|
void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2011-04-18 01:56:13 +04:00
|
|
|
/// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
|
|
|
|
/// tail padding of a previous base. If this happens, the type of the previous
|
|
|
|
/// base needs to be changed to an array of i8. Returns true if the last
|
|
|
|
/// laid out base was resized.
|
|
|
|
bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
|
|
|
|
|
2010-11-09 08:25:47 +03:00
|
|
|
/// getByteArrayType - Returns a byte array type with the given number of
|
|
|
|
/// elements.
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *getByteArrayType(CharUnits NumBytes);
|
2010-11-09 08:25:47 +03:00
|
|
|
|
2010-03-31 04:11:27 +04:00
|
|
|
/// AppendBytes - Append a given number of bytes to the record.
|
2011-02-16 01:21:29 +03:00
|
|
|
void AppendBytes(CharUnits numBytes);
|
2010-03-31 04:11:27 +04:00
|
|
|
|
|
|
|
/// AppendTailPadding - Append enough tail padding so that the type will have
|
|
|
|
/// the passed size.
|
2011-04-24 20:53:44 +04:00
|
|
|
void AppendTailPadding(CharUnits RecordSize);
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
CharUnits getTypeAlignment(llvm::Type *Ty) const;
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2010-11-29 02:06:23 +03:00
|
|
|
/// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
|
|
|
|
/// LLVM element types.
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits getAlignmentAsLLVMStruct() const;
|
2010-11-29 02:06:23 +03:00
|
|
|
|
2010-08-23 01:01:12 +04:00
|
|
|
/// CheckZeroInitializable - Check if the given type contains a pointer
|
2010-03-31 04:11:27 +04:00
|
|
|
/// to data member.
|
2010-08-23 01:01:12 +04:00
|
|
|
void CheckZeroInitializable(QualType T);
|
2010-03-31 04:11:27 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
CGRecordLayoutBuilder(CodeGenTypes &Types)
|
2011-02-15 09:40:56 +03:00
|
|
|
: BaseSubobjectType(0),
|
|
|
|
IsZeroInitializable(true), IsZeroInitializableAsBase(true),
|
2011-04-27 03:52:16 +04:00
|
|
|
Packed(false), IsMsStruct(false),
|
|
|
|
Types(Types), BitsAvailableInLastField(0) { }
|
2010-03-31 04:11:27 +04:00
|
|
|
|
|
|
|
/// Layout - Will layout a RecordDecl.
|
|
|
|
void Layout(const RecordDecl *D);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
|
2011-02-16 01:21:29 +03:00
|
|
|
Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
|
2009-09-02 21:51:33 +04:00
|
|
|
Packed = D->hasAttr<PackedAttr>();
|
2011-04-27 03:52:16 +04:00
|
|
|
|
|
|
|
IsMsStruct = D->hasAttr<MsStructAttr>();
|
2009-08-08 23:38:24 +04:00
|
|
|
|
2009-07-23 07:43:54 +04:00
|
|
|
if (D->isUnion()) {
|
|
|
|
LayoutUnion(D);
|
|
|
|
return;
|
|
|
|
}
|
2009-08-08 22:23:56 +04:00
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
if (LayoutFields(D))
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
// We weren't able to layout the struct. Try again with a packed struct
|
2009-07-23 21:24:40 +04:00
|
|
|
Packed = true;
|
2011-04-18 01:56:13 +04:00
|
|
|
LastLaidOutBase.invalidate();
|
2011-02-16 01:21:29 +03:00
|
|
|
NextFieldOffset = CharUnits::Zero();
|
2009-07-23 07:17:50 +04:00
|
|
|
FieldTypes.clear();
|
2011-02-15 09:40:56 +03:00
|
|
|
Fields.clear();
|
|
|
|
BitFields.clear();
|
|
|
|
NonVirtualBases.clear();
|
|
|
|
VirtualBases.clear();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
LayoutFields(D);
|
|
|
|
}
|
|
|
|
|
2010-09-03 03:53:28 +04:00
|
|
|
CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
|
|
|
|
const FieldDecl *FD,
|
|
|
|
uint64_t FieldOffset,
|
|
|
|
uint64_t FieldSize,
|
|
|
|
uint64_t ContainingTypeSizeInBits,
|
|
|
|
unsigned ContainingTypeAlign) {
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
|
2011-02-26 11:41:59 +03:00
|
|
|
CharUnits TypeSizeInBytes =
|
|
|
|
CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(Ty));
|
|
|
|
uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
|
2010-04-13 01:01:28 +04:00
|
|
|
|
2011-05-20 20:38:50 +04:00
|
|
|
bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
|
2010-04-13 01:01:28 +04:00
|
|
|
|
2010-04-16 20:23:02 +04:00
|
|
|
if (FieldSize > TypeSizeInBits) {
|
2010-04-18 02:54:57 +04:00
|
|
|
// We have a wide bit-field. The extra bits are only used for padding, so
|
|
|
|
// if we have a bitfield of type T, with size N:
|
|
|
|
//
|
|
|
|
// T t : N;
|
|
|
|
//
|
|
|
|
// We can just assume that it's:
|
|
|
|
//
|
|
|
|
// T t : sizeof(T);
|
|
|
|
//
|
|
|
|
FieldSize = TypeSizeInBits;
|
2010-04-16 20:23:02 +04:00
|
|
|
}
|
|
|
|
|
2011-02-18 01:09:58 +03:00
|
|
|
// in big-endian machines the first fields are in higher bit positions,
|
|
|
|
// so revert the offset. The byte offsets are reversed(back) later.
|
|
|
|
if (Types.getTargetData().isBigEndian()) {
|
|
|
|
FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize);
|
|
|
|
}
|
|
|
|
|
2010-04-22 06:35:46 +04:00
|
|
|
// Compute the access components. The policy we use is to start by attempting
|
|
|
|
// to access using the width of the bit-field type itself and to always access
|
|
|
|
// at aligned indices of that type. If such an access would fail because it
|
|
|
|
// extends past the bound of the type, then we reduce size to the next smaller
|
|
|
|
// power of two and retry. The current algorithm assumes pow2 sized types,
|
|
|
|
// although this is easy to fix.
|
|
|
|
//
|
|
|
|
assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
|
|
|
|
CGBitFieldInfo::AccessInfo Components[3];
|
|
|
|
unsigned NumComponents = 0;
|
2011-03-22 20:35:47 +03:00
|
|
|
unsigned AccessedTargetBits = 0; // The number of target bits accessed.
|
2010-04-22 06:35:46 +04:00
|
|
|
unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
|
|
|
|
|
IRgen: Add a -fuse-register-sized-bitfield-access option, for testing.
- Changes bit-field access policy to try to use (aligned) register sized accesses.
The idea here is that by using larger accesses we expose more coalescing
potential to the backend when we have situations like adjacent bit-fields in the
same structure (which is common), and that the backend should be smart enough to
narrow the accesses down when no coalescing is done or when it is shown not to
be profitable.
--
$ clang -m32 -O3 -S -o - t.c
_f0: ## @f0
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movb (%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, (%eax)
movb 1(%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, 1(%eax)
movb 2(%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, 2(%eax)
movb 3(%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, 3(%eax)
popl %ebp
ret
$ clang -m32 -O3 -S -o - t.c -Xclang -fuse-register-sized-bitfield-access
_f0: ## @f0
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movl $-2139062144, %ecx ## imm = 0xFFFFFFFF80808080
andl (%eax), %ecx
orl $16843009, %ecx ## imm = 0x1010101
movl %ecx, (%eax)
popl %ebp
ret
--
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133532 91177308-0d34-0410-b5e6-96231b3b80d8
2011-06-21 22:54:46 +04:00
|
|
|
// If requested, widen the initial bit-field access to be register sized. The
|
|
|
|
// theory is that this is most likely to allow multiple accesses into the same
|
|
|
|
// structure to be coalesced, and that the backend should be smart enough to
|
|
|
|
// narrow the store if no coalescing is ever done.
|
|
|
|
//
|
|
|
|
// The subsequent code will handle align these access to common boundaries and
|
|
|
|
// guaranteeing that we do not access past the end of the structure.
|
|
|
|
if (Types.getCodeGenOpts().UseRegisterSizedBitfieldAccess) {
|
|
|
|
if (AccessWidth < Types.getTarget().getRegisterWidth())
|
|
|
|
AccessWidth = Types.getTarget().getRegisterWidth();
|
|
|
|
}
|
|
|
|
|
2010-04-22 06:35:46 +04:00
|
|
|
// Round down from the field offset to find the first access position that is
|
|
|
|
// at an aligned offset of the initial access type.
|
2010-04-22 19:22:33 +04:00
|
|
|
uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
|
|
|
|
|
|
|
|
// Adjust initial access size to fit within record.
|
2011-02-26 11:41:59 +03:00
|
|
|
while (AccessWidth > Types.getTarget().getCharWidth() &&
|
2010-04-22 19:22:33 +04:00
|
|
|
AccessStart + AccessWidth > ContainingTypeSizeInBits) {
|
|
|
|
AccessWidth >>= 1;
|
|
|
|
AccessStart = FieldOffset - (FieldOffset % AccessWidth);
|
|
|
|
}
|
2010-04-22 06:35:46 +04:00
|
|
|
|
|
|
|
while (AccessedTargetBits < FieldSize) {
|
|
|
|
// Check that we can access using a type of this size, without reading off
|
|
|
|
// the end of the structure. This can occur with packed structures and
|
|
|
|
// -fno-bitfield-type-align, for example.
|
|
|
|
if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
|
|
|
|
// If so, reduce access size to the next smaller power-of-two and retry.
|
|
|
|
AccessWidth >>= 1;
|
2011-02-26 11:41:59 +03:00
|
|
|
assert(AccessWidth >= Types.getTarget().getCharWidth()
|
|
|
|
&& "Cannot access under byte size!");
|
2010-04-22 06:35:46 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, add an access component.
|
|
|
|
|
|
|
|
// First, compute the bits inside this access which are part of the
|
|
|
|
// target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
|
|
|
|
// intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
|
|
|
|
// in the target that we are reading.
|
2010-04-22 19:22:33 +04:00
|
|
|
assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
|
|
|
|
assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
|
2010-04-22 06:35:46 +04:00
|
|
|
uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
|
|
|
|
uint64_t AccessBitsInFieldSize =
|
2010-04-22 19:22:33 +04:00
|
|
|
std::min(AccessWidth + AccessStart,
|
|
|
|
FieldOffset + FieldSize) - AccessBitsInFieldStart;
|
2010-04-22 18:56:10 +04:00
|
|
|
|
2010-04-22 06:35:46 +04:00
|
|
|
assert(NumComponents < 3 && "Unexpected number of components!");
|
|
|
|
CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
|
|
|
|
AI.FieldIndex = 0;
|
|
|
|
// FIXME: We still follow the old access pattern of only using the field
|
|
|
|
// byte offset. We should switch this once we fix the struct layout to be
|
|
|
|
// pretty.
|
2011-02-18 01:09:58 +03:00
|
|
|
|
|
|
|
// on big-endian machines we reverted the bit offset because first fields are
|
|
|
|
// in higher bits. But this also reverts the bytes, so fix this here by reverting
|
|
|
|
// the byte offset on big-endian machines.
|
|
|
|
if (Types.getTargetData().isBigEndian()) {
|
2011-04-24 14:04:59 +04:00
|
|
|
AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(
|
|
|
|
ContainingTypeSizeInBits - AccessStart - AccessWidth);
|
2011-02-18 01:09:58 +03:00
|
|
|
} else {
|
2011-04-24 14:04:59 +04:00
|
|
|
AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart);
|
2011-02-18 01:09:58 +03:00
|
|
|
}
|
2010-04-22 06:35:46 +04:00
|
|
|
AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
|
|
|
|
AI.AccessWidth = AccessWidth;
|
2011-04-24 14:13:17 +04:00
|
|
|
AI.AccessAlignment = Types.getContext().toCharUnitsFromBits(
|
|
|
|
llvm::MinAlign(ContainingTypeAlign, AccessStart));
|
2010-04-22 06:35:46 +04:00
|
|
|
AI.TargetBitOffset = AccessedTargetBits;
|
|
|
|
AI.TargetBitWidth = AccessBitsInFieldSize;
|
|
|
|
|
|
|
|
AccessStart += AccessWidth;
|
|
|
|
AccessedTargetBits += AI.TargetBitWidth;
|
2010-04-14 00:58:55 +04:00
|
|
|
}
|
|
|
|
|
2010-04-22 06:35:46 +04:00
|
|
|
assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
|
2010-04-15 09:09:32 +04:00
|
|
|
return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
|
2010-04-13 01:01:28 +04:00
|
|
|
}
|
|
|
|
|
2010-09-03 03:53:28 +04:00
|
|
|
CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
|
|
|
|
const FieldDecl *FD,
|
|
|
|
uint64_t FieldOffset,
|
|
|
|
uint64_t FieldSize) {
|
|
|
|
const RecordDecl *RD = FD->getParent();
|
|
|
|
const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
|
2011-02-11 04:54:29 +03:00
|
|
|
uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
|
2011-02-15 05:32:40 +03:00
|
|
|
unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
|
2010-09-03 03:53:28 +04:00
|
|
|
|
|
|
|
return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
|
|
|
|
ContainingTypeAlign);
|
|
|
|
}
|
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
|
2011-02-16 01:21:29 +03:00
|
|
|
uint64_t fieldOffset) {
|
2011-10-10 22:28:20 +04:00
|
|
|
uint64_t fieldSize = D->getBitWidthValue(Types.getContext());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
if (fieldSize == 0)
|
2009-07-23 07:17:50 +04:00
|
|
|
return;
|
|
|
|
|
2011-02-26 11:41:59 +03:00
|
|
|
uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
|
2011-04-24 20:40:29 +04:00
|
|
|
CharUnits numBytesToAppend;
|
2011-09-02 04:18:52 +04:00
|
|
|
unsigned charAlign = Types.getContext().getTargetInfo().getCharAlign();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-04-18 01:56:13 +04:00
|
|
|
if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) {
|
2011-04-24 20:40:29 +04:00
|
|
|
assert(fieldOffset % charAlign == 0 &&
|
|
|
|
"Field offset not aligned correctly");
|
2011-04-18 01:56:13 +04:00
|
|
|
|
|
|
|
CharUnits fieldOffsetInCharUnits =
|
|
|
|
Types.getContext().toCharUnitsFromBits(fieldOffset);
|
|
|
|
|
|
|
|
// Try to resize the last base field.
|
|
|
|
if (ResizeLastBaseFieldIfNecessary(fieldOffsetInCharUnits))
|
|
|
|
nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
|
|
|
|
}
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
if (fieldOffset < nextFieldOffsetInBits) {
|
2009-07-23 07:17:50 +04:00
|
|
|
assert(BitsAvailableInLastField && "Bitfield size mismatch!");
|
2011-02-16 01:21:29 +03:00
|
|
|
assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
// The bitfield begins in the previous bit-field.
|
2011-04-24 20:40:29 +04:00
|
|
|
numBytesToAppend = Types.getContext().toCharUnitsFromBits(
|
|
|
|
llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField,
|
|
|
|
charAlign));
|
2009-07-23 07:17:50 +04:00
|
|
|
} else {
|
2011-04-24 20:40:29 +04:00
|
|
|
assert(fieldOffset % charAlign == 0 &&
|
|
|
|
"Field offset not aligned correctly");
|
2009-07-23 07:17:50 +04:00
|
|
|
|
|
|
|
// Append padding if necessary.
|
2011-04-24 20:40:29 +04:00
|
|
|
AppendPadding(Types.getContext().toCharUnitsFromBits(fieldOffset),
|
|
|
|
CharUnits::One());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-04-24 20:40:29 +04:00
|
|
|
numBytesToAppend = Types.getContext().toCharUnitsFromBits(
|
|
|
|
llvm::RoundUpToAlignment(fieldSize, charAlign));
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-04-24 20:40:29 +04:00
|
|
|
assert(!numBytesToAppend.isZero() && "No bytes to append!");
|
2009-07-23 07:17:50 +04:00
|
|
|
}
|
|
|
|
|
2010-04-13 01:01:28 +04:00
|
|
|
// Add the bit field info.
|
2011-02-15 09:40:56 +03:00
|
|
|
BitFields.insert(std::make_pair(D,
|
2011-02-16 01:21:29 +03:00
|
|
|
CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-04-24 20:40:29 +04:00
|
|
|
AppendBytes(numBytesToAppend);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
BitsAvailableInLastField =
|
2011-04-24 20:40:29 +04:00
|
|
|
Types.getContext().toBits(NextFieldOffset) - (fieldOffset + fieldSize);
|
2009-07-23 07:17:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
|
2011-02-16 01:21:29 +03:00
|
|
|
uint64_t fieldOffset) {
|
2009-07-23 07:17:50 +04:00
|
|
|
// If the field is packed, then we need a packed struct.
|
2009-08-08 22:23:56 +04:00
|
|
|
if (!Packed && D->hasAttr<PackedAttr>())
|
2009-07-23 07:17:50 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (D->isBitField()) {
|
|
|
|
// We must use packed structs for unnamed bit fields since they
|
|
|
|
// don't affect the struct alignment.
|
2009-07-23 21:24:40 +04:00
|
|
|
if (!Packed && !D->getDeclName())
|
2009-07-23 07:17:50 +04:00
|
|
|
return false;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
LayoutBitField(D, fieldOffset);
|
2009-07-23 07:17:50 +04:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-23 01:01:12 +04:00
|
|
|
CheckZeroInitializable(D->getType());
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2011-02-26 11:41:59 +03:00
|
|
|
assert(fieldOffset % Types.getTarget().getCharWidth() == 0
|
|
|
|
&& "field offset is not on a byte boundary!");
|
|
|
|
CharUnits fieldOffsetInBytes
|
|
|
|
= Types.getContext().toCharUnitsFromBits(fieldOffset);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *Ty = Types.ConvertTypeForMem(D->getType());
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits typeAlignment = getTypeAlignment(Ty);
|
2009-07-23 07:17:50 +04:00
|
|
|
|
2009-08-08 23:38:24 +04:00
|
|
|
// If the type alignment is larger then the struct alignment, we must use
|
|
|
|
// a packed struct.
|
2011-02-16 01:21:29 +03:00
|
|
|
if (typeAlignment > Alignment) {
|
2009-08-08 23:38:24 +04:00
|
|
|
assert(!Packed && "Alignment is wrong even with packed struct!");
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
if (!Packed) {
|
|
|
|
if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
|
|
|
|
const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
|
|
|
|
if (const MaxFieldAlignmentAttr *MFAA =
|
|
|
|
RD->getAttr<MaxFieldAlignmentAttr>()) {
|
2011-02-26 11:41:59 +03:00
|
|
|
if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
|
2011-02-16 01:21:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-08 23:38:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-04 20:29:15 +04:00
|
|
|
// Round up the field offset to the alignment of the field type.
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits alignedNextFieldOffsetInBytes =
|
|
|
|
NextFieldOffset.RoundUpToAlignment(typeAlignment);
|
2009-08-04 20:29:15 +04:00
|
|
|
|
2011-04-18 01:56:13 +04:00
|
|
|
if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
|
|
|
|
// Try to resize the last base field.
|
|
|
|
if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
|
|
|
|
alignedNextFieldOffsetInBytes =
|
|
|
|
NextFieldOffset.RoundUpToAlignment(typeAlignment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
|
2009-08-04 20:29:15 +04:00
|
|
|
assert(!Packed && "Could not place field even with packed struct!");
|
|
|
|
return false;
|
2009-07-23 07:17:50 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
AppendPadding(fieldOffsetInBytes, typeAlignment);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
// Now append the field.
|
2011-02-15 09:40:56 +03:00
|
|
|
Fields[D] = FieldTypes.size();
|
2011-02-16 01:21:29 +03:00
|
|
|
AppendField(fieldOffsetInBytes, Ty);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-04-18 01:56:13 +04:00
|
|
|
LastLaidOutBase.invalidate();
|
2009-07-23 07:17:50 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *
|
2010-04-18 00:49:27 +04:00
|
|
|
CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
|
|
|
|
const ASTRecordLayout &Layout) {
|
|
|
|
if (Field->isBitField()) {
|
2011-10-10 22:28:20 +04:00
|
|
|
uint64_t FieldSize = Field->getBitWidthValue(Types.getContext());
|
2010-04-18 00:49:27 +04:00
|
|
|
|
|
|
|
// Ignore zero sized bit fields.
|
|
|
|
if (FieldSize == 0)
|
|
|
|
return 0;
|
|
|
|
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
|
2011-04-24 20:47:33 +04:00
|
|
|
CharUnits NumBytesToAppend = Types.getContext().toCharUnitsFromBits(
|
|
|
|
llvm::RoundUpToAlignment(FieldSize,
|
2011-09-02 04:18:52 +04:00
|
|
|
Types.getContext().getTargetInfo().getCharAlign()));
|
2010-04-18 01:04:52 +04:00
|
|
|
|
2011-04-24 20:47:33 +04:00
|
|
|
if (NumBytesToAppend > CharUnits::One())
|
|
|
|
FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
|
2010-04-18 01:04:52 +04:00
|
|
|
|
2010-04-18 00:49:27 +04:00
|
|
|
// Add the bit field info.
|
2011-02-15 09:40:56 +03:00
|
|
|
BitFields.insert(std::make_pair(Field,
|
|
|
|
CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
|
2010-04-18 01:04:52 +04:00
|
|
|
return FieldTy;
|
2010-04-18 00:49:27 +04:00
|
|
|
}
|
2010-04-20 21:52:30 +04:00
|
|
|
|
2010-04-18 00:49:27 +04:00
|
|
|
// This is a regular union field.
|
2011-02-15 09:40:56 +03:00
|
|
|
Fields[Field] = 0;
|
2011-07-09 21:41:47 +04:00
|
|
|
return Types.ConvertTypeForMem(Field->getType());
|
2010-04-18 00:49:27 +04:00
|
|
|
}
|
|
|
|
|
2009-07-23 07:43:54 +04:00
|
|
|
void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
|
|
|
|
assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *unionType = 0;
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits unionSize = CharUnits::Zero();
|
|
|
|
CharUnits unionAlign = CharUnits::Zero();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
bool hasOnlyZeroSizedBitFields = true;
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
unsigned fieldNo = 0;
|
|
|
|
for (RecordDecl::field_iterator field = D->field_begin(),
|
|
|
|
fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
|
|
|
|
assert(layout.getFieldOffset(fieldNo) == 0 &&
|
2009-07-23 07:43:54 +04:00
|
|
|
"Union field offset did not start at the beginning of record!");
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *fieldType = LayoutUnionField(*field, layout);
|
2010-04-18 00:49:27 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
if (!fieldType)
|
2010-04-18 00:49:27 +04:00
|
|
|
continue;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
hasOnlyZeroSizedBitFields = false;
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits fieldAlign = CharUnits::fromQuantity(
|
|
|
|
Types.getTargetData().getABITypeAlignment(fieldType));
|
|
|
|
CharUnits fieldSize = CharUnits::fromQuantity(
|
|
|
|
Types.getTargetData().getTypeAllocSize(fieldType));
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
if (fieldAlign < unionAlign)
|
2009-07-23 07:43:54 +04:00
|
|
|
continue;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
if (fieldAlign > unionAlign || fieldSize > unionSize) {
|
|
|
|
unionType = fieldType;
|
|
|
|
unionAlign = fieldAlign;
|
|
|
|
unionSize = fieldSize;
|
2009-07-23 07:43:54 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-23 07:43:54 +04:00
|
|
|
// Now add our field.
|
2011-02-16 01:21:29 +03:00
|
|
|
if (unionType) {
|
|
|
|
AppendField(CharUnits::Zero(), unionType);
|
2009-09-04 02:56:02 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
if (getTypeAlignment(unionType) > layout.getAlignment()) {
|
2009-09-04 02:56:02 +04:00
|
|
|
// We need a packed struct.
|
|
|
|
Packed = true;
|
2011-02-16 01:21:29 +03:00
|
|
|
unionAlign = CharUnits::One();
|
2009-09-04 02:56:02 +04:00
|
|
|
}
|
|
|
|
}
|
2011-02-16 01:21:29 +03:00
|
|
|
if (unionAlign.isZero()) {
|
|
|
|
assert(hasOnlyZeroSizedBitFields &&
|
2010-01-28 21:22:03 +03:00
|
|
|
"0-align record did not have all zero-sized bit-fields!");
|
2011-02-16 01:21:29 +03:00
|
|
|
unionAlign = CharUnits::One();
|
2009-11-06 23:47:40 +03:00
|
|
|
}
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2009-07-23 07:43:54 +04:00
|
|
|
// Append tail padding.
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits recordSize = layout.getSize();
|
|
|
|
if (recordSize > unionSize)
|
|
|
|
AppendPadding(recordSize, unionAlign);
|
2009-07-23 07:43:54 +04:00
|
|
|
}
|
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
|
2011-02-16 01:21:29 +03:00
|
|
|
const CGRecordLayout &baseLayout,
|
|
|
|
CharUnits baseOffset) {
|
2011-04-18 01:56:13 +04:00
|
|
|
ResizeLastBaseFieldIfNecessary(baseOffset);
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
AppendPadding(baseOffset, CharUnits::One());
|
2010-12-05 02:59:48 +03:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
const ASTRecordLayout &baseASTLayout
|
|
|
|
= Types.getContext().getASTRecordLayout(base);
|
|
|
|
|
2011-04-18 01:56:13 +04:00
|
|
|
LastLaidOutBase.Offset = NextFieldOffset;
|
|
|
|
LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
// Fields and bases can be laid out in the tail padding of previous
|
|
|
|
// bases. If this happens, we need to allocate the base as an i8
|
|
|
|
// array; otherwise, we can use the subobject type. However,
|
|
|
|
// actually doing that would require knowledge of what immediately
|
|
|
|
// follows this base in the layout, so instead we do a conservative
|
|
|
|
// approximation, which is to use the base subobject type if it
|
|
|
|
// has the same LLVM storage size as the nvsize.
|
|
|
|
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
|
2011-04-18 01:56:13 +04:00
|
|
|
AppendField(baseOffset, subobjectType);
|
2010-12-05 02:59:48 +03:00
|
|
|
}
|
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits baseOffset) {
|
2010-11-25 04:59:35 +03:00
|
|
|
// Ignore empty bases.
|
2011-02-15 09:40:56 +03:00
|
|
|
if (base->isEmpty()) return;
|
2010-11-25 04:59:35 +03:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
|
|
|
|
if (IsZeroInitializableAsBase) {
|
|
|
|
assert(IsZeroInitializable &&
|
|
|
|
"class zero-initializable as base but not as complete object");
|
2010-11-25 04:59:35 +03:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
IsZeroInitializable = IsZeroInitializableAsBase =
|
|
|
|
baseLayout.isZeroInitializableAsBase();
|
|
|
|
}
|
2010-11-25 04:59:35 +03:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
LayoutBase(base, baseLayout, baseOffset);
|
2011-02-15 09:40:56 +03:00
|
|
|
NonVirtualBases[base] = (FieldTypes.size() - 1);
|
|
|
|
}
|
2010-11-25 04:59:35 +03:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
void
|
|
|
|
CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits baseOffset) {
|
2011-02-15 09:40:56 +03:00
|
|
|
// Ignore empty bases.
|
|
|
|
if (base->isEmpty()) return;
|
2010-11-25 04:59:35 +03:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
|
|
|
|
if (IsZeroInitializable)
|
|
|
|
IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
LayoutBase(base, baseLayout, baseOffset);
|
2011-02-15 09:40:56 +03:00
|
|
|
VirtualBases[base] = (FieldTypes.size() - 1);
|
2010-11-25 04:59:35 +03:00
|
|
|
}
|
|
|
|
|
2011-11-08 08:01:03 +04:00
|
|
|
void
|
|
|
|
CGRecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
const ASTRecordLayout &Layout) {
|
|
|
|
if (!RD->getNumVBases())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// The vbases list is uniqued and ordered by a depth-first
|
|
|
|
// traversal, which is what we need here.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
|
|
|
|
E = RD->vbases_end(); I != E; ++I) {
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
|
|
|
|
LayoutVirtualBase(BaseDecl, vbaseOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-28 22:18:44 +03:00
|
|
|
/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
|
|
|
|
void
|
|
|
|
CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
const ASTRecordLayout &Layout) {
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
// We only want to lay out virtual bases that aren't indirect primary bases
|
|
|
|
// of some other base.
|
|
|
|
if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
|
|
|
|
// Only lay out the base once.
|
|
|
|
if (!LaidOutVirtualBases.insert(BaseDecl))
|
|
|
|
continue;
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
|
|
|
|
LayoutVirtualBase(BaseDecl, vbaseOffset);
|
2010-11-28 22:18:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!BaseDecl->getNumVBases()) {
|
|
|
|
// This base isn't interesting since it doesn't have any virtual bases.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
LayoutVirtualBases(BaseDecl, Layout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-18 09:12:20 +04:00
|
|
|
void
|
|
|
|
CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
const ASTRecordLayout &Layout) {
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
|
2011-11-08 08:01:03 +04:00
|
|
|
// If we have a primary base, lay it out first.
|
|
|
|
if (PrimaryBase) {
|
|
|
|
if (!Layout.isPrimaryBaseVirtual())
|
|
|
|
LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero());
|
|
|
|
else
|
|
|
|
LayoutVirtualBase(PrimaryBase, CharUnits::Zero());
|
|
|
|
|
|
|
|
// Otherwise, add a vtable / vf-table if the layout says to do so.
|
|
|
|
} else if (Types.getContext().getTargetInfo().getCXXABI() == CXXABI_Microsoft
|
|
|
|
? Layout.getVFPtrOffset() != CharUnits::fromQuantity(-1)
|
|
|
|
: RD->isDynamicClass()) {
|
|
|
|
llvm::Type *FunctionType =
|
|
|
|
llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
|
|
|
|
/*isVarArg=*/true);
|
|
|
|
llvm::Type *VTableTy = FunctionType->getPointerTo();
|
|
|
|
|
|
|
|
assert(NextFieldOffset.isZero() &&
|
|
|
|
"VTable pointer must come first!");
|
|
|
|
AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
|
2010-05-18 09:12:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Layout the non-virtual bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
// We've already laid out the primary base.
|
2010-11-25 02:12:57 +03:00
|
|
|
if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
|
2010-05-18 09:12:20 +04:00
|
|
|
continue;
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
|
2009-12-16 20:27:20 +03:00
|
|
|
}
|
2011-11-08 08:01:03 +04:00
|
|
|
|
|
|
|
// Add a vb-table pointer if the layout insists.
|
|
|
|
if (Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1)) {
|
|
|
|
CharUnits VBPtrOffset = Layout.getVBPtrOffset();
|
|
|
|
llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext());
|
|
|
|
AppendPadding(VBPtrOffset, getTypeAlignment(Vbptr));
|
|
|
|
AppendField(VBPtrOffset, Vbptr);
|
|
|
|
}
|
2009-12-16 20:27:20 +03:00
|
|
|
}
|
|
|
|
|
2010-12-10 03:11:00 +03:00
|
|
|
bool
|
2010-11-09 08:25:47 +03:00
|
|
|
CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
|
|
|
|
const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
|
|
|
|
|
2011-02-08 05:02:47 +03:00
|
|
|
CharUnits NonVirtualSize = Layout.getNonVirtualSize();
|
|
|
|
CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits AlignedNonVirtualTypeSize =
|
|
|
|
NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
|
2010-11-09 08:25:47 +03:00
|
|
|
|
|
|
|
// First check if we can use the same fields as for the complete class.
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits RecordSize = Layout.getSize();
|
2011-02-15 09:40:56 +03:00
|
|
|
if (AlignedNonVirtualTypeSize == RecordSize)
|
2010-12-10 03:11:00 +03:00
|
|
|
return true;
|
2010-11-09 08:25:47 +03:00
|
|
|
|
|
|
|
// Check if we need padding.
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits AlignedNextFieldOffset =
|
|
|
|
NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
|
2010-11-09 08:25:47 +03:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
|
|
|
|
assert(!Packed && "cannot layout even as packed struct");
|
2010-12-10 03:11:00 +03:00
|
|
|
return false; // Needs packing.
|
2011-02-15 09:40:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
|
|
|
|
if (needsPadding) {
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
|
2011-02-15 09:40:56 +03:00
|
|
|
FieldTypes.push_back(getByteArrayType(NumBytes));
|
|
|
|
}
|
2011-07-09 21:41:47 +04:00
|
|
|
|
2011-08-12 21:43:31 +04:00
|
|
|
BaseSubobjectType = llvm::StructType::create(Types.getLLVMContext(),
|
|
|
|
FieldTypes, "", Packed);
|
2011-07-09 21:41:47 +04:00
|
|
|
Types.addRecordTypeName(RD, BaseSubobjectType, ".base");
|
2010-11-09 08:25:47 +03:00
|
|
|
|
2011-07-09 21:41:47 +04:00
|
|
|
// Pull the padding back off.
|
|
|
|
if (needsPadding)
|
2011-02-15 09:40:56 +03:00
|
|
|
FieldTypes.pop_back();
|
2010-11-09 08:25:47 +03:00
|
|
|
|
2010-12-10 03:11:00 +03:00
|
|
|
return true;
|
2010-11-09 08:25:47 +03:00
|
|
|
}
|
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
|
|
|
|
assert(!D->isUnion() && "Can't call LayoutFields on a union!");
|
2011-02-16 01:21:29 +03:00
|
|
|
assert(!Alignment.isZero() && "Did not set alignment!");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-23 07:43:54 +04:00
|
|
|
const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-11-09 08:25:47 +03:00
|
|
|
const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
|
|
|
|
if (RD)
|
2010-05-18 09:12:20 +04:00
|
|
|
LayoutNonVirtualBases(RD, Layout);
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
unsigned FieldNo = 0;
|
2011-04-27 03:52:16 +04:00
|
|
|
const FieldDecl *LastFD = 0;
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
for (RecordDecl::field_iterator Field = D->field_begin(),
|
2009-07-23 07:17:50 +04:00
|
|
|
FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
|
2011-04-27 03:52:16 +04:00
|
|
|
if (IsMsStruct) {
|
|
|
|
// Zero-length bitfields following non-bitfield members are
|
|
|
|
// ignored:
|
|
|
|
const FieldDecl *FD = (*Field);
|
2011-05-04 00:21:04 +04:00
|
|
|
if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
|
2011-04-27 03:52:16 +04:00
|
|
|
--FieldNo;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LastFD = FD;
|
|
|
|
}
|
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
|
2009-09-09 19:08:12 +04:00
|
|
|
assert(!Packed &&
|
2009-07-23 07:17:50 +04:00
|
|
|
"Could not layout fields even with a packed LLVM struct!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-25 04:59:35 +03:00
|
|
|
if (RD) {
|
2010-11-28 22:18:44 +03:00
|
|
|
// We've laid out the non-virtual bases and the fields, now compute the
|
|
|
|
// non-virtual base field types.
|
2010-12-10 03:11:00 +03:00
|
|
|
if (!ComputeNonVirtualBaseType(RD)) {
|
|
|
|
assert(!Packed && "Could not layout even with a packed LLVM struct!");
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-28 22:18:44 +03:00
|
|
|
|
2011-11-08 08:01:03 +04:00
|
|
|
// Lay out the virtual bases. The MS ABI uses a different
|
|
|
|
// algorithm here due to the lack of primary virtual bases.
|
|
|
|
if (Types.getContext().getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
|
|
|
|
RD->getIndirectPrimaryBases(IndirectPrimaryBases);
|
|
|
|
if (Layout.isPrimaryBaseVirtual())
|
|
|
|
IndirectPrimaryBases.insert(Layout.getPrimaryBase());
|
|
|
|
|
|
|
|
LayoutVirtualBases(RD, Layout);
|
|
|
|
} else {
|
|
|
|
MSLayoutVirtualBases(RD, Layout);
|
|
|
|
}
|
2010-11-25 04:59:35 +03:00
|
|
|
}
|
2010-11-09 08:25:47 +03:00
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
// Append tail padding if necessary.
|
2011-04-24 20:53:44 +04:00
|
|
|
AppendTailPadding(Layout.getSize());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-24 20:53:44 +04:00
|
|
|
void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
|
|
|
|
ResizeLastBaseFieldIfNecessary(RecordSize);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-04-24 20:53:44 +04:00
|
|
|
assert(NextFieldOffset <= RecordSize && "Size mismatch!");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits AlignedNextFieldOffset =
|
|
|
|
NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
|
2009-12-08 04:24:23 +03:00
|
|
|
|
2011-04-24 20:53:44 +04:00
|
|
|
if (AlignedNextFieldOffset == RecordSize) {
|
2009-12-08 04:24:23 +03:00
|
|
|
// We don't need any padding.
|
|
|
|
return;
|
|
|
|
}
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2011-04-24 20:53:44 +04:00
|
|
|
CharUnits NumPadBytes = RecordSize - NextFieldOffset;
|
2009-07-27 18:55:54 +04:00
|
|
|
AppendBytes(NumPadBytes);
|
|
|
|
}
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *fieldType) {
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits fieldSize =
|
|
|
|
CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType));
|
2009-07-24 06:45:50 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
FieldTypes.push_back(fieldType);
|
2009-07-23 07:17:50 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
NextFieldOffset = fieldOffset + fieldSize;
|
2009-07-23 07:17:50 +04:00
|
|
|
BitsAvailableInLastField = 0;
|
|
|
|
}
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
|
|
|
|
CharUnits fieldAlignment) {
|
|
|
|
assert(NextFieldOffset <= fieldOffset &&
|
2009-07-23 07:17:50 +04:00
|
|
|
"Incorrect field layout!");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-11-08 08:01:03 +04:00
|
|
|
// Do nothing if we're already at the right offset.
|
|
|
|
if (fieldOffset == NextFieldOffset) return;
|
2009-07-23 07:17:50 +04:00
|
|
|
|
2011-11-08 08:01:03 +04:00
|
|
|
// If we're not emitting a packed LLVM type, try to avoid adding
|
|
|
|
// unnecessary padding fields.
|
|
|
|
if (!Packed) {
|
|
|
|
// Round up the field offset to the alignment of the field type.
|
|
|
|
CharUnits alignedNextFieldOffset =
|
|
|
|
NextFieldOffset.RoundUpToAlignment(fieldAlignment);
|
|
|
|
assert(alignedNextFieldOffset <= fieldOffset);
|
2009-07-23 07:17:50 +04:00
|
|
|
|
2011-11-08 08:01:03 +04:00
|
|
|
// If that's the right offset, we're done.
|
|
|
|
if (alignedNextFieldOffset == fieldOffset) return;
|
2009-07-23 07:17:50 +04:00
|
|
|
}
|
2011-11-08 08:01:03 +04:00
|
|
|
|
|
|
|
// Otherwise we need explicit padding.
|
|
|
|
CharUnits padding = fieldOffset - NextFieldOffset;
|
|
|
|
AppendBytes(padding);
|
2009-07-23 07:17:50 +04:00
|
|
|
}
|
|
|
|
|
2011-04-18 01:56:13 +04:00
|
|
|
bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
|
|
|
|
// Check if we have a base to resize.
|
|
|
|
if (!LastLaidOutBase.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// This offset does not overlap with the tail padding.
|
|
|
|
if (offset >= NextFieldOffset)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Restore the field offset and append an i8 array instead.
|
|
|
|
FieldTypes.pop_back();
|
|
|
|
NextFieldOffset = LastLaidOutBase.Offset;
|
|
|
|
AppendBytes(LastLaidOutBase.NonVirtualSize);
|
|
|
|
LastLaidOutBase.invalidate();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
|
2011-02-16 01:21:29 +03:00
|
|
|
assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
|
2011-02-16 01:21:29 +03:00
|
|
|
if (numBytes > CharUnits::One())
|
|
|
|
Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-11-09 08:25:47 +03:00
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
|
|
|
|
if (numBytes.isZero())
|
2010-11-09 08:25:47 +03:00
|
|
|
return;
|
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
// Append the padding field
|
2011-02-16 01:21:29 +03:00
|
|
|
AppendField(NextFieldOffset, getByteArrayType(numBytes));
|
2009-07-23 07:17:50 +04:00
|
|
|
}
|
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
CharUnits CGRecordLayoutBuilder::getTypeAlignment(llvm::Type *Ty) const {
|
2009-07-23 21:24:40 +04:00
|
|
|
if (Packed)
|
2011-02-16 01:21:29 +03:00
|
|
|
return CharUnits::One();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty));
|
2009-07-23 07:17:50 +04:00
|
|
|
}
|
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
|
2010-11-29 02:06:23 +03:00
|
|
|
if (Packed)
|
2011-02-16 01:21:29 +03:00
|
|
|
return CharUnits::One();
|
2010-11-29 02:06:23 +03:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
CharUnits maxAlignment = CharUnits::One();
|
2010-11-29 02:06:23 +03:00
|
|
|
for (size_t i = 0; i != FieldTypes.size(); ++i)
|
2011-02-16 01:21:29 +03:00
|
|
|
maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
|
2010-11-29 02:06:23 +03:00
|
|
|
|
2011-02-16 01:21:29 +03:00
|
|
|
return maxAlignment;
|
2010-11-29 02:06:23 +03:00
|
|
|
}
|
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
/// Merge in whether a field of the given type is zero-initializable.
|
2010-08-23 01:01:12 +04:00
|
|
|
void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
|
2009-08-23 05:25:01 +04:00
|
|
|
// This record already contains a member pointer.
|
2011-02-15 09:40:56 +03:00
|
|
|
if (!IsZeroInitializableAsBase)
|
2009-08-23 05:25:01 +04:00
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-23 05:25:01 +04:00
|
|
|
// Can only have member pointers if we're compiling C++.
|
|
|
|
if (!Types.getContext().getLangOptions().CPlusPlus)
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
const Type *elementType = T->getBaseElementTypeUnsafe();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
|
2010-08-23 01:01:12 +04:00
|
|
|
if (!Types.getCXXABI().isZeroInitializable(MPT))
|
2011-02-15 09:40:56 +03:00
|
|
|
IsZeroInitializable = IsZeroInitializableAsBase = false;
|
|
|
|
} else if (const RecordType *RT = elementType->getAs<RecordType>()) {
|
2010-02-02 08:17:25 +03:00
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
2011-02-15 09:40:56 +03:00
|
|
|
const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
|
|
|
|
if (!Layout.isZeroInitializable())
|
|
|
|
IsZeroInitializable = IsZeroInitializableAsBase = false;
|
2010-05-18 20:51:41 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-31 04:11:27 +04:00
|
|
|
|
2011-07-09 21:41:47 +04:00
|
|
|
CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
|
|
|
|
llvm::StructType *Ty) {
|
2010-03-31 04:11:27 +04:00
|
|
|
CGRecordLayoutBuilder Builder(*this);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
Builder.Layout(D);
|
2009-07-24 19:20:52 +04:00
|
|
|
|
2011-07-09 21:41:47 +04:00
|
|
|
Ty->setBody(Builder.FieldTypes, Builder.Packed);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
// If we're in C++, compute the base subobject type.
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::StructType *BaseTy = 0;
|
2010-11-09 08:25:47 +03:00
|
|
|
if (isa<CXXRecordDecl>(D)) {
|
2011-02-15 09:40:56 +03:00
|
|
|
BaseTy = Builder.BaseSubobjectType;
|
|
|
|
if (!BaseTy) BaseTy = Ty;
|
2010-11-09 08:25:47 +03:00
|
|
|
}
|
|
|
|
|
2010-03-31 05:09:11 +04:00
|
|
|
CGRecordLayout *RL =
|
2011-02-15 09:40:56 +03:00
|
|
|
new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
|
|
|
|
Builder.IsZeroInitializableAsBase);
|
2010-03-31 05:09:11 +04:00
|
|
|
|
2011-02-15 09:40:56 +03:00
|
|
|
RL->NonVirtualBases.swap(Builder.NonVirtualBases);
|
|
|
|
RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
|
2010-05-18 09:22:06 +04:00
|
|
|
|
2009-07-23 07:17:50 +04:00
|
|
|
// Add all the field numbers.
|
2011-02-15 09:40:56 +03:00
|
|
|
RL->FieldInfo.swap(Builder.Fields);
|
2009-07-23 07:17:50 +04:00
|
|
|
|
|
|
|
// Add bitfield info.
|
2011-02-15 09:40:56 +03:00
|
|
|
RL->BitFields.swap(Builder.BitFields);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-20 00:44:47 +04:00
|
|
|
// Dump the layout, if requested.
|
2010-04-14 00:58:55 +04:00
|
|
|
if (getContext().getLangOptions().DumpRecordLayouts) {
|
2010-04-20 00:44:53 +04:00
|
|
|
llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
|
2010-04-14 00:58:55 +04:00
|
|
|
llvm::errs() << "Record: ";
|
|
|
|
D->dump();
|
|
|
|
llvm::errs() << "\nLayout: ";
|
2010-04-12 22:14:18 +04:00
|
|
|
RL->dump();
|
2010-04-14 00:58:55 +04:00
|
|
|
}
|
2010-04-12 22:14:18 +04:00
|
|
|
|
2010-04-22 06:35:46 +04:00
|
|
|
#ifndef NDEBUG
|
2010-04-20 00:44:47 +04:00
|
|
|
// Verify that the computed LLVM struct size matches the AST layout size.
|
2010-11-09 08:25:47 +03:00
|
|
|
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
|
|
|
|
|
2011-02-11 04:54:29 +03:00
|
|
|
uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
|
2010-04-22 06:35:46 +04:00
|
|
|
assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
|
2010-04-20 00:44:47 +04:00
|
|
|
"Type size mismatch!");
|
|
|
|
|
2010-11-09 08:25:47 +03:00
|
|
|
if (BaseTy) {
|
2011-02-08 05:02:47 +03:00
|
|
|
CharUnits NonVirtualSize = Layout.getNonVirtualSize();
|
|
|
|
CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
|
|
|
|
CharUnits AlignedNonVirtualTypeSize =
|
|
|
|
NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
|
|
|
|
|
|
|
|
uint64_t AlignedNonVirtualTypeSizeInBits =
|
2011-02-11 04:54:29 +03:00
|
|
|
getContext().toBits(AlignedNonVirtualTypeSize);
|
2010-11-09 08:25:47 +03:00
|
|
|
|
|
|
|
assert(AlignedNonVirtualTypeSizeInBits ==
|
|
|
|
getTargetData().getTypeAllocSizeInBits(BaseTy) &&
|
|
|
|
"Type size mismatch!");
|
|
|
|
}
|
|
|
|
|
2010-04-21 23:10:49 +04:00
|
|
|
// Verify that the LLVM and AST field offsets agree.
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::StructType *ST =
|
2010-04-21 23:10:49 +04:00
|
|
|
dyn_cast<llvm::StructType>(RL->getLLVMType());
|
|
|
|
const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
|
|
|
|
|
|
|
|
const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
|
|
|
|
RecordDecl::field_iterator it = D->field_begin();
|
2011-04-27 03:52:16 +04:00
|
|
|
const FieldDecl *LastFD = 0;
|
|
|
|
bool IsMsStruct = D->hasAttr<MsStructAttr>();
|
2010-04-21 23:10:49 +04:00
|
|
|
for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
|
|
|
|
const FieldDecl *FD = *it;
|
2010-04-22 06:35:46 +04:00
|
|
|
|
|
|
|
// For non-bit-fields, just check that the LLVM struct offset matches the
|
|
|
|
// AST offset.
|
|
|
|
if (!FD->isBitField()) {
|
2010-04-21 23:10:49 +04:00
|
|
|
unsigned FieldNo = RL->getLLVMFieldNo(FD);
|
|
|
|
assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
|
|
|
|
"Invalid field offset!");
|
2011-04-27 03:52:16 +04:00
|
|
|
LastFD = FD;
|
2010-04-22 06:35:46 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:52:16 +04:00
|
|
|
if (IsMsStruct) {
|
|
|
|
// Zero-length bitfields following non-bitfield members are
|
|
|
|
// ignored:
|
2011-05-04 00:21:04 +04:00
|
|
|
if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
|
2011-04-27 03:52:16 +04:00
|
|
|
--i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LastFD = FD;
|
|
|
|
}
|
|
|
|
|
2010-04-22 06:35:46 +04:00
|
|
|
// Ignore unnamed bit-fields.
|
2011-04-27 03:52:16 +04:00
|
|
|
if (!FD->getDeclName()) {
|
|
|
|
LastFD = FD;
|
2010-04-22 06:35:46 +04:00
|
|
|
continue;
|
2011-04-27 03:52:16 +04:00
|
|
|
}
|
|
|
|
|
2010-04-22 06:35:46 +04:00
|
|
|
const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
|
|
|
|
for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
|
|
|
|
const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
|
|
|
|
|
|
|
|
// Verify that every component access is within the structure.
|
|
|
|
uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
|
2011-02-26 11:41:59 +03:00
|
|
|
uint64_t AccessBitOffset = FieldOffset +
|
2011-04-24 14:04:59 +04:00
|
|
|
getContext().toBits(AI.FieldByteOffset);
|
2010-04-22 06:35:46 +04:00
|
|
|
assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
|
|
|
|
"Invalid bit-field access (out of range)!");
|
2010-04-21 23:10:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-04-20 00:44:47 +04:00
|
|
|
|
2010-03-31 05:09:11 +04:00
|
|
|
return RL;
|
2009-07-23 07:17:50 +04:00
|
|
|
}
|
2010-04-12 22:14:18 +04:00
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
void CGRecordLayout::print(raw_ostream &OS) const {
|
2010-04-12 22:14:18 +04:00
|
|
|
OS << "<CGRecordLayout\n";
|
2011-02-15 09:40:56 +03:00
|
|
|
OS << " LLVMType:" << *CompleteObjectType << "\n";
|
|
|
|
if (BaseSubobjectType)
|
|
|
|
OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
|
2010-08-23 01:01:12 +04:00
|
|
|
OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
|
2010-04-12 22:14:18 +04:00
|
|
|
OS << " BitFields:[\n";
|
2010-04-22 06:35:36 +04:00
|
|
|
|
|
|
|
// Print bit-field infos in declaration order.
|
|
|
|
std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
|
2010-04-12 22:14:18 +04:00
|
|
|
for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
|
|
|
|
it = BitFields.begin(), ie = BitFields.end();
|
|
|
|
it != ie; ++it) {
|
2010-04-22 06:35:36 +04:00
|
|
|
const RecordDecl *RD = it->first->getParent();
|
|
|
|
unsigned Index = 0;
|
|
|
|
for (RecordDecl::field_iterator
|
|
|
|
it2 = RD->field_begin(); *it2 != it->first; ++it2)
|
|
|
|
++Index;
|
|
|
|
BFIs.push_back(std::make_pair(Index, &it->second));
|
|
|
|
}
|
|
|
|
llvm::array_pod_sort(BFIs.begin(), BFIs.end());
|
|
|
|
for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
|
2010-04-14 00:58:55 +04:00
|
|
|
OS.indent(4);
|
2010-04-22 06:35:36 +04:00
|
|
|
BFIs[i].second->print(OS);
|
2010-04-12 22:14:18 +04:00
|
|
|
OS << "\n";
|
|
|
|
}
|
2010-04-22 06:35:36 +04:00
|
|
|
|
2010-04-12 22:14:18 +04:00
|
|
|
OS << "]>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGRecordLayout::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
void CGBitFieldInfo::print(raw_ostream &OS) const {
|
2010-04-12 22:14:18 +04:00
|
|
|
OS << "<CGBitFieldInfo";
|
|
|
|
OS << " Size:" << Size;
|
2010-04-14 00:58:55 +04:00
|
|
|
OS << " IsSigned:" << IsSigned << "\n";
|
|
|
|
|
|
|
|
OS.indent(4 + strlen("<CGBitFieldInfo"));
|
|
|
|
OS << " NumComponents:" << getNumComponents();
|
|
|
|
OS << " Components: [";
|
|
|
|
if (getNumComponents()) {
|
|
|
|
OS << "\n";
|
|
|
|
for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
|
|
|
|
const AccessInfo &AI = getComponent(i);
|
|
|
|
OS.indent(8);
|
|
|
|
OS << "<AccessInfo"
|
|
|
|
<< " FieldIndex:" << AI.FieldIndex
|
2011-04-24 14:04:59 +04:00
|
|
|
<< " FieldByteOffset:" << AI.FieldByteOffset.getQuantity()
|
2010-04-14 00:58:55 +04:00
|
|
|
<< " FieldBitStart:" << AI.FieldBitStart
|
|
|
|
<< " AccessWidth:" << AI.AccessWidth << "\n";
|
|
|
|
OS.indent(8 + strlen("<AccessInfo"));
|
2011-04-24 14:13:17 +04:00
|
|
|
OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity()
|
2010-04-14 00:58:55 +04:00
|
|
|
<< " TargetBitOffset:" << AI.TargetBitOffset
|
|
|
|
<< " TargetBitWidth:" << AI.TargetBitWidth
|
|
|
|
<< ">\n";
|
|
|
|
}
|
|
|
|
OS.indent(4);
|
|
|
|
}
|
|
|
|
OS << "]>";
|
2010-04-12 22:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGBitFieldInfo::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
}
|