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