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