Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 1 | //===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===// |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 10 | // Builder implementation for CGRecordLayout objects. |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 14 | #include "CGRecordLayout.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Attr.h" |
Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/RecordLayout.h" |
| 21 | #include "CodeGenTypes.h" |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 22 | #include "CGCXXABI.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 23 | #include "llvm/DerivedTypes.h" |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 24 | #include "llvm/Type.h" |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
John McCall | bcd3821 | 2010-11-30 23:17:27 +0000 | [diff] [blame] | 31 | namespace { |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 32 | |
| 33 | class CGRecordLayoutBuilder { |
| 34 | public: |
| 35 | /// FieldTypes - Holds the LLVM types that the struct is created from. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 36 | /// |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 37 | std::vector<const llvm::Type *> FieldTypes; |
| 38 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 39 | /// BaseSubobjectType - Holds the LLVM type for the non-virtual part |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 40 | /// of the struct. For example, consider: |
| 41 | /// |
| 42 | /// struct A { int i; }; |
| 43 | /// struct B { void *v; }; |
| 44 | /// struct C : virtual A, B { }; |
| 45 | /// |
| 46 | /// The LLVM type of C will be |
| 47 | /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B } |
| 48 | /// |
| 49 | /// And the LLVM type of the non-virtual base struct will be |
| 50 | /// %struct.C.base = type { i32 (...)**, %struct.A, i32 } |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 51 | /// |
| 52 | /// This only gets initialized if the base subobject type is |
| 53 | /// different from the complete-object type. |
| 54 | const llvm::StructType *BaseSubobjectType; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 55 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 56 | /// FieldInfo - Holds a field and its corresponding LLVM field number. |
| 57 | llvm::DenseMap<const FieldDecl *, unsigned> Fields; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 58 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 59 | /// BitFieldInfo - Holds location and size information about a bit field. |
| 60 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 61 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 62 | llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases; |
| 63 | llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases; |
Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 64 | |
| 65 | /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are |
| 66 | /// primary base classes for some other direct or indirect base class. |
| 67 | CXXIndirectPrimaryBaseSet IndirectPrimaryBases; |
| 68 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 69 | /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid |
| 70 | /// avoid laying out virtual bases more than once. |
| 71 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases; |
| 72 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 73 | /// IsZeroInitializable - Whether this struct can be C++ |
| 74 | /// zero-initialized with an LLVM zeroinitializer. |
| 75 | bool IsZeroInitializable; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 76 | bool IsZeroInitializableAsBase; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 77 | |
| 78 | /// Packed - Whether the resulting LLVM struct will be packed or not. |
| 79 | bool Packed; |
| 80 | |
| 81 | private: |
| 82 | CodeGenTypes &Types; |
| 83 | |
| 84 | /// Alignment - Contains the alignment of the RecordDecl. |
| 85 | // |
| 86 | // FIXME: This is not needed and should be removed. |
| 87 | unsigned Alignment; |
| 88 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 89 | /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field, |
| 90 | /// this will have the number of bits still available in the field. |
| 91 | char BitsAvailableInLastField; |
| 92 | |
| 93 | /// NextFieldOffsetInBytes - Holds the next field offset in bytes. |
| 94 | uint64_t NextFieldOffsetInBytes; |
| 95 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 96 | /// LayoutUnionField - Will layout a field in an union and return the type |
| 97 | /// that the field will have. |
| 98 | const llvm::Type *LayoutUnionField(const FieldDecl *Field, |
| 99 | const ASTRecordLayout &Layout); |
| 100 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 101 | /// LayoutUnion - Will layout a union RecordDecl. |
| 102 | void LayoutUnion(const RecordDecl *D); |
| 103 | |
| 104 | /// LayoutField - try to layout all fields in the record decl. |
| 105 | /// Returns false if the operation failed because the struct is not packed. |
| 106 | bool LayoutFields(const RecordDecl *D); |
| 107 | |
Anders Carlsson | a518b2a | 2010-12-04 23:59:48 +0000 | [diff] [blame] | 108 | /// Layout a single base, virtual or non-virtual |
| 109 | void LayoutBase(const CXXRecordDecl *BaseDecl, uint64_t BaseOffset); |
| 110 | |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 111 | /// LayoutVirtualBase - layout a single virtual base. |
| 112 | void LayoutVirtualBase(const CXXRecordDecl *BaseDecl, uint64_t BaseOffset); |
| 113 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 114 | /// LayoutVirtualBases - layout the virtual bases of a record decl. |
| 115 | void LayoutVirtualBases(const CXXRecordDecl *RD, |
| 116 | const ASTRecordLayout &Layout); |
| 117 | |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 118 | /// LayoutNonVirtualBase - layout a single non-virtual base. |
| 119 | void LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl, |
| 120 | uint64_t BaseOffset); |
| 121 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 122 | /// LayoutNonVirtualBases - layout the virtual bases of a record decl. |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 123 | void LayoutNonVirtualBases(const CXXRecordDecl *RD, |
| 124 | const ASTRecordLayout &Layout); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 125 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 126 | /// ComputeNonVirtualBaseType - Compute the non-virtual base field types. |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 127 | bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 128 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 129 | /// LayoutField - layout a single field. Returns false if the operation failed |
| 130 | /// because the current struct is not packed. |
| 131 | bool LayoutField(const FieldDecl *D, uint64_t FieldOffset); |
| 132 | |
| 133 | /// LayoutBitField - layout a single bit field. |
| 134 | void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset); |
| 135 | |
| 136 | /// AppendField - Appends a field with the given offset and type. |
| 137 | void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy); |
| 138 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 139 | /// AppendPadding - Appends enough padding bytes so that the total |
| 140 | /// struct size is a multiple of the field alignment. |
Anders Carlsson | d74cad8 | 2010-12-04 23:53:18 +0000 | [diff] [blame] | 141 | void AppendPadding(uint64_t FieldOffsetInBytes, |
| 142 | unsigned FieldAlignmentInBytes); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 143 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 144 | /// getByteArrayType - Returns a byte array type with the given number of |
| 145 | /// elements. |
| 146 | const llvm::Type *getByteArrayType(uint64_t NumBytes); |
| 147 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 148 | /// AppendBytes - Append a given number of bytes to the record. |
| 149 | void AppendBytes(uint64_t NumBytes); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 150 | void AppendBytes(CharUnits numBytes) { AppendBytes(numBytes.getQuantity()); } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 151 | |
| 152 | /// AppendTailPadding - Append enough tail padding so that the type will have |
| 153 | /// the passed size. |
| 154 | void AppendTailPadding(uint64_t RecordSize); |
| 155 | |
| 156 | unsigned getTypeAlignment(const llvm::Type *Ty) const; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 157 | |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 158 | /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the |
| 159 | /// LLVM element types. |
| 160 | unsigned getAlignmentAsLLVMStruct() const; |
| 161 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 162 | /// CheckZeroInitializable - Check if the given type contains a pointer |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 163 | /// to data member. |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 164 | void CheckZeroInitializable(QualType T); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 165 | |
| 166 | public: |
| 167 | CGRecordLayoutBuilder(CodeGenTypes &Types) |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 168 | : BaseSubobjectType(0), |
| 169 | IsZeroInitializable(true), IsZeroInitializableAsBase(true), |
| 170 | Packed(false), Types(Types), Alignment(0), |
| 171 | BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 172 | |
| 173 | /// Layout - Will layout a RecordDecl. |
| 174 | void Layout(const RecordDecl *D); |
| 175 | }; |
| 176 | |
| 177 | } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 178 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 179 | void CGRecordLayoutBuilder::Layout(const RecordDecl *D) { |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 180 | Alignment = |
| 181 | Types.getContext().getASTRecordLayout(D).getAlignment().getQuantity(); |
Anders Carlsson | 09a3774 | 2009-09-02 17:51:33 +0000 | [diff] [blame] | 182 | Packed = D->hasAttr<PackedAttr>(); |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 183 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 184 | if (D->isUnion()) { |
| 185 | LayoutUnion(D); |
| 186 | return; |
| 187 | } |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 188 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 189 | if (LayoutFields(D)) |
| 190 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 192 | // We weren't able to layout the struct. Try again with a packed struct |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 193 | Packed = true; |
Anders Carlsson | d5d6413 | 2009-07-28 17:56:36 +0000 | [diff] [blame] | 194 | NextFieldOffsetInBytes = 0; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 195 | FieldTypes.clear(); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 196 | Fields.clear(); |
| 197 | BitFields.clear(); |
| 198 | NonVirtualBases.clear(); |
| 199 | VirtualBases.clear(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 201 | LayoutFields(D); |
| 202 | } |
| 203 | |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 204 | CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types, |
| 205 | const FieldDecl *FD, |
| 206 | uint64_t FieldOffset, |
| 207 | uint64_t FieldSize, |
| 208 | uint64_t ContainingTypeSizeInBits, |
| 209 | unsigned ContainingTypeAlign) { |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 210 | const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType()); |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 211 | uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty); |
| 212 | uint64_t TypeSizeInBits = TypeSizeInBytes * 8; |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 213 | |
| 214 | bool IsSigned = FD->getType()->isSignedIntegerType(); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 215 | |
Anders Carlsson | be6f318 | 2010-04-16 16:23:02 +0000 | [diff] [blame] | 216 | if (FieldSize > TypeSizeInBits) { |
Anders Carlsson | d5f27b0 | 2010-04-17 22:54:57 +0000 | [diff] [blame] | 217 | // We have a wide bit-field. The extra bits are only used for padding, so |
| 218 | // if we have a bitfield of type T, with size N: |
| 219 | // |
| 220 | // T t : N; |
| 221 | // |
| 222 | // We can just assume that it's: |
| 223 | // |
| 224 | // T t : sizeof(T); |
| 225 | // |
| 226 | FieldSize = TypeSizeInBits; |
Anders Carlsson | be6f318 | 2010-04-16 16:23:02 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 229 | // Compute the access components. The policy we use is to start by attempting |
| 230 | // to access using the width of the bit-field type itself and to always access |
| 231 | // at aligned indices of that type. If such an access would fail because it |
| 232 | // extends past the bound of the type, then we reduce size to the next smaller |
| 233 | // power of two and retry. The current algorithm assumes pow2 sized types, |
| 234 | // although this is easy to fix. |
| 235 | // |
| 236 | // FIXME: This algorithm is wrong on big-endian systems, I think. |
| 237 | assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!"); |
| 238 | CGBitFieldInfo::AccessInfo Components[3]; |
| 239 | unsigned NumComponents = 0; |
| 240 | unsigned AccessedTargetBits = 0; // The tumber of target bits accessed. |
| 241 | unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt. |
Anders Carlsson | be6f318 | 2010-04-16 16:23:02 +0000 | [diff] [blame] | 242 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 243 | // Round down from the field offset to find the first access position that is |
| 244 | // at an aligned offset of the initial access type. |
Daniel Dunbar | 5981377 | 2010-04-22 15:22:33 +0000 | [diff] [blame] | 245 | uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth); |
| 246 | |
| 247 | // Adjust initial access size to fit within record. |
| 248 | while (AccessWidth > 8 && |
| 249 | AccessStart + AccessWidth > ContainingTypeSizeInBits) { |
| 250 | AccessWidth >>= 1; |
| 251 | AccessStart = FieldOffset - (FieldOffset % AccessWidth); |
| 252 | } |
Daniel Dunbar | 9c78d63 | 2010-04-15 05:09:32 +0000 | [diff] [blame] | 253 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 254 | while (AccessedTargetBits < FieldSize) { |
| 255 | // Check that we can access using a type of this size, without reading off |
| 256 | // the end of the structure. This can occur with packed structures and |
| 257 | // -fno-bitfield-type-align, for example. |
| 258 | if (AccessStart + AccessWidth > ContainingTypeSizeInBits) { |
| 259 | // If so, reduce access size to the next smaller power-of-two and retry. |
| 260 | AccessWidth >>= 1; |
| 261 | assert(AccessWidth >= 8 && "Cannot access under byte size!"); |
| 262 | continue; |
| 263 | } |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 264 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 265 | // Otherwise, add an access component. |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 266 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 267 | // First, compute the bits inside this access which are part of the |
| 268 | // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the |
| 269 | // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits |
| 270 | // in the target that we are reading. |
Daniel Dunbar | 5981377 | 2010-04-22 15:22:33 +0000 | [diff] [blame] | 271 | assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!"); |
| 272 | assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!"); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 273 | uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset); |
| 274 | uint64_t AccessBitsInFieldSize = |
Daniel Dunbar | 5981377 | 2010-04-22 15:22:33 +0000 | [diff] [blame] | 275 | std::min(AccessWidth + AccessStart, |
| 276 | FieldOffset + FieldSize) - AccessBitsInFieldStart; |
Daniel Dunbar | 5d6c07e | 2010-04-22 14:56:10 +0000 | [diff] [blame] | 277 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 278 | assert(NumComponents < 3 && "Unexpected number of components!"); |
| 279 | CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++]; |
| 280 | AI.FieldIndex = 0; |
| 281 | // FIXME: We still follow the old access pattern of only using the field |
| 282 | // byte offset. We should switch this once we fix the struct layout to be |
| 283 | // pretty. |
| 284 | AI.FieldByteOffset = AccessStart / 8; |
| 285 | AI.FieldBitStart = AccessBitsInFieldStart - AccessStart; |
| 286 | AI.AccessWidth = AccessWidth; |
Daniel Dunbar | fc66e0e | 2010-04-22 03:17:04 +0000 | [diff] [blame] | 287 | AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8; |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 288 | AI.TargetBitOffset = AccessedTargetBits; |
| 289 | AI.TargetBitWidth = AccessBitsInFieldSize; |
| 290 | |
| 291 | AccessStart += AccessWidth; |
| 292 | AccessedTargetBits += AI.TargetBitWidth; |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 295 | assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!"); |
Daniel Dunbar | 9c78d63 | 2010-04-15 05:09:32 +0000 | [diff] [blame] | 296 | return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 299 | CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types, |
| 300 | const FieldDecl *FD, |
| 301 | uint64_t FieldOffset, |
| 302 | uint64_t FieldSize) { |
| 303 | const RecordDecl *RD = FD->getParent(); |
| 304 | const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 305 | uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 306 | unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment()); |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 307 | |
| 308 | return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits, |
| 309 | ContainingTypeAlign); |
| 310 | } |
| 311 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 312 | void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D, |
| 313 | uint64_t FieldOffset) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | uint64_t FieldSize = |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 315 | D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 317 | if (FieldSize == 0) |
| 318 | return; |
| 319 | |
Anders Carlsson | d5d6413 | 2009-07-28 17:56:36 +0000 | [diff] [blame] | 320 | uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 321 | unsigned NumBytesToAppend; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 323 | if (FieldOffset < NextFieldOffset) { |
| 324 | assert(BitsAvailableInLastField && "Bitfield size mismatch!"); |
Anders Carlsson | d5d6413 | 2009-07-28 17:56:36 +0000 | [diff] [blame] | 325 | assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 327 | // The bitfield begins in the previous bit-field. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | NumBytesToAppend = |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 329 | llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8; |
| 330 | } else { |
| 331 | assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly"); |
| 332 | |
| 333 | // Append padding if necessary. |
Anders Carlsson | d74cad8 | 2010-12-04 23:53:18 +0000 | [diff] [blame] | 334 | AppendPadding(FieldOffset / 8, 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
| 336 | NumBytesToAppend = |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 337 | llvm::RoundUpToAlignment(FieldSize, 8) / 8; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 339 | assert(NumBytesToAppend && "No bytes to append!"); |
| 340 | } |
| 341 | |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 342 | // Add the bit field info. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 343 | BitFields.insert(std::make_pair(D, |
| 344 | CGBitFieldInfo::MakeInfo(Types, D, FieldOffset, FieldSize))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 346 | AppendBytes(NumBytesToAppend); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | BitsAvailableInLastField = |
Anders Carlsson | d5d6413 | 2009-07-28 17:56:36 +0000 | [diff] [blame] | 349 | NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D, |
| 353 | uint64_t FieldOffset) { |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 354 | // If the field is packed, then we need a packed struct. |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 355 | if (!Packed && D->hasAttr<PackedAttr>()) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 356 | return false; |
| 357 | |
| 358 | if (D->isBitField()) { |
| 359 | // We must use packed structs for unnamed bit fields since they |
| 360 | // don't affect the struct alignment. |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 361 | if (!Packed && !D->getDeclName()) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 362 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 364 | LayoutBitField(D, FieldOffset); |
| 365 | return true; |
| 366 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 368 | CheckZeroInitializable(D->getType()); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 369 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 370 | assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!"); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 371 | uint64_t FieldOffsetInBytes = FieldOffset / 8; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 373 | const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType()); |
| 374 | unsigned TypeAlignment = getTypeAlignment(Ty); |
| 375 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 376 | // If the type alignment is larger then the struct alignment, we must use |
| 377 | // a packed struct. |
| 378 | if (TypeAlignment > Alignment) { |
| 379 | assert(!Packed && "Alignment is wrong even with packed struct!"); |
| 380 | return false; |
| 381 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 383 | if (const RecordType *RT = D->getType()->getAs<RecordType>()) { |
| 384 | const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); |
Daniel Dunbar | 4013044 | 2010-05-27 01:12:46 +0000 | [diff] [blame] | 385 | if (const MaxFieldAlignmentAttr *MFAA = |
| 386 | RD->getAttr<MaxFieldAlignmentAttr>()) { |
| 387 | if (MFAA->getAlignment() != TypeAlignment * 8 && !Packed) |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 388 | return false; |
| 389 | } |
| 390 | } |
| 391 | |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 392 | // Round up the field offset to the alignment of the field type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | uint64_t AlignedNextFieldOffsetInBytes = |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 394 | llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment); |
| 395 | |
| 396 | if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) { |
| 397 | assert(!Packed && "Could not place field even with packed struct!"); |
| 398 | return false; |
| 399 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Anders Carlsson | d74cad8 | 2010-12-04 23:53:18 +0000 | [diff] [blame] | 401 | AppendPadding(FieldOffsetInBytes, TypeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 403 | // Now append the field. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 404 | Fields[D] = FieldTypes.size(); |
Anders Carlsson | 6e853bf | 2009-07-24 02:45:50 +0000 | [diff] [blame] | 405 | AppendField(FieldOffsetInBytes, Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 407 | return true; |
| 408 | } |
| 409 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 410 | const llvm::Type * |
| 411 | CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field, |
| 412 | const ASTRecordLayout &Layout) { |
| 413 | if (Field->isBitField()) { |
| 414 | uint64_t FieldSize = |
| 415 | Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue(); |
| 416 | |
| 417 | // Ignore zero sized bit fields. |
| 418 | if (FieldSize == 0) |
| 419 | return 0; |
| 420 | |
Daniel Dunbar | 20b551a | 2010-04-20 17:52:30 +0000 | [diff] [blame] | 421 | const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext()); |
| 422 | unsigned NumBytesToAppend = |
| 423 | llvm::RoundUpToAlignment(FieldSize, 8) / 8; |
Anders Carlsson | 2295f13 | 2010-04-17 21:04:52 +0000 | [diff] [blame] | 424 | |
Daniel Dunbar | 20b551a | 2010-04-20 17:52:30 +0000 | [diff] [blame] | 425 | if (NumBytesToAppend > 1) |
| 426 | FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend); |
Anders Carlsson | 2295f13 | 2010-04-17 21:04:52 +0000 | [diff] [blame] | 427 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 428 | // Add the bit field info. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 429 | BitFields.insert(std::make_pair(Field, |
| 430 | CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize))); |
Anders Carlsson | 2295f13 | 2010-04-17 21:04:52 +0000 | [diff] [blame] | 431 | return FieldTy; |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 432 | } |
Daniel Dunbar | 20b551a | 2010-04-20 17:52:30 +0000 | [diff] [blame] | 433 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 434 | // This is a regular union field. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 435 | Fields[Field] = 0; |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 436 | return Types.ConvertTypeForMemRecursive(Field->getType()); |
| 437 | } |
| 438 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 439 | void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) { |
| 440 | assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 442 | const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 444 | const llvm::Type *Ty = 0; |
| 445 | uint64_t Size = 0; |
| 446 | unsigned Align = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Anders Carlsson | b1ef991 | 2010-01-28 18:22:03 +0000 | [diff] [blame] | 448 | bool HasOnlyZeroSizedBitFields = true; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 449 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 450 | unsigned FieldNo = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 452 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | assert(Layout.getFieldOffset(FieldNo) == 0 && |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 454 | "Union field offset did not start at the beginning of record!"); |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 455 | const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout); |
Anders Carlsson | f814ee6 | 2009-07-23 04:00:39 +0000 | [diff] [blame] | 456 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 457 | if (!FieldTy) |
| 458 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Anders Carlsson | b1ef991 | 2010-01-28 18:22:03 +0000 | [diff] [blame] | 460 | HasOnlyZeroSizedBitFields = false; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 461 | |
Anders Carlsson | e2accf4 | 2009-07-23 21:52:03 +0000 | [diff] [blame] | 462 | unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy); |
| 463 | uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 465 | if (FieldAlign < Align) |
| 466 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 468 | if (FieldAlign > Align || FieldSize > Size) { |
| 469 | Ty = FieldTy; |
| 470 | Align = FieldAlign; |
| 471 | Size = FieldSize; |
| 472 | } |
| 473 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 475 | // Now add our field. |
Anders Carlsson | 0e91275 | 2009-09-03 22:56:02 +0000 | [diff] [blame] | 476 | if (Ty) { |
Anders Carlsson | 6e853bf | 2009-07-24 02:45:50 +0000 | [diff] [blame] | 477 | AppendField(0, Ty); |
Anders Carlsson | 0e91275 | 2009-09-03 22:56:02 +0000 | [diff] [blame] | 478 | |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 479 | if (getTypeAlignment(Ty) > Layout.getAlignment().getQuantity()) { |
Anders Carlsson | 0e91275 | 2009-09-03 22:56:02 +0000 | [diff] [blame] | 480 | // We need a packed struct. |
| 481 | Packed = true; |
| 482 | Align = 1; |
| 483 | } |
| 484 | } |
Fariborz Jahanian | e8e631c | 2009-11-06 20:47:40 +0000 | [diff] [blame] | 485 | if (!Align) { |
Anders Carlsson | b1ef991 | 2010-01-28 18:22:03 +0000 | [diff] [blame] | 486 | assert(HasOnlyZeroSizedBitFields && |
| 487 | "0-align record did not have all zero-sized bit-fields!"); |
Fariborz Jahanian | e8e631c | 2009-11-06 20:47:40 +0000 | [diff] [blame] | 488 | Align = 1; |
| 489 | } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 490 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 491 | // Append tail padding. |
Ken Dyck | 89d9f36 | 2011-02-10 12:36:29 +0000 | [diff] [blame] | 492 | uint64_t RecordSize = Layout.getSize().getQuantity(); |
| 493 | if (RecordSize > Size) |
| 494 | AppendPadding(RecordSize, Align); |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 495 | } |
| 496 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 497 | void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base, |
| 498 | uint64_t baseOffsetInBits) { |
| 499 | uint64_t baseOffsetInBytes = baseOffsetInBits / 8; |
| 500 | AppendPadding(baseOffsetInBytes, 1); |
Anders Carlsson | a518b2a | 2010-12-04 23:59:48 +0000 | [diff] [blame] | 501 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 502 | const ASTRecordLayout &baseASTLayout |
| 503 | = Types.getContext().getASTRecordLayout(base); |
| 504 | |
| 505 | // FIXME: use a better type than [sizeof(base) x i8]. |
| 506 | // We could use the base layout's subobject type as the actualy |
| 507 | // subobject type in the layout if its size is the nvsize of the |
| 508 | // base, or if we'd need padding out to the enclosing object anyhow. |
| 509 | AppendBytes(baseASTLayout.getNonVirtualSize()); |
| 510 | } |
| 511 | |
| 512 | void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base, |
| 513 | uint64_t baseOffsetInBits) { |
| 514 | // Ignore empty bases. |
| 515 | if (base->isEmpty()) return; |
| 516 | |
| 517 | const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base); |
| 518 | if (IsZeroInitializableAsBase) { |
| 519 | assert(IsZeroInitializable && |
| 520 | "class zero-initializable as base but not as complete object"); |
| 521 | |
| 522 | IsZeroInitializable = IsZeroInitializableAsBase = |
| 523 | baseLayout.isZeroInitializableAsBase(); |
| 524 | } |
| 525 | |
| 526 | LayoutBase(base, baseOffsetInBits); |
| 527 | NonVirtualBases[base] = (FieldTypes.size() - 1); |
Anders Carlsson | a518b2a | 2010-12-04 23:59:48 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 530 | void |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 531 | CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base, |
| 532 | uint64_t baseOffsetInBits) { |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 533 | // Ignore empty bases. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 534 | if (base->isEmpty()) return; |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 535 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 536 | const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base); |
| 537 | if (IsZeroInitializable) |
| 538 | IsZeroInitializable = baseLayout.isZeroInitializableAsBase(); |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 539 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 540 | LayoutBase(base, baseOffsetInBits); |
| 541 | VirtualBases[base] = (FieldTypes.size() - 1); |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 544 | /// LayoutVirtualBases - layout the non-virtual bases of a record decl. |
| 545 | void |
| 546 | CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, |
| 547 | const ASTRecordLayout &Layout) { |
| 548 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 549 | E = RD->bases_end(); I != E; ++I) { |
| 550 | const CXXRecordDecl *BaseDecl = |
| 551 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 552 | |
| 553 | // We only want to lay out virtual bases that aren't indirect primary bases |
| 554 | // of some other base. |
| 555 | if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) { |
| 556 | // Only lay out the base once. |
| 557 | if (!LaidOutVirtualBases.insert(BaseDecl)) |
| 558 | continue; |
| 559 | |
| 560 | uint64_t VBaseOffset = Layout.getVBaseClassOffsetInBits(BaseDecl); |
| 561 | LayoutVirtualBase(BaseDecl, VBaseOffset); |
| 562 | } |
| 563 | |
| 564 | if (!BaseDecl->getNumVBases()) { |
| 565 | // This base isn't interesting since it doesn't have any virtual bases. |
| 566 | continue; |
| 567 | } |
| 568 | |
| 569 | LayoutVirtualBases(BaseDecl, Layout); |
| 570 | } |
| 571 | } |
| 572 | |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 573 | void |
| 574 | CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD, |
| 575 | const ASTRecordLayout &Layout) { |
| 576 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 577 | |
| 578 | // Check if we need to add a vtable pointer. |
| 579 | if (RD->isDynamicClass()) { |
| 580 | if (!PrimaryBase) { |
| 581 | const llvm::Type *FunctionType = |
| 582 | llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()), |
| 583 | /*isVarArg=*/true); |
| 584 | const llvm::Type *VTableTy = FunctionType->getPointerTo(); |
| 585 | |
| 586 | assert(NextFieldOffsetInBytes == 0 && |
| 587 | "VTable pointer must come first!"); |
| 588 | AppendField(NextFieldOffsetInBytes, VTableTy->getPointerTo()); |
| 589 | } else { |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 590 | if (!Layout.isPrimaryBaseVirtual()) |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 591 | LayoutNonVirtualBase(PrimaryBase, 0); |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 592 | else |
| 593 | LayoutVirtualBase(PrimaryBase, 0); |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
| 597 | // Layout the non-virtual bases. |
| 598 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 599 | E = RD->bases_end(); I != E; ++I) { |
| 600 | if (I->isVirtual()) |
| 601 | continue; |
| 602 | |
| 603 | const CXXRecordDecl *BaseDecl = |
| 604 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 605 | |
| 606 | // We've already laid out the primary base. |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 607 | if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual()) |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 608 | continue; |
| 609 | |
Anders Carlsson | fd88a61 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 610 | LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffsetInBits(BaseDecl)); |
Anders Carlsson | d681a29 | 2009-12-16 17:27:20 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 614 | bool |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 615 | CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) { |
| 616 | const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD); |
| 617 | |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 618 | CharUnits NonVirtualSize = Layout.getNonVirtualSize(); |
| 619 | CharUnits NonVirtualAlign = Layout.getNonVirtualAlign(); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 620 | uint64_t AlignedNonVirtualTypeSize = |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 621 | NonVirtualSize.RoundUpToAlignment(NonVirtualAlign).getQuantity(); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 622 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 623 | // First check if we can use the same fields as for the complete class. |
Ken Dyck | 89d9f36 | 2011-02-10 12:36:29 +0000 | [diff] [blame] | 624 | uint64_t RecordSize = Layout.getSize().getQuantity(); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 625 | if (AlignedNonVirtualTypeSize == RecordSize) |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 626 | return true; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 627 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 628 | // Check if we need padding. |
| 629 | uint64_t AlignedNextFieldOffset = |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 630 | llvm::RoundUpToAlignment(NextFieldOffsetInBytes, |
| 631 | getAlignmentAsLLVMStruct()); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 632 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 633 | if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) { |
| 634 | assert(!Packed && "cannot layout even as packed struct"); |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 635 | return false; // Needs packing. |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 636 | } |
| 637 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 638 | bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset); |
| 639 | if (needsPadding) { |
| 640 | uint64_t NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset; |
| 641 | FieldTypes.push_back(getByteArrayType(NumBytes)); |
| 642 | } |
| 643 | |
| 644 | BaseSubobjectType = llvm::StructType::get(Types.getLLVMContext(), |
| 645 | FieldTypes, Packed); |
| 646 | |
| 647 | if (needsPadding) { |
| 648 | // Pull the padding back off. |
| 649 | FieldTypes.pop_back(); |
| 650 | } |
| 651 | |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 652 | return true; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 655 | bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
| 656 | assert(!D->isUnion() && "Can't call LayoutFields on a union!"); |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 657 | assert(Alignment && "Did not set alignment!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 659 | const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 661 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D); |
| 662 | if (RD) |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 663 | LayoutNonVirtualBases(RD, Layout); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 664 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 665 | unsigned FieldNo = 0; |
Fariborz Jahanian | 7b2b1ec | 2009-07-27 20:57:45 +0000 | [diff] [blame] | 666 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 668 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { |
| 669 | if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | assert(!Packed && |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 671 | "Could not layout fields even with a packed LLVM struct!"); |
| 672 | return false; |
| 673 | } |
| 674 | } |
| 675 | |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 676 | if (RD) { |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 677 | // We've laid out the non-virtual bases and the fields, now compute the |
| 678 | // non-virtual base field types. |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 679 | if (!ComputeNonVirtualBaseType(RD)) { |
| 680 | assert(!Packed && "Could not layout even with a packed LLVM struct!"); |
| 681 | return false; |
| 682 | } |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 683 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 684 | // And lay out the virtual bases. |
| 685 | RD->getIndirectPrimaryBases(IndirectPrimaryBases); |
| 686 | if (Layout.isPrimaryBaseVirtual()) |
| 687 | IndirectPrimaryBases.insert(Layout.getPrimaryBase()); |
| 688 | LayoutVirtualBases(RD, Layout); |
| 689 | } |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 690 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 691 | // Append tail padding if necessary. |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 692 | AppendTailPadding(Types.getContext().toBits(Layout.getSize())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 694 | return true; |
| 695 | } |
| 696 | |
Anders Carlsson | b97a3ec | 2009-07-27 14:55:54 +0000 | [diff] [blame] | 697 | void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) { |
| 698 | assert(RecordSize % 8 == 0 && "Invalid record size!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 699 | |
Anders Carlsson | b97a3ec | 2009-07-27 14:55:54 +0000 | [diff] [blame] | 700 | uint64_t RecordSizeInBytes = RecordSize / 8; |
Anders Carlsson | d5d6413 | 2009-07-28 17:56:36 +0000 | [diff] [blame] | 701 | assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 703 | uint64_t AlignedNextFieldOffset = |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 704 | llvm::RoundUpToAlignment(NextFieldOffsetInBytes, |
| 705 | getAlignmentAsLLVMStruct()); |
Anders Carlsson | 220bf4f | 2009-12-08 01:24:23 +0000 | [diff] [blame] | 706 | |
| 707 | if (AlignedNextFieldOffset == RecordSizeInBytes) { |
| 708 | // We don't need any padding. |
| 709 | return; |
| 710 | } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 711 | |
Anders Carlsson | d5d6413 | 2009-07-28 17:56:36 +0000 | [diff] [blame] | 712 | unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes; |
Anders Carlsson | b97a3ec | 2009-07-27 14:55:54 +0000 | [diff] [blame] | 713 | AppendBytes(NumPadBytes); |
| 714 | } |
| 715 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes, |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 717 | const llvm::Type *FieldTy) { |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 718 | uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy); |
Anders Carlsson | 6e853bf | 2009-07-24 02:45:50 +0000 | [diff] [blame] | 719 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 720 | FieldTypes.push_back(FieldTy); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 721 | |
Anders Carlsson | d5d6413 | 2009-07-28 17:56:36 +0000 | [diff] [blame] | 722 | NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 723 | BitsAvailableInLastField = 0; |
| 724 | } |
| 725 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes, |
Anders Carlsson | d74cad8 | 2010-12-04 23:53:18 +0000 | [diff] [blame] | 727 | unsigned FieldAlignmentInBytes) { |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 728 | assert(NextFieldOffsetInBytes <= FieldOffsetInBytes && |
| 729 | "Incorrect field layout!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 731 | // Round up the field offset to the alignment of the field type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 732 | uint64_t AlignedNextFieldOffsetInBytes = |
Anders Carlsson | d74cad8 | 2010-12-04 23:53:18 +0000 | [diff] [blame] | 733 | llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignmentInBytes); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 734 | |
| 735 | if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) { |
| 736 | // Even with alignment, the field offset is not at the right place, |
| 737 | // insert padding. |
| 738 | uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes; |
| 739 | |
| 740 | AppendBytes(PaddingInBytes); |
| 741 | } |
| 742 | } |
| 743 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 744 | const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(uint64_t NumBytes) { |
| 745 | assert(NumBytes != 0 && "Empty byte array's aren't allowed."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 747 | const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext()); |
Anders Carlsson | b97a3ec | 2009-07-27 14:55:54 +0000 | [diff] [blame] | 748 | if (NumBytes > 1) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 749 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 751 | return Ty; |
| 752 | } |
| 753 | |
| 754 | void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) { |
| 755 | if (NumBytes == 0) |
| 756 | return; |
| 757 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 758 | // Append the padding field |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 759 | AppendField(NextFieldOffsetInBytes, getByteArrayType(NumBytes)); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const { |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 763 | if (Packed) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 764 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 765 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 766 | return Types.getTargetData().getABITypeAlignment(Ty); |
| 767 | } |
| 768 | |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 769 | unsigned CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const { |
| 770 | if (Packed) |
| 771 | return 1; |
| 772 | |
| 773 | unsigned MaxAlignment = 1; |
| 774 | for (size_t i = 0; i != FieldTypes.size(); ++i) |
| 775 | MaxAlignment = std::max(MaxAlignment, getTypeAlignment(FieldTypes[i])); |
| 776 | |
| 777 | return MaxAlignment; |
| 778 | } |
| 779 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 780 | /// Merge in whether a field of the given type is zero-initializable. |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 781 | void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) { |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 782 | // This record already contains a member pointer. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 783 | if (!IsZeroInitializableAsBase) |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 784 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 786 | // Can only have member pointers if we're compiling C++. |
| 787 | if (!Types.getContext().getLangOptions().CPlusPlus) |
| 788 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 789 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 790 | const Type *elementType = T->getBaseElementTypeUnsafe(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 791 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 792 | if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) { |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 793 | if (!Types.getCXXABI().isZeroInitializable(MPT)) |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 794 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
| 795 | } else if (const RecordType *RT = elementType->getAs<RecordType>()) { |
Anders Carlsson | e8bfe41 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 796 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 797 | const CGRecordLayout &Layout = Types.getCGRecordLayout(RD); |
| 798 | if (!Layout.isZeroInitializable()) |
| 799 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 800 | } |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 803 | CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) { |
| 804 | CGRecordLayoutBuilder Builder(*this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 806 | Builder.Layout(D); |
Anders Carlsson | e1d5ca5 | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 807 | |
Anders Carlsson | 36e2fa8 | 2010-11-24 19:37:16 +0000 | [diff] [blame] | 808 | const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(), |
| 809 | Builder.FieldTypes, |
| 810 | Builder.Packed); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 812 | // If we're in C++, compute the base subobject type. |
Anders Carlsson | 36e2fa8 | 2010-11-24 19:37:16 +0000 | [diff] [blame] | 813 | const llvm::StructType *BaseTy = 0; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 814 | if (isa<CXXRecordDecl>(D)) { |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 815 | BaseTy = Builder.BaseSubobjectType; |
| 816 | if (!BaseTy) BaseTy = Ty; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 819 | CGRecordLayout *RL = |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 820 | new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable, |
| 821 | Builder.IsZeroInitializableAsBase); |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 822 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 823 | RL->NonVirtualBases.swap(Builder.NonVirtualBases); |
| 824 | RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases); |
Anders Carlsson | 061ca52 | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 825 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 826 | // Add all the field numbers. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 827 | RL->FieldInfo.swap(Builder.Fields); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 828 | |
| 829 | // Add bitfield info. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 830 | RL->BitFields.swap(Builder.BitFields); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 832 | // Dump the layout, if requested. |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 833 | if (getContext().getLangOptions().DumpRecordLayouts) { |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 834 | llvm::errs() << "\n*** Dumping IRgen Record Layout\n"; |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 835 | llvm::errs() << "Record: "; |
| 836 | D->dump(); |
| 837 | llvm::errs() << "\nLayout: "; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 838 | RL->dump(); |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 839 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 840 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 841 | #ifndef NDEBUG |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 842 | // Verify that the computed LLVM struct size matches the AST layout size. |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 843 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D); |
| 844 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 845 | uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize()); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 846 | assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) && |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 847 | "Type size mismatch!"); |
| 848 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 849 | if (BaseTy) { |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 850 | CharUnits NonVirtualSize = Layout.getNonVirtualSize(); |
| 851 | CharUnits NonVirtualAlign = Layout.getNonVirtualAlign(); |
| 852 | CharUnits AlignedNonVirtualTypeSize = |
| 853 | NonVirtualSize.RoundUpToAlignment(NonVirtualAlign); |
| 854 | |
| 855 | uint64_t AlignedNonVirtualTypeSizeInBits = |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 856 | getContext().toBits(AlignedNonVirtualTypeSize); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 857 | |
| 858 | assert(AlignedNonVirtualTypeSizeInBits == |
| 859 | getTargetData().getTypeAllocSizeInBits(BaseTy) && |
| 860 | "Type size mismatch!"); |
| 861 | } |
| 862 | |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 863 | // Verify that the LLVM and AST field offsets agree. |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 864 | const llvm::StructType *ST = |
| 865 | dyn_cast<llvm::StructType>(RL->getLLVMType()); |
| 866 | const llvm::StructLayout *SL = getTargetData().getStructLayout(ST); |
| 867 | |
| 868 | const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D); |
| 869 | RecordDecl::field_iterator it = D->field_begin(); |
| 870 | for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) { |
| 871 | const FieldDecl *FD = *it; |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 872 | |
| 873 | // For non-bit-fields, just check that the LLVM struct offset matches the |
| 874 | // AST offset. |
| 875 | if (!FD->isBitField()) { |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 876 | unsigned FieldNo = RL->getLLVMFieldNo(FD); |
| 877 | assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) && |
| 878 | "Invalid field offset!"); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 879 | continue; |
| 880 | } |
| 881 | |
| 882 | // Ignore unnamed bit-fields. |
| 883 | if (!FD->getDeclName()) |
| 884 | continue; |
| 885 | |
| 886 | const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD); |
| 887 | for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) { |
| 888 | const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i); |
| 889 | |
| 890 | // Verify that every component access is within the structure. |
| 891 | uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex); |
| 892 | uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8; |
| 893 | assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits && |
| 894 | "Invalid bit-field access (out of range)!"); |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | #endif |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 898 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 899 | return RL; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 900 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 901 | |
| 902 | void CGRecordLayout::print(llvm::raw_ostream &OS) const { |
| 903 | OS << "<CGRecordLayout\n"; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 904 | OS << " LLVMType:" << *CompleteObjectType << "\n"; |
| 905 | if (BaseSubobjectType) |
| 906 | OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n"; |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 907 | OS << " IsZeroInitializable:" << IsZeroInitializable << "\n"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 908 | OS << " BitFields:[\n"; |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 909 | |
| 910 | // Print bit-field infos in declaration order. |
| 911 | std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 912 | for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator |
| 913 | it = BitFields.begin(), ie = BitFields.end(); |
| 914 | it != ie; ++it) { |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 915 | const RecordDecl *RD = it->first->getParent(); |
| 916 | unsigned Index = 0; |
| 917 | for (RecordDecl::field_iterator |
| 918 | it2 = RD->field_begin(); *it2 != it->first; ++it2) |
| 919 | ++Index; |
| 920 | BFIs.push_back(std::make_pair(Index, &it->second)); |
| 921 | } |
| 922 | llvm::array_pod_sort(BFIs.begin(), BFIs.end()); |
| 923 | for (unsigned i = 0, e = BFIs.size(); i != e; ++i) { |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 924 | OS.indent(4); |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 925 | BFIs[i].second->print(OS); |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 926 | OS << "\n"; |
| 927 | } |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 928 | |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 929 | OS << "]>\n"; |
| 930 | } |
| 931 | |
| 932 | void CGRecordLayout::dump() const { |
| 933 | print(llvm::errs()); |
| 934 | } |
| 935 | |
| 936 | void CGBitFieldInfo::print(llvm::raw_ostream &OS) const { |
| 937 | OS << "<CGBitFieldInfo"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 938 | OS << " Size:" << Size; |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 939 | OS << " IsSigned:" << IsSigned << "\n"; |
| 940 | |
| 941 | OS.indent(4 + strlen("<CGBitFieldInfo")); |
| 942 | OS << " NumComponents:" << getNumComponents(); |
| 943 | OS << " Components: ["; |
| 944 | if (getNumComponents()) { |
| 945 | OS << "\n"; |
| 946 | for (unsigned i = 0, e = getNumComponents(); i != e; ++i) { |
| 947 | const AccessInfo &AI = getComponent(i); |
| 948 | OS.indent(8); |
| 949 | OS << "<AccessInfo" |
| 950 | << " FieldIndex:" << AI.FieldIndex |
| 951 | << " FieldByteOffset:" << AI.FieldByteOffset |
| 952 | << " FieldBitStart:" << AI.FieldBitStart |
| 953 | << " AccessWidth:" << AI.AccessWidth << "\n"; |
| 954 | OS.indent(8 + strlen("<AccessInfo")); |
| 955 | OS << " AccessAlignment:" << AI.AccessAlignment |
| 956 | << " TargetBitOffset:" << AI.TargetBitOffset |
| 957 | << " TargetBitWidth:" << AI.TargetBitWidth |
| 958 | << ">\n"; |
| 959 | } |
| 960 | OS.indent(4); |
| 961 | } |
| 962 | OS << "]>"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | void CGBitFieldInfo::dump() const { |
| 966 | print(llvm::errs()); |
| 967 | } |