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