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