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