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