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