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