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