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