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