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