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