Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 1 | //===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===// |
Anders Carlsson | 307846f | 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 | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 10 | // Builder implementation for CGRecordLayout objects. |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 14 | #include "CGRecordLayout.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Attr.h" |
Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/RecordLayout.h" |
| 21 | #include "CodeGenTypes.h" |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 22 | #include "CGCXXABI.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 23 | #include "llvm/DerivedTypes.h" |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 24 | #include "llvm/Type.h" |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
John McCall | bcd3821 | 2010-11-30 23:17:27 +0000 | [diff] [blame] | 31 | namespace { |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 32 | |
| 33 | class CGRecordLayoutBuilder { |
| 34 | public: |
| 35 | /// FieldTypes - Holds the LLVM types that the struct is created from. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 36 | /// |
Anders Carlsson | b6d31e7 | 2011-04-17 21:32:41 +0000 | [diff] [blame] | 37 | llvm::SmallVector<const llvm::Type *, 16> FieldTypes; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 38 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 39 | /// BaseSubobjectType - Holds the LLVM type for the non-virtual part |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 40 | /// of the struct. For example, consider: |
| 41 | /// |
| 42 | /// struct A { int i; }; |
| 43 | /// struct B { void *v; }; |
| 44 | /// struct C : virtual A, B { }; |
| 45 | /// |
| 46 | /// The LLVM type of C will be |
| 47 | /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B } |
| 48 | /// |
| 49 | /// And the LLVM type of the non-virtual base struct will be |
| 50 | /// %struct.C.base = type { i32 (...)**, %struct.A, i32 } |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 51 | /// |
| 52 | /// This only gets initialized if the base subobject type is |
| 53 | /// different from the complete-object type. |
| 54 | const llvm::StructType *BaseSubobjectType; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 55 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 56 | /// FieldInfo - Holds a field and its corresponding LLVM field number. |
| 57 | llvm::DenseMap<const FieldDecl *, unsigned> Fields; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 58 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 59 | /// BitFieldInfo - Holds location and size information about a bit field. |
| 60 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 61 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 62 | llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases; |
| 63 | llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases; |
Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 64 | |
| 65 | /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are |
| 66 | /// primary base classes for some other direct or indirect base class. |
| 67 | CXXIndirectPrimaryBaseSet IndirectPrimaryBases; |
| 68 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 69 | /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid |
| 70 | /// avoid laying out virtual bases more than once. |
| 71 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases; |
| 72 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 73 | /// IsZeroInitializable - Whether this struct can be C++ |
| 74 | /// zero-initialized with an LLVM zeroinitializer. |
| 75 | bool IsZeroInitializable; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 76 | bool IsZeroInitializableAsBase; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 77 | |
| 78 | /// Packed - Whether the resulting LLVM struct will be packed or not. |
| 79 | bool Packed; |
| 80 | |
| 81 | private: |
| 82 | CodeGenTypes &Types; |
| 83 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 84 | /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the |
| 85 | /// last base laid out. Used so that we can replace the last laid out base |
| 86 | /// type with an i8 array if needed. |
| 87 | struct LastLaidOutBaseInfo { |
| 88 | CharUnits Offset; |
| 89 | CharUnits NonVirtualSize; |
| 90 | |
| 91 | bool isValid() const { return !NonVirtualSize.isZero(); } |
| 92 | void invalidate() { NonVirtualSize = CharUnits::Zero(); } |
| 93 | |
| 94 | } LastLaidOutBase; |
| 95 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 96 | /// Alignment - Contains the alignment of the RecordDecl. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 97 | CharUnits Alignment; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 98 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 99 | /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field, |
| 100 | /// this will have the number of bits still available in the field. |
| 101 | char BitsAvailableInLastField; |
| 102 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 103 | /// NextFieldOffset - Holds the next field offset. |
| 104 | CharUnits NextFieldOffset; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 105 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 106 | /// LayoutUnionField - Will layout a field in an union and return the type |
| 107 | /// that the field will have. |
| 108 | const llvm::Type *LayoutUnionField(const FieldDecl *Field, |
| 109 | const ASTRecordLayout &Layout); |
| 110 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 111 | /// LayoutUnion - Will layout a union RecordDecl. |
| 112 | void LayoutUnion(const RecordDecl *D); |
| 113 | |
| 114 | /// LayoutField - try to layout all fields in the record decl. |
| 115 | /// Returns false if the operation failed because the struct is not packed. |
| 116 | bool LayoutFields(const RecordDecl *D); |
| 117 | |
Anders Carlsson | a518b2a | 2010-12-04 23:59:48 +0000 | [diff] [blame] | 118 | /// Layout a single base, virtual or non-virtual |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 119 | void LayoutBase(const CXXRecordDecl *base, |
| 120 | const CGRecordLayout &baseLayout, |
| 121 | CharUnits baseOffset); |
Anders Carlsson | a518b2a | 2010-12-04 23:59:48 +0000 | [diff] [blame] | 122 | |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 123 | /// LayoutVirtualBase - layout a single virtual base. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 124 | void LayoutVirtualBase(const CXXRecordDecl *base, |
| 125 | CharUnits baseOffset); |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 126 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 127 | /// LayoutVirtualBases - layout the virtual bases of a record decl. |
| 128 | void LayoutVirtualBases(const CXXRecordDecl *RD, |
| 129 | const ASTRecordLayout &Layout); |
| 130 | |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 131 | /// LayoutNonVirtualBase - layout a single non-virtual base. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 132 | void LayoutNonVirtualBase(const CXXRecordDecl *base, |
| 133 | CharUnits baseOffset); |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 134 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 135 | /// LayoutNonVirtualBases - layout the virtual bases of a record decl. |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 136 | void LayoutNonVirtualBases(const CXXRecordDecl *RD, |
| 137 | const ASTRecordLayout &Layout); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 138 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 139 | /// ComputeNonVirtualBaseType - Compute the non-virtual base field types. |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 140 | bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 141 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 142 | /// LayoutField - layout a single field. Returns false if the operation failed |
| 143 | /// because the current struct is not packed. |
| 144 | bool LayoutField(const FieldDecl *D, uint64_t FieldOffset); |
| 145 | |
| 146 | /// LayoutBitField - layout a single bit field. |
| 147 | void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset); |
| 148 | |
| 149 | /// AppendField - Appends a field with the given offset and type. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 150 | void AppendField(CharUnits fieldOffset, const llvm::Type *FieldTy); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 151 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 152 | /// AppendPadding - Appends enough padding bytes so that the total |
| 153 | /// struct size is a multiple of the field alignment. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 154 | void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 155 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 156 | /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the |
| 157 | /// tail padding of a previous base. If this happens, the type of the previous |
| 158 | /// base needs to be changed to an array of i8. Returns true if the last |
| 159 | /// laid out base was resized. |
| 160 | bool ResizeLastBaseFieldIfNecessary(CharUnits offset); |
| 161 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 162 | /// getByteArrayType - Returns a byte array type with the given number of |
| 163 | /// elements. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 164 | const llvm::Type *getByteArrayType(CharUnits NumBytes); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 165 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 166 | /// AppendBytes - Append a given number of bytes to the record. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 167 | void AppendBytes(CharUnits numBytes); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 168 | |
| 169 | /// AppendTailPadding - Append enough tail padding so that the type will have |
| 170 | /// the passed size. |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 171 | void AppendTailPadding(CharUnits RecordSize); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 172 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 173 | CharUnits getTypeAlignment(const llvm::Type *Ty) const; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 174 | |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 175 | /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the |
| 176 | /// LLVM element types. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 177 | CharUnits getAlignmentAsLLVMStruct() const; |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 178 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 179 | /// CheckZeroInitializable - Check if the given type contains a pointer |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 180 | /// to data member. |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 181 | void CheckZeroInitializable(QualType T); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 182 | |
| 183 | public: |
| 184 | CGRecordLayoutBuilder(CodeGenTypes &Types) |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 185 | : BaseSubobjectType(0), |
| 186 | IsZeroInitializable(true), IsZeroInitializableAsBase(true), |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 187 | Packed(false), Types(Types), BitsAvailableInLastField(0) { } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 188 | |
| 189 | /// Layout - Will layout a RecordDecl. |
| 190 | void Layout(const RecordDecl *D); |
| 191 | }; |
| 192 | |
| 193 | } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 194 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 195 | void CGRecordLayoutBuilder::Layout(const RecordDecl *D) { |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 196 | Alignment = Types.getContext().getASTRecordLayout(D).getAlignment(); |
Anders Carlsson | 09a3774 | 2009-09-02 17:51:33 +0000 | [diff] [blame] | 197 | Packed = D->hasAttr<PackedAttr>(); |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 198 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 199 | if (D->isUnion()) { |
| 200 | LayoutUnion(D); |
| 201 | return; |
| 202 | } |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 203 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 204 | if (LayoutFields(D)) |
| 205 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 207 | // We weren't able to layout the struct. Try again with a packed struct |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 208 | Packed = true; |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 209 | LastLaidOutBase.invalidate(); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 210 | NextFieldOffset = CharUnits::Zero(); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 211 | FieldTypes.clear(); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 212 | Fields.clear(); |
| 213 | BitFields.clear(); |
| 214 | NonVirtualBases.clear(); |
| 215 | VirtualBases.clear(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 217 | LayoutFields(D); |
| 218 | } |
| 219 | |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 220 | CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types, |
| 221 | const FieldDecl *FD, |
| 222 | uint64_t FieldOffset, |
| 223 | uint64_t FieldSize, |
| 224 | uint64_t ContainingTypeSizeInBits, |
| 225 | unsigned ContainingTypeAlign) { |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 226 | const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType()); |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 227 | CharUnits TypeSizeInBytes = |
| 228 | CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(Ty)); |
| 229 | uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 230 | |
| 231 | bool IsSigned = FD->getType()->isSignedIntegerType(); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 232 | |
Anders Carlsson | be6f318 | 2010-04-16 16:23:02 +0000 | [diff] [blame] | 233 | if (FieldSize > TypeSizeInBits) { |
Anders Carlsson | d5f27b0 | 2010-04-17 22:54:57 +0000 | [diff] [blame] | 234 | // We have a wide bit-field. The extra bits are only used for padding, so |
| 235 | // if we have a bitfield of type T, with size N: |
| 236 | // |
| 237 | // T t : N; |
| 238 | // |
| 239 | // We can just assume that it's: |
| 240 | // |
| 241 | // T t : sizeof(T); |
| 242 | // |
| 243 | FieldSize = TypeSizeInBits; |
Anders Carlsson | be6f318 | 2010-04-16 16:23:02 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Chris Lattner | fb59c7c | 2011-02-17 22:09:58 +0000 | [diff] [blame] | 246 | // in big-endian machines the first fields are in higher bit positions, |
| 247 | // so revert the offset. The byte offsets are reversed(back) later. |
| 248 | if (Types.getTargetData().isBigEndian()) { |
| 249 | FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize); |
| 250 | } |
| 251 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 252 | // Compute the access components. The policy we use is to start by attempting |
| 253 | // to access using the width of the bit-field type itself and to always access |
| 254 | // at aligned indices of that type. If such an access would fail because it |
| 255 | // extends past the bound of the type, then we reduce size to the next smaller |
| 256 | // power of two and retry. The current algorithm assumes pow2 sized types, |
| 257 | // although this is easy to fix. |
| 258 | // |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 259 | assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!"); |
| 260 | CGBitFieldInfo::AccessInfo Components[3]; |
| 261 | unsigned NumComponents = 0; |
Nick Lewycky | d2348d8 | 2011-03-22 17:35:47 +0000 | [diff] [blame] | 262 | unsigned AccessedTargetBits = 0; // The number of target bits accessed. |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 263 | unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt. |
Anders Carlsson | be6f318 | 2010-04-16 16:23:02 +0000 | [diff] [blame] | 264 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 265 | // Round down from the field offset to find the first access position that is |
| 266 | // at an aligned offset of the initial access type. |
Daniel Dunbar | 5981377 | 2010-04-22 15:22:33 +0000 | [diff] [blame] | 267 | uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth); |
| 268 | |
| 269 | // Adjust initial access size to fit within record. |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 270 | while (AccessWidth > Types.getTarget().getCharWidth() && |
Daniel Dunbar | 5981377 | 2010-04-22 15:22:33 +0000 | [diff] [blame] | 271 | AccessStart + AccessWidth > ContainingTypeSizeInBits) { |
| 272 | AccessWidth >>= 1; |
| 273 | AccessStart = FieldOffset - (FieldOffset % AccessWidth); |
| 274 | } |
Daniel Dunbar | 9c78d63 | 2010-04-15 05:09:32 +0000 | [diff] [blame] | 275 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 276 | while (AccessedTargetBits < FieldSize) { |
| 277 | // Check that we can access using a type of this size, without reading off |
| 278 | // the end of the structure. This can occur with packed structures and |
| 279 | // -fno-bitfield-type-align, for example. |
| 280 | if (AccessStart + AccessWidth > ContainingTypeSizeInBits) { |
| 281 | // If so, reduce access size to the next smaller power-of-two and retry. |
| 282 | AccessWidth >>= 1; |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 283 | assert(AccessWidth >= Types.getTarget().getCharWidth() |
| 284 | && "Cannot access under byte size!"); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 285 | continue; |
| 286 | } |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 287 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 288 | // Otherwise, add an access component. |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 289 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 290 | // First, compute the bits inside this access which are part of the |
| 291 | // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the |
| 292 | // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits |
| 293 | // in the target that we are reading. |
Daniel Dunbar | 5981377 | 2010-04-22 15:22:33 +0000 | [diff] [blame] | 294 | assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!"); |
| 295 | assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!"); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 296 | uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset); |
| 297 | uint64_t AccessBitsInFieldSize = |
Daniel Dunbar | 5981377 | 2010-04-22 15:22:33 +0000 | [diff] [blame] | 298 | std::min(AccessWidth + AccessStart, |
| 299 | FieldOffset + FieldSize) - AccessBitsInFieldStart; |
Daniel Dunbar | 5d6c07e | 2010-04-22 14:56:10 +0000 | [diff] [blame] | 300 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 301 | assert(NumComponents < 3 && "Unexpected number of components!"); |
| 302 | CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++]; |
| 303 | AI.FieldIndex = 0; |
| 304 | // FIXME: We still follow the old access pattern of only using the field |
| 305 | // byte offset. We should switch this once we fix the struct layout to be |
| 306 | // pretty. |
Chris Lattner | fb59c7c | 2011-02-17 22:09:58 +0000 | [diff] [blame] | 307 | |
| 308 | // on big-endian machines we reverted the bit offset because first fields are |
| 309 | // in higher bits. But this also reverts the bytes, so fix this here by reverting |
| 310 | // the byte offset on big-endian machines. |
| 311 | if (Types.getTargetData().isBigEndian()) { |
Ken Dyck | f76759c | 2011-04-24 10:04:59 +0000 | [diff] [blame] | 312 | AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits( |
| 313 | ContainingTypeSizeInBits - AccessStart - AccessWidth); |
Chris Lattner | fb59c7c | 2011-02-17 22:09:58 +0000 | [diff] [blame] | 314 | } else { |
Ken Dyck | f76759c | 2011-04-24 10:04:59 +0000 | [diff] [blame] | 315 | AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart); |
Chris Lattner | fb59c7c | 2011-02-17 22:09:58 +0000 | [diff] [blame] | 316 | } |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 317 | AI.FieldBitStart = AccessBitsInFieldStart - AccessStart; |
| 318 | AI.AccessWidth = AccessWidth; |
Ken Dyck | 27337a8 | 2011-04-24 10:13:17 +0000 | [diff] [blame] | 319 | AI.AccessAlignment = Types.getContext().toCharUnitsFromBits( |
| 320 | llvm::MinAlign(ContainingTypeAlign, AccessStart)); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 321 | AI.TargetBitOffset = AccessedTargetBits; |
| 322 | AI.TargetBitWidth = AccessBitsInFieldSize; |
| 323 | |
| 324 | AccessStart += AccessWidth; |
| 325 | AccessedTargetBits += AI.TargetBitWidth; |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 328 | assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!"); |
Daniel Dunbar | 9c78d63 | 2010-04-15 05:09:32 +0000 | [diff] [blame] | 329 | return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 332 | CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types, |
| 333 | const FieldDecl *FD, |
| 334 | uint64_t FieldOffset, |
| 335 | uint64_t FieldSize) { |
| 336 | const RecordDecl *RD = FD->getParent(); |
| 337 | const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 338 | uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 339 | unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment()); |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 340 | |
| 341 | return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits, |
| 342 | ContainingTypeAlign); |
| 343 | } |
| 344 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 345 | void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 346 | uint64_t fieldOffset) { |
| 347 | uint64_t fieldSize = |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 348 | D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 350 | if (fieldSize == 0) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 351 | return; |
| 352 | |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 353 | uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset); |
Ken Dyck | 345a6de | 2011-04-24 16:40:29 +0000 | [diff] [blame] | 354 | CharUnits numBytesToAppend; |
| 355 | unsigned charAlign = Types.getContext().Target.getCharAlign(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 357 | if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) { |
Ken Dyck | 345a6de | 2011-04-24 16:40:29 +0000 | [diff] [blame] | 358 | assert(fieldOffset % charAlign == 0 && |
| 359 | "Field offset not aligned correctly"); |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 360 | |
| 361 | CharUnits fieldOffsetInCharUnits = |
| 362 | Types.getContext().toCharUnitsFromBits(fieldOffset); |
| 363 | |
| 364 | // Try to resize the last base field. |
| 365 | if (ResizeLastBaseFieldIfNecessary(fieldOffsetInCharUnits)) |
| 366 | nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset); |
| 367 | } |
| 368 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 369 | if (fieldOffset < nextFieldOffsetInBits) { |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 370 | assert(BitsAvailableInLastField && "Bitfield size mismatch!"); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 371 | assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 373 | // The bitfield begins in the previous bit-field. |
Ken Dyck | 345a6de | 2011-04-24 16:40:29 +0000 | [diff] [blame] | 374 | numBytesToAppend = Types.getContext().toCharUnitsFromBits( |
| 375 | llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField, |
| 376 | charAlign)); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 377 | } else { |
Ken Dyck | 345a6de | 2011-04-24 16:40:29 +0000 | [diff] [blame] | 378 | assert(fieldOffset % charAlign == 0 && |
| 379 | "Field offset not aligned correctly"); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 380 | |
| 381 | // Append padding if necessary. |
Ken Dyck | 345a6de | 2011-04-24 16:40:29 +0000 | [diff] [blame] | 382 | AppendPadding(Types.getContext().toCharUnitsFromBits(fieldOffset), |
| 383 | CharUnits::One()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Ken Dyck | 345a6de | 2011-04-24 16:40:29 +0000 | [diff] [blame] | 385 | numBytesToAppend = Types.getContext().toCharUnitsFromBits( |
| 386 | llvm::RoundUpToAlignment(fieldSize, charAlign)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Ken Dyck | 345a6de | 2011-04-24 16:40:29 +0000 | [diff] [blame] | 388 | assert(!numBytesToAppend.isZero() && "No bytes to append!"); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 391 | // Add the bit field info. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 392 | BitFields.insert(std::make_pair(D, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 393 | CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Ken Dyck | 345a6de | 2011-04-24 16:40:29 +0000 | [diff] [blame] | 395 | AppendBytes(numBytesToAppend); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | BitsAvailableInLastField = |
Ken Dyck | 345a6de | 2011-04-24 16:40:29 +0000 | [diff] [blame] | 398 | Types.getContext().toBits(NextFieldOffset) - (fieldOffset + fieldSize); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 402 | uint64_t fieldOffset) { |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 403 | // If the field is packed, then we need a packed struct. |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 404 | if (!Packed && D->hasAttr<PackedAttr>()) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 405 | return false; |
| 406 | |
| 407 | if (D->isBitField()) { |
| 408 | // We must use packed structs for unnamed bit fields since they |
| 409 | // don't affect the struct alignment. |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 410 | if (!Packed && !D->getDeclName()) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 411 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 413 | LayoutBitField(D, fieldOffset); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 414 | return true; |
| 415 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 417 | CheckZeroInitializable(D->getType()); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 418 | |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 419 | assert(fieldOffset % Types.getTarget().getCharWidth() == 0 |
| 420 | && "field offset is not on a byte boundary!"); |
| 421 | CharUnits fieldOffsetInBytes |
| 422 | = Types.getContext().toCharUnitsFromBits(fieldOffset); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 424 | const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType()); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 425 | CharUnits typeAlignment = getTypeAlignment(Ty); |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 426 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 427 | // If the type alignment is larger then the struct alignment, we must use |
| 428 | // a packed struct. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 429 | if (typeAlignment > Alignment) { |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 430 | assert(!Packed && "Alignment is wrong even with packed struct!"); |
| 431 | return false; |
| 432 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 434 | if (!Packed) { |
| 435 | if (const RecordType *RT = D->getType()->getAs<RecordType>()) { |
| 436 | const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); |
| 437 | if (const MaxFieldAlignmentAttr *MFAA = |
| 438 | RD->getAttr<MaxFieldAlignmentAttr>()) { |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 439 | if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment)) |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 440 | return false; |
| 441 | } |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 445 | // Round up the field offset to the alignment of the field type. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 446 | CharUnits alignedNextFieldOffsetInBytes = |
| 447 | NextFieldOffset.RoundUpToAlignment(typeAlignment); |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 448 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 449 | if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) { |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 450 | // Try to resize the last base field. |
| 451 | if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) { |
| 452 | alignedNextFieldOffsetInBytes = |
| 453 | NextFieldOffset.RoundUpToAlignment(typeAlignment); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) { |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 458 | assert(!Packed && "Could not place field even with packed struct!"); |
| 459 | return false; |
| 460 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 462 | AppendPadding(fieldOffsetInBytes, typeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 464 | // Now append the field. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 465 | Fields[D] = FieldTypes.size(); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 466 | AppendField(fieldOffsetInBytes, Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 468 | LastLaidOutBase.invalidate(); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 469 | return true; |
| 470 | } |
| 471 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 472 | const llvm::Type * |
| 473 | CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field, |
| 474 | const ASTRecordLayout &Layout) { |
| 475 | if (Field->isBitField()) { |
| 476 | uint64_t FieldSize = |
| 477 | Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue(); |
| 478 | |
| 479 | // Ignore zero sized bit fields. |
| 480 | if (FieldSize == 0) |
| 481 | return 0; |
| 482 | |
Daniel Dunbar | 20b551a | 2010-04-20 17:52:30 +0000 | [diff] [blame] | 483 | const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext()); |
Ken Dyck | 7a0b19f | 2011-04-24 16:47:33 +0000 | [diff] [blame] | 484 | CharUnits NumBytesToAppend = Types.getContext().toCharUnitsFromBits( |
| 485 | llvm::RoundUpToAlignment(FieldSize, |
| 486 | Types.getContext().Target.getCharAlign())); |
Anders Carlsson | 2295f13 | 2010-04-17 21:04:52 +0000 | [diff] [blame] | 487 | |
Ken Dyck | 7a0b19f | 2011-04-24 16:47:33 +0000 | [diff] [blame] | 488 | if (NumBytesToAppend > CharUnits::One()) |
| 489 | FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity()); |
Anders Carlsson | 2295f13 | 2010-04-17 21:04:52 +0000 | [diff] [blame] | 490 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 491 | // Add the bit field info. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 492 | BitFields.insert(std::make_pair(Field, |
| 493 | CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize))); |
Anders Carlsson | 2295f13 | 2010-04-17 21:04:52 +0000 | [diff] [blame] | 494 | return FieldTy; |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 495 | } |
Daniel Dunbar | 20b551a | 2010-04-20 17:52:30 +0000 | [diff] [blame] | 496 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 497 | // This is a regular union field. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 498 | Fields[Field] = 0; |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 499 | return Types.ConvertTypeForMemRecursive(Field->getType()); |
| 500 | } |
| 501 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 502 | void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) { |
| 503 | assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 505 | const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 506 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 507 | const llvm::Type *unionType = 0; |
| 508 | CharUnits unionSize = CharUnits::Zero(); |
| 509 | CharUnits unionAlign = CharUnits::Zero(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 511 | bool hasOnlyZeroSizedBitFields = true; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 512 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 513 | unsigned fieldNo = 0; |
| 514 | for (RecordDecl::field_iterator field = D->field_begin(), |
| 515 | fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) { |
| 516 | assert(layout.getFieldOffset(fieldNo) == 0 && |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 517 | "Union field offset did not start at the beginning of record!"); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 518 | const llvm::Type *fieldType = LayoutUnionField(*field, layout); |
Anders Carlsson | f814ee6 | 2009-07-23 04:00:39 +0000 | [diff] [blame] | 519 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 520 | if (!fieldType) |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 521 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 523 | hasOnlyZeroSizedBitFields = false; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 524 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 525 | CharUnits fieldAlign = CharUnits::fromQuantity( |
| 526 | Types.getTargetData().getABITypeAlignment(fieldType)); |
| 527 | CharUnits fieldSize = CharUnits::fromQuantity( |
| 528 | Types.getTargetData().getTypeAllocSize(fieldType)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 530 | if (fieldAlign < unionAlign) |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 531 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 533 | if (fieldAlign > unionAlign || fieldSize > unionSize) { |
| 534 | unionType = fieldType; |
| 535 | unionAlign = fieldAlign; |
| 536 | unionSize = fieldSize; |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 540 | // Now add our field. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 541 | if (unionType) { |
| 542 | AppendField(CharUnits::Zero(), unionType); |
Anders Carlsson | 0e91275 | 2009-09-03 22:56:02 +0000 | [diff] [blame] | 543 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 544 | if (getTypeAlignment(unionType) > layout.getAlignment()) { |
Anders Carlsson | 0e91275 | 2009-09-03 22:56:02 +0000 | [diff] [blame] | 545 | // We need a packed struct. |
| 546 | Packed = true; |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 547 | unionAlign = CharUnits::One(); |
Anders Carlsson | 0e91275 | 2009-09-03 22:56:02 +0000 | [diff] [blame] | 548 | } |
| 549 | } |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 550 | if (unionAlign.isZero()) { |
| 551 | assert(hasOnlyZeroSizedBitFields && |
Anders Carlsson | b1ef991 | 2010-01-28 18:22:03 +0000 | [diff] [blame] | 552 | "0-align record did not have all zero-sized bit-fields!"); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 553 | unionAlign = CharUnits::One(); |
Fariborz Jahanian | e8e631c | 2009-11-06 20:47:40 +0000 | [diff] [blame] | 554 | } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 555 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 556 | // Append tail padding. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 557 | CharUnits recordSize = layout.getSize(); |
| 558 | if (recordSize > unionSize) |
| 559 | AppendPadding(recordSize, unionAlign); |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 560 | } |
| 561 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 562 | void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 563 | const CGRecordLayout &baseLayout, |
| 564 | CharUnits baseOffset) { |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 565 | ResizeLastBaseFieldIfNecessary(baseOffset); |
| 566 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 567 | AppendPadding(baseOffset, CharUnits::One()); |
Anders Carlsson | a518b2a | 2010-12-04 23:59:48 +0000 | [diff] [blame] | 568 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 569 | const ASTRecordLayout &baseASTLayout |
| 570 | = Types.getContext().getASTRecordLayout(base); |
| 571 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 572 | LastLaidOutBase.Offset = NextFieldOffset; |
| 573 | LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize(); |
| 574 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 575 | // Fields and bases can be laid out in the tail padding of previous |
| 576 | // bases. If this happens, we need to allocate the base as an i8 |
| 577 | // array; otherwise, we can use the subobject type. However, |
| 578 | // actually doing that would require knowledge of what immediately |
| 579 | // follows this base in the layout, so instead we do a conservative |
| 580 | // approximation, which is to use the base subobject type if it |
| 581 | // has the same LLVM storage size as the nvsize. |
| 582 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 583 | const llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType(); |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 584 | AppendField(baseOffset, subobjectType); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 585 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 586 | Types.addBaseSubobjectTypeName(base, baseLayout); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 590 | CharUnits baseOffset) { |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 591 | // Ignore empty bases. |
| 592 | if (base->isEmpty()) return; |
| 593 | |
| 594 | const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base); |
| 595 | if (IsZeroInitializableAsBase) { |
| 596 | assert(IsZeroInitializable && |
| 597 | "class zero-initializable as base but not as complete object"); |
| 598 | |
| 599 | IsZeroInitializable = IsZeroInitializableAsBase = |
| 600 | baseLayout.isZeroInitializableAsBase(); |
| 601 | } |
| 602 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 603 | LayoutBase(base, baseLayout, baseOffset); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 604 | NonVirtualBases[base] = (FieldTypes.size() - 1); |
Anders Carlsson | a518b2a | 2010-12-04 23:59:48 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 607 | void |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 608 | CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 609 | CharUnits baseOffset) { |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 610 | // Ignore empty bases. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 611 | if (base->isEmpty()) return; |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 612 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 613 | const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base); |
| 614 | if (IsZeroInitializable) |
| 615 | IsZeroInitializable = baseLayout.isZeroInitializableAsBase(); |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 616 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 617 | LayoutBase(base, baseLayout, baseOffset); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 618 | VirtualBases[base] = (FieldTypes.size() - 1); |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 621 | /// LayoutVirtualBases - layout the non-virtual bases of a record decl. |
| 622 | void |
| 623 | CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, |
| 624 | const ASTRecordLayout &Layout) { |
| 625 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 626 | E = RD->bases_end(); I != E; ++I) { |
| 627 | const CXXRecordDecl *BaseDecl = |
| 628 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 629 | |
| 630 | // We only want to lay out virtual bases that aren't indirect primary bases |
| 631 | // of some other base. |
| 632 | if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) { |
| 633 | // Only lay out the base once. |
| 634 | if (!LaidOutVirtualBases.insert(BaseDecl)) |
| 635 | continue; |
| 636 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 637 | CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
| 638 | LayoutVirtualBase(BaseDecl, vbaseOffset); |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | if (!BaseDecl->getNumVBases()) { |
| 642 | // This base isn't interesting since it doesn't have any virtual bases. |
| 643 | continue; |
| 644 | } |
| 645 | |
| 646 | LayoutVirtualBases(BaseDecl, Layout); |
| 647 | } |
| 648 | } |
| 649 | |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 650 | void |
| 651 | CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD, |
| 652 | const ASTRecordLayout &Layout) { |
| 653 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 654 | |
| 655 | // Check if we need to add a vtable pointer. |
| 656 | if (RD->isDynamicClass()) { |
| 657 | if (!PrimaryBase) { |
| 658 | const llvm::Type *FunctionType = |
| 659 | llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()), |
| 660 | /*isVarArg=*/true); |
| 661 | const llvm::Type *VTableTy = FunctionType->getPointerTo(); |
| 662 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 663 | assert(NextFieldOffset.isZero() && |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 664 | "VTable pointer must come first!"); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 665 | AppendField(CharUnits::Zero(), VTableTy->getPointerTo()); |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 666 | } else { |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 667 | if (!Layout.isPrimaryBaseVirtual()) |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 668 | LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero()); |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 669 | else |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 670 | LayoutVirtualBase(PrimaryBase, CharUnits::Zero()); |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | // Layout the non-virtual bases. |
| 675 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 676 | E = RD->bases_end(); I != E; ++I) { |
| 677 | if (I->isVirtual()) |
| 678 | continue; |
| 679 | |
| 680 | const CXXRecordDecl *BaseDecl = |
| 681 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 682 | |
| 683 | // We've already laid out the primary base. |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 684 | if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual()) |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 685 | continue; |
| 686 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 687 | LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl)); |
Anders Carlsson | d681a29 | 2009-12-16 17:27:20 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 691 | bool |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 692 | CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) { |
| 693 | const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD); |
| 694 | |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 695 | CharUnits NonVirtualSize = Layout.getNonVirtualSize(); |
| 696 | CharUnits NonVirtualAlign = Layout.getNonVirtualAlign(); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 697 | CharUnits AlignedNonVirtualTypeSize = |
| 698 | NonVirtualSize.RoundUpToAlignment(NonVirtualAlign); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 699 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 700 | // First check if we can use the same fields as for the complete class. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 701 | CharUnits RecordSize = Layout.getSize(); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 702 | if (AlignedNonVirtualTypeSize == RecordSize) |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 703 | return true; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 704 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 705 | // Check if we need padding. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 706 | CharUnits AlignedNextFieldOffset = |
| 707 | NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct()); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 708 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 709 | if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) { |
| 710 | assert(!Packed && "cannot layout even as packed struct"); |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 711 | return false; // Needs packing. |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 712 | } |
| 713 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 714 | bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset); |
| 715 | if (needsPadding) { |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 716 | CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 717 | FieldTypes.push_back(getByteArrayType(NumBytes)); |
| 718 | } |
| 719 | |
| 720 | BaseSubobjectType = llvm::StructType::get(Types.getLLVMContext(), |
| 721 | FieldTypes, Packed); |
| 722 | |
| 723 | if (needsPadding) { |
| 724 | // Pull the padding back off. |
| 725 | FieldTypes.pop_back(); |
| 726 | } |
| 727 | |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 728 | return true; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 731 | bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
| 732 | assert(!D->isUnion() && "Can't call LayoutFields on a union!"); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 733 | assert(!Alignment.isZero() && "Did not set alignment!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 734 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 735 | const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 736 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 737 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D); |
| 738 | if (RD) |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 739 | LayoutNonVirtualBases(RD, Layout); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 740 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 741 | unsigned FieldNo = 0; |
Fariborz Jahanian | 7b2b1ec | 2009-07-27 20:57:45 +0000 | [diff] [blame] | 742 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 744 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { |
| 745 | if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | assert(!Packed && |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 747 | "Could not layout fields even with a packed LLVM struct!"); |
| 748 | return false; |
| 749 | } |
| 750 | } |
| 751 | |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 752 | if (RD) { |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 753 | // We've laid out the non-virtual bases and the fields, now compute the |
| 754 | // non-virtual base field types. |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 755 | if (!ComputeNonVirtualBaseType(RD)) { |
| 756 | assert(!Packed && "Could not layout even with a packed LLVM struct!"); |
| 757 | return false; |
| 758 | } |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 759 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 760 | // And lay out the virtual bases. |
| 761 | RD->getIndirectPrimaryBases(IndirectPrimaryBases); |
| 762 | if (Layout.isPrimaryBaseVirtual()) |
| 763 | IndirectPrimaryBases.insert(Layout.getPrimaryBase()); |
| 764 | LayoutVirtualBases(RD, Layout); |
| 765 | } |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 766 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 767 | // Append tail padding if necessary. |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 768 | AppendTailPadding(Layout.getSize()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 770 | return true; |
| 771 | } |
| 772 | |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 773 | void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) { |
| 774 | ResizeLastBaseFieldIfNecessary(RecordSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 776 | assert(NextFieldOffset <= RecordSize && "Size mismatch!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 778 | CharUnits AlignedNextFieldOffset = |
| 779 | NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct()); |
Anders Carlsson | 220bf4f | 2009-12-08 01:24:23 +0000 | [diff] [blame] | 780 | |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 781 | if (AlignedNextFieldOffset == RecordSize) { |
Anders Carlsson | 220bf4f | 2009-12-08 01:24:23 +0000 | [diff] [blame] | 782 | // We don't need any padding. |
| 783 | return; |
| 784 | } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 785 | |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 786 | CharUnits NumPadBytes = RecordSize - NextFieldOffset; |
Anders Carlsson | b97a3ec | 2009-07-27 14:55:54 +0000 | [diff] [blame] | 787 | AppendBytes(NumPadBytes); |
| 788 | } |
| 789 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 790 | void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset, |
| 791 | const llvm::Type *fieldType) { |
| 792 | CharUnits fieldSize = |
| 793 | CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType)); |
Anders Carlsson | 6e853bf | 2009-07-24 02:45:50 +0000 | [diff] [blame] | 794 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 795 | FieldTypes.push_back(fieldType); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 796 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 797 | NextFieldOffset = fieldOffset + fieldSize; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 798 | BitsAvailableInLastField = 0; |
| 799 | } |
| 800 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 801 | void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset, |
| 802 | CharUnits fieldAlignment) { |
| 803 | assert(NextFieldOffset <= fieldOffset && |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 804 | "Incorrect field layout!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 806 | // Round up the field offset to the alignment of the field type. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 807 | CharUnits alignedNextFieldOffset = |
| 808 | NextFieldOffset.RoundUpToAlignment(fieldAlignment); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 809 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 810 | if (alignedNextFieldOffset < fieldOffset) { |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 811 | // Even with alignment, the field offset is not at the right place, |
| 812 | // insert padding. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 813 | CharUnits padding = fieldOffset - NextFieldOffset; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 814 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 815 | AppendBytes(padding); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 819 | bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) { |
| 820 | // Check if we have a base to resize. |
| 821 | if (!LastLaidOutBase.isValid()) |
| 822 | return false; |
| 823 | |
| 824 | // This offset does not overlap with the tail padding. |
| 825 | if (offset >= NextFieldOffset) |
| 826 | return false; |
| 827 | |
| 828 | // Restore the field offset and append an i8 array instead. |
| 829 | FieldTypes.pop_back(); |
| 830 | NextFieldOffset = LastLaidOutBase.Offset; |
| 831 | AppendBytes(LastLaidOutBase.NonVirtualSize); |
| 832 | LastLaidOutBase.invalidate(); |
| 833 | |
| 834 | return true; |
| 835 | } |
| 836 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 837 | const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) { |
| 838 | assert(!numBytes.isZero() && "Empty byte arrays aren't allowed."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 840 | const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext()); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 841 | if (numBytes > CharUnits::One()) |
| 842 | Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 844 | return Ty; |
| 845 | } |
| 846 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 847 | void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) { |
| 848 | if (numBytes.isZero()) |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 849 | return; |
| 850 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 851 | // Append the padding field |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 852 | AppendField(NextFieldOffset, getByteArrayType(numBytes)); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 853 | } |
| 854 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 855 | CharUnits CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const { |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 856 | if (Packed) |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 857 | return CharUnits::One(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 859 | return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty)); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 860 | } |
| 861 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 862 | CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const { |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 863 | if (Packed) |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 864 | return CharUnits::One(); |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 865 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 866 | CharUnits maxAlignment = CharUnits::One(); |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 867 | for (size_t i = 0; i != FieldTypes.size(); ++i) |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 868 | maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i])); |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 869 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 870 | return maxAlignment; |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 871 | } |
| 872 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 873 | /// Merge in whether a field of the given type is zero-initializable. |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 874 | void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) { |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 875 | // This record already contains a member pointer. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 876 | if (!IsZeroInitializableAsBase) |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 877 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 879 | // Can only have member pointers if we're compiling C++. |
| 880 | if (!Types.getContext().getLangOptions().CPlusPlus) |
| 881 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 883 | const Type *elementType = T->getBaseElementTypeUnsafe(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 885 | if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) { |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 886 | if (!Types.getCXXABI().isZeroInitializable(MPT)) |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 887 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
| 888 | } else if (const RecordType *RT = elementType->getAs<RecordType>()) { |
Anders Carlsson | e8bfe41 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 889 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 890 | const CGRecordLayout &Layout = Types.getCGRecordLayout(RD); |
| 891 | if (!Layout.isZeroInitializable()) |
| 892 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 893 | } |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 896 | CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) { |
| 897 | CGRecordLayoutBuilder Builder(*this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 899 | Builder.Layout(D); |
Anders Carlsson | e1d5ca5 | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 900 | |
Anders Carlsson | 36e2fa8 | 2010-11-24 19:37:16 +0000 | [diff] [blame] | 901 | const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(), |
| 902 | Builder.FieldTypes, |
| 903 | Builder.Packed); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 905 | // If we're in C++, compute the base subobject type. |
Anders Carlsson | 36e2fa8 | 2010-11-24 19:37:16 +0000 | [diff] [blame] | 906 | const llvm::StructType *BaseTy = 0; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 907 | if (isa<CXXRecordDecl>(D)) { |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 908 | BaseTy = Builder.BaseSubobjectType; |
| 909 | if (!BaseTy) BaseTy = Ty; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 912 | CGRecordLayout *RL = |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 913 | new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable, |
| 914 | Builder.IsZeroInitializableAsBase); |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 915 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 916 | RL->NonVirtualBases.swap(Builder.NonVirtualBases); |
| 917 | RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases); |
Anders Carlsson | 061ca52 | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 918 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 919 | // Add all the field numbers. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 920 | RL->FieldInfo.swap(Builder.Fields); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 921 | |
| 922 | // Add bitfield info. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 923 | RL->BitFields.swap(Builder.BitFields); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 925 | // Dump the layout, if requested. |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 926 | if (getContext().getLangOptions().DumpRecordLayouts) { |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 927 | llvm::errs() << "\n*** Dumping IRgen Record Layout\n"; |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 928 | llvm::errs() << "Record: "; |
| 929 | D->dump(); |
| 930 | llvm::errs() << "\nLayout: "; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 931 | RL->dump(); |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 932 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 933 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 934 | #ifndef NDEBUG |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 935 | // Verify that the computed LLVM struct size matches the AST layout size. |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 936 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D); |
| 937 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 938 | uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize()); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 939 | assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) && |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 940 | "Type size mismatch!"); |
| 941 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 942 | if (BaseTy) { |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 943 | CharUnits NonVirtualSize = Layout.getNonVirtualSize(); |
| 944 | CharUnits NonVirtualAlign = Layout.getNonVirtualAlign(); |
| 945 | CharUnits AlignedNonVirtualTypeSize = |
| 946 | NonVirtualSize.RoundUpToAlignment(NonVirtualAlign); |
| 947 | |
| 948 | uint64_t AlignedNonVirtualTypeSizeInBits = |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 949 | getContext().toBits(AlignedNonVirtualTypeSize); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 950 | |
| 951 | assert(AlignedNonVirtualTypeSizeInBits == |
| 952 | getTargetData().getTypeAllocSizeInBits(BaseTy) && |
| 953 | "Type size mismatch!"); |
| 954 | } |
| 955 | |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 956 | // Verify that the LLVM and AST field offsets agree. |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 957 | const llvm::StructType *ST = |
| 958 | dyn_cast<llvm::StructType>(RL->getLLVMType()); |
| 959 | const llvm::StructLayout *SL = getTargetData().getStructLayout(ST); |
| 960 | |
| 961 | const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D); |
| 962 | RecordDecl::field_iterator it = D->field_begin(); |
| 963 | for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) { |
| 964 | const FieldDecl *FD = *it; |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 965 | |
| 966 | // For non-bit-fields, just check that the LLVM struct offset matches the |
| 967 | // AST offset. |
| 968 | if (!FD->isBitField()) { |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 969 | unsigned FieldNo = RL->getLLVMFieldNo(FD); |
| 970 | assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) && |
| 971 | "Invalid field offset!"); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 972 | continue; |
| 973 | } |
| 974 | |
| 975 | // Ignore unnamed bit-fields. |
| 976 | if (!FD->getDeclName()) |
| 977 | continue; |
| 978 | |
| 979 | const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD); |
| 980 | for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) { |
| 981 | const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i); |
| 982 | |
| 983 | // Verify that every component access is within the structure. |
| 984 | uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex); |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 985 | uint64_t AccessBitOffset = FieldOffset + |
Ken Dyck | f76759c | 2011-04-24 10:04:59 +0000 | [diff] [blame] | 986 | getContext().toBits(AI.FieldByteOffset); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 987 | assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits && |
| 988 | "Invalid bit-field access (out of range)!"); |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | #endif |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 992 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 993 | return RL; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 994 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 995 | |
| 996 | void CGRecordLayout::print(llvm::raw_ostream &OS) const { |
| 997 | OS << "<CGRecordLayout\n"; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 998 | OS << " LLVMType:" << *CompleteObjectType << "\n"; |
| 999 | if (BaseSubobjectType) |
| 1000 | OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n"; |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 1001 | OS << " IsZeroInitializable:" << IsZeroInitializable << "\n"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1002 | OS << " BitFields:[\n"; |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 1003 | |
| 1004 | // Print bit-field infos in declaration order. |
| 1005 | std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1006 | for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator |
| 1007 | it = BitFields.begin(), ie = BitFields.end(); |
| 1008 | it != ie; ++it) { |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 1009 | const RecordDecl *RD = it->first->getParent(); |
| 1010 | unsigned Index = 0; |
| 1011 | for (RecordDecl::field_iterator |
| 1012 | it2 = RD->field_begin(); *it2 != it->first; ++it2) |
| 1013 | ++Index; |
| 1014 | BFIs.push_back(std::make_pair(Index, &it->second)); |
| 1015 | } |
| 1016 | llvm::array_pod_sort(BFIs.begin(), BFIs.end()); |
| 1017 | for (unsigned i = 0, e = BFIs.size(); i != e; ++i) { |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 1018 | OS.indent(4); |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 1019 | BFIs[i].second->print(OS); |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1020 | OS << "\n"; |
| 1021 | } |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 1022 | |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1023 | OS << "]>\n"; |
| 1024 | } |
| 1025 | |
| 1026 | void CGRecordLayout::dump() const { |
| 1027 | print(llvm::errs()); |
| 1028 | } |
| 1029 | |
| 1030 | void CGBitFieldInfo::print(llvm::raw_ostream &OS) const { |
| 1031 | OS << "<CGBitFieldInfo"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1032 | OS << " Size:" << Size; |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 1033 | OS << " IsSigned:" << IsSigned << "\n"; |
| 1034 | |
| 1035 | OS.indent(4 + strlen("<CGBitFieldInfo")); |
| 1036 | OS << " NumComponents:" << getNumComponents(); |
| 1037 | OS << " Components: ["; |
| 1038 | if (getNumComponents()) { |
| 1039 | OS << "\n"; |
| 1040 | for (unsigned i = 0, e = getNumComponents(); i != e; ++i) { |
| 1041 | const AccessInfo &AI = getComponent(i); |
| 1042 | OS.indent(8); |
| 1043 | OS << "<AccessInfo" |
| 1044 | << " FieldIndex:" << AI.FieldIndex |
Ken Dyck | f76759c | 2011-04-24 10:04:59 +0000 | [diff] [blame] | 1045 | << " FieldByteOffset:" << AI.FieldByteOffset.getQuantity() |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 1046 | << " FieldBitStart:" << AI.FieldBitStart |
| 1047 | << " AccessWidth:" << AI.AccessWidth << "\n"; |
| 1048 | OS.indent(8 + strlen("<AccessInfo")); |
Ken Dyck | 27337a8 | 2011-04-24 10:13:17 +0000 | [diff] [blame] | 1049 | OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity() |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 1050 | << " TargetBitOffset:" << AI.TargetBitOffset |
| 1051 | << " TargetBitWidth:" << AI.TargetBitWidth |
| 1052 | << ">\n"; |
| 1053 | } |
| 1054 | OS.indent(4); |
| 1055 | } |
| 1056 | OS << "]>"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | void CGBitFieldInfo::dump() const { |
| 1060 | print(llvm::errs()); |
| 1061 | } |