blob: 855552f960cd369a49ce3826087272d43cfe6908 [file] [log] [blame]
Daniel Dunbar23ee4b72010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson307846f2009-07-23 03:17:50 +00002//
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 Dunbar23ee4b72010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson307846f2009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
Anders Carlsson4131f002010-11-24 22:50:27 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/RecordLayout.h"
21#include "CodeGenTypes.h"
John McCall614dbdc2010-08-22 21:01:12 +000022#include "CGCXXABI.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000023#include "llvm/DerivedTypes.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000024#include "llvm/Type.h"
Daniel Dunbar2ba67442010-04-21 19:10:49 +000025#include "llvm/Support/Debug.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000026#include "llvm/Support/raw_ostream.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000027#include "llvm/Target/TargetData.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000028using namespace clang;
29using namespace CodeGen;
30
John McCallbcd38212010-11-30 23:17:27 +000031namespace {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000032
33class CGRecordLayoutBuilder {
34public:
35 /// FieldTypes - Holds the LLVM types that the struct is created from.
36 std::vector<const llvm::Type *> FieldTypes;
37
Anders Carlssonc1351ca2010-11-09 05:25:47 +000038 /// NonVirtualBaseFieldTypes - Holds the LLVM types for the non-virtual part
39 /// of the struct. For example, consider:
40 ///
41 /// struct A { int i; };
42 /// struct B { void *v; };
43 /// struct C : virtual A, B { };
44 ///
45 /// The LLVM type of C will be
46 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
47 ///
48 /// And the LLVM type of the non-virtual base struct will be
49 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
50 std::vector<const llvm::Type *> NonVirtualBaseFieldTypes;
51
52 /// NonVirtualBaseTypeIsSameAsCompleteType - Whether the non-virtual part of
53 /// the struct is equivalent to the complete struct.
54 bool NonVirtualBaseTypeIsSameAsCompleteType;
55
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000056 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
57 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
58 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
59
60 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
Daniel Dunbard4549102010-04-06 01:07:41 +000061 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000062 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
63
Anders Carlsson061ca522010-05-18 05:22:06 +000064 typedef std::pair<const CXXRecordDecl *, unsigned> LLVMBaseInfo;
65 llvm::SmallVector<LLVMBaseInfo, 16> LLVMNonVirtualBases;
Anders Carlsson4131f002010-11-24 22:50:27 +000066
67 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
68 /// primary base classes for some other direct or indirect base class.
69 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
70
Anders Carlssona459adb2010-11-28 19:18:44 +000071 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
72 /// avoid laying out virtual bases more than once.
73 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
74
John McCall614dbdc2010-08-22 21:01:12 +000075 /// IsZeroInitializable - Whether this struct can be C++
76 /// zero-initialized with an LLVM zeroinitializer.
77 bool IsZeroInitializable;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000078
79 /// Packed - Whether the resulting LLVM struct will be packed or not.
80 bool Packed;
81
82private:
83 CodeGenTypes &Types;
84
85 /// Alignment - Contains the alignment of the RecordDecl.
86 //
87 // FIXME: This is not needed and should be removed.
88 unsigned Alignment;
89
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000090 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
91 /// this will have the number of bits still available in the field.
92 char BitsAvailableInLastField;
93
94 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
95 uint64_t NextFieldOffsetInBytes;
96
Anders Carlsson1de2f572010-04-17 20:49:27 +000097 /// LayoutUnionField - Will layout a field in an union and return the type
98 /// that the field will have.
99 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
100 const ASTRecordLayout &Layout);
101
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000102 /// LayoutUnion - Will layout a union RecordDecl.
103 void LayoutUnion(const RecordDecl *D);
104
105 /// LayoutField - try to layout all fields in the record decl.
106 /// Returns false if the operation failed because the struct is not packed.
107 bool LayoutFields(const RecordDecl *D);
108
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000109 /// LayoutVirtualBase - layout a single virtual base.
110 void LayoutVirtualBase(const CXXRecordDecl *BaseDecl, uint64_t BaseOffset);
111
Anders Carlssona459adb2010-11-28 19:18:44 +0000112 /// LayoutVirtualBases - layout the virtual bases of a record decl.
113 void LayoutVirtualBases(const CXXRecordDecl *RD,
114 const ASTRecordLayout &Layout);
115
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000116 /// LayoutNonVirtualBase - layout a single non-virtual base.
117 void LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
118 uint64_t BaseOffset);
119
Anders Carlssona459adb2010-11-28 19:18:44 +0000120 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000121 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
122 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000123
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000124 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
125 void ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
126
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000127 /// LayoutField - layout a single field. Returns false if the operation failed
128 /// because the current struct is not packed.
129 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
130
131 /// LayoutBitField - layout a single bit field.
132 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
133
134 /// AppendField - Appends a field with the given offset and type.
135 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
136
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000137 /// AppendPadding - Appends enough padding bytes so that the total
138 /// struct size is a multiple of the field alignment.
Anders Carlssond74cad82010-12-04 23:53:18 +0000139 void AppendPadding(uint64_t FieldOffsetInBytes,
140 unsigned FieldAlignmentInBytes);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000141
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000142 /// getByteArrayType - Returns a byte array type with the given number of
143 /// elements.
144 const llvm::Type *getByteArrayType(uint64_t NumBytes);
145
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000146 /// AppendBytes - Append a given number of bytes to the record.
147 void AppendBytes(uint64_t NumBytes);
148
149 /// AppendTailPadding - Append enough tail padding so that the type will have
150 /// the passed size.
151 void AppendTailPadding(uint64_t RecordSize);
152
153 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000154
Anders Carlssonacf877b2010-11-28 23:06:23 +0000155 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
156 /// LLVM element types.
157 unsigned getAlignmentAsLLVMStruct() const;
158
John McCall614dbdc2010-08-22 21:01:12 +0000159 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000160 /// to data member.
John McCall614dbdc2010-08-22 21:01:12 +0000161 void CheckZeroInitializable(QualType T);
162 void CheckZeroInitializable(const CXXRecordDecl *RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000163
164public:
165 CGRecordLayoutBuilder(CodeGenTypes &Types)
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000166 : NonVirtualBaseTypeIsSameAsCompleteType(false), IsZeroInitializable(true),
Anders Carlssonacf877b2010-11-28 23:06:23 +0000167 Packed(false), Types(Types), Alignment(0), BitsAvailableInLastField(0),
168 NextFieldOffsetInBytes(0) { }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000169
170 /// Layout - Will layout a RecordDecl.
171 void Layout(const RecordDecl *D);
172};
173
174}
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000175
Anders Carlsson307846f2009-07-23 03:17:50 +0000176void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000177 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlsson09a37742009-09-02 17:51:33 +0000178 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000179
Anders Carlsson697f6592009-07-23 03:43:54 +0000180 if (D->isUnion()) {
181 LayoutUnion(D);
182 return;
183 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000184
Anders Carlsson307846f2009-07-23 03:17:50 +0000185 if (LayoutFields(D))
186 return;
Mike Stump11289f42009-09-09 15:08:12 +0000187
Anders Carlsson307846f2009-07-23 03:17:50 +0000188 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000189 Packed = true;
Anders Carlssond5d64132009-07-28 17:56:36 +0000190 NextFieldOffsetInBytes = 0;
Anders Carlsson307846f2009-07-23 03:17:50 +0000191 FieldTypes.clear();
Anders Carlsson307846f2009-07-23 03:17:50 +0000192 LLVMFields.clear();
193 LLVMBitFields.clear();
Anders Carlsson061ca522010-05-18 05:22:06 +0000194 LLVMNonVirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000195
Anders Carlsson307846f2009-07-23 03:17:50 +0000196 LayoutFields(D);
197}
198
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000199CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
200 const FieldDecl *FD,
201 uint64_t FieldOffset,
202 uint64_t FieldSize,
203 uint64_t ContainingTypeSizeInBits,
204 unsigned ContainingTypeAlign) {
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000205 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarb935b932010-04-13 20:58:55 +0000206 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
207 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000208
209 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000210
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000211 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000212 // We have a wide bit-field. The extra bits are only used for padding, so
213 // if we have a bitfield of type T, with size N:
214 //
215 // T t : N;
216 //
217 // We can just assume that it's:
218 //
219 // T t : sizeof(T);
220 //
221 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000222 }
223
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000224 // Compute the access components. The policy we use is to start by attempting
225 // to access using the width of the bit-field type itself and to always access
226 // at aligned indices of that type. If such an access would fail because it
227 // extends past the bound of the type, then we reduce size to the next smaller
228 // power of two and retry. The current algorithm assumes pow2 sized types,
229 // although this is easy to fix.
230 //
231 // FIXME: This algorithm is wrong on big-endian systems, I think.
232 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
233 CGBitFieldInfo::AccessInfo Components[3];
234 unsigned NumComponents = 0;
235 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
236 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000237
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000238 // Round down from the field offset to find the first access position that is
239 // at an aligned offset of the initial access type.
Daniel Dunbar59813772010-04-22 15:22:33 +0000240 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
241
242 // Adjust initial access size to fit within record.
243 while (AccessWidth > 8 &&
244 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
245 AccessWidth >>= 1;
246 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
247 }
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000248
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000249 while (AccessedTargetBits < FieldSize) {
250 // Check that we can access using a type of this size, without reading off
251 // the end of the structure. This can occur with packed structures and
252 // -fno-bitfield-type-align, for example.
253 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
254 // If so, reduce access size to the next smaller power-of-two and retry.
255 AccessWidth >>= 1;
256 assert(AccessWidth >= 8 && "Cannot access under byte size!");
257 continue;
258 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000259
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000260 // Otherwise, add an access component.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000261
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000262 // First, compute the bits inside this access which are part of the
263 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
264 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
265 // in the target that we are reading.
Daniel Dunbar59813772010-04-22 15:22:33 +0000266 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
267 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000268 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
269 uint64_t AccessBitsInFieldSize =
Daniel Dunbar59813772010-04-22 15:22:33 +0000270 std::min(AccessWidth + AccessStart,
271 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar5d6c07e2010-04-22 14:56:10 +0000272
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000273 assert(NumComponents < 3 && "Unexpected number of components!");
274 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
275 AI.FieldIndex = 0;
276 // FIXME: We still follow the old access pattern of only using the field
277 // byte offset. We should switch this once we fix the struct layout to be
278 // pretty.
279 AI.FieldByteOffset = AccessStart / 8;
280 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
281 AI.AccessWidth = AccessWidth;
Daniel Dunbarfc66e0e2010-04-22 03:17:04 +0000282 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000283 AI.TargetBitOffset = AccessedTargetBits;
284 AI.TargetBitWidth = AccessBitsInFieldSize;
285
286 AccessStart += AccessWidth;
287 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000288 }
289
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000290 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000291 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000292}
293
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000294CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
295 const FieldDecl *FD,
296 uint64_t FieldOffset,
297 uint64_t FieldSize) {
298 const RecordDecl *RD = FD->getParent();
299 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
300 uint64_t ContainingTypeSizeInBits = RL.getSize();
301 unsigned ContainingTypeAlign = RL.getAlignment();
302
303 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
304 ContainingTypeAlign);
305}
306
Anders Carlsson307846f2009-07-23 03:17:50 +0000307void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
308 uint64_t FieldOffset) {
Mike Stump11289f42009-09-09 15:08:12 +0000309 uint64_t FieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000310 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000311
Anders Carlsson307846f2009-07-23 03:17:50 +0000312 if (FieldSize == 0)
313 return;
314
Anders Carlssond5d64132009-07-28 17:56:36 +0000315 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000316 unsigned NumBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000317
Anders Carlsson307846f2009-07-23 03:17:50 +0000318 if (FieldOffset < NextFieldOffset) {
319 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +0000320 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump11289f42009-09-09 15:08:12 +0000321
Anders Carlsson307846f2009-07-23 03:17:50 +0000322 // The bitfield begins in the previous bit-field.
Mike Stump11289f42009-09-09 15:08:12 +0000323 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000324 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
325 } else {
326 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
327
328 // Append padding if necessary.
Anders Carlssond74cad82010-12-04 23:53:18 +0000329 AppendPadding(FieldOffset / 8, 1);
Mike Stump11289f42009-09-09 15:08:12 +0000330
331 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000332 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000333
Anders Carlsson307846f2009-07-23 03:17:50 +0000334 assert(NumBytesToAppend && "No bytes to append!");
335 }
336
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000337 // Add the bit field info.
338 LLVMBitFields.push_back(
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000339 LLVMBitFieldInfo(D, CGBitFieldInfo::MakeInfo(Types, D, FieldOffset,
340 FieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000341
Anders Carlsson307846f2009-07-23 03:17:50 +0000342 AppendBytes(NumBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000343
Mike Stump11289f42009-09-09 15:08:12 +0000344 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +0000345 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000346}
347
348bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
349 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000350 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000351 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000352 return false;
353
354 if (D->isBitField()) {
355 // We must use packed structs for unnamed bit fields since they
356 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000357 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000358 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000359
Anders Carlsson307846f2009-07-23 03:17:50 +0000360 LayoutBitField(D, FieldOffset);
361 return true;
362 }
Mike Stump11289f42009-09-09 15:08:12 +0000363
John McCall614dbdc2010-08-22 21:01:12 +0000364 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000365
Anders Carlsson307846f2009-07-23 03:17:50 +0000366 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000367 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000368
Anders Carlsson19702bb2009-08-04 16:29:15 +0000369 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
370 unsigned TypeAlignment = getTypeAlignment(Ty);
371
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000372 // If the type alignment is larger then the struct alignment, we must use
373 // a packed struct.
374 if (TypeAlignment > Alignment) {
375 assert(!Packed && "Alignment is wrong even with packed struct!");
376 return false;
377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000379 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
380 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
Daniel Dunbar40130442010-05-27 01:12:46 +0000381 if (const MaxFieldAlignmentAttr *MFAA =
382 RD->getAttr<MaxFieldAlignmentAttr>()) {
383 if (MFAA->getAlignment() != TypeAlignment * 8 && !Packed)
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000384 return false;
385 }
386 }
387
Anders Carlsson19702bb2009-08-04 16:29:15 +0000388 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000389 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson19702bb2009-08-04 16:29:15 +0000390 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
391
392 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
393 assert(!Packed && "Could not place field even with packed struct!");
394 return false;
395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Anders Carlssond74cad82010-12-04 23:53:18 +0000397 AppendPadding(FieldOffsetInBytes, TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000398
Anders Carlsson307846f2009-07-23 03:17:50 +0000399 // Now append the field.
400 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000401 AppendField(FieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000402
Anders Carlsson307846f2009-07-23 03:17:50 +0000403 return true;
404}
405
Anders Carlsson1de2f572010-04-17 20:49:27 +0000406const llvm::Type *
407CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
408 const ASTRecordLayout &Layout) {
409 if (Field->isBitField()) {
410 uint64_t FieldSize =
411 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
412
413 // Ignore zero sized bit fields.
414 if (FieldSize == 0)
415 return 0;
416
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000417 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
418 unsigned NumBytesToAppend =
419 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlsson2295f132010-04-17 21:04:52 +0000420
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000421 if (NumBytesToAppend > 1)
422 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlsson2295f132010-04-17 21:04:52 +0000423
Anders Carlsson1de2f572010-04-17 20:49:27 +0000424 // Add the bit field info.
425 LLVMBitFields.push_back(
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000426 LLVMBitFieldInfo(Field, CGBitFieldInfo::MakeInfo(Types, Field,
427 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000428 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000429 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000430
Anders Carlsson1de2f572010-04-17 20:49:27 +0000431 // This is a regular union field.
432 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
433 return Types.ConvertTypeForMemRecursive(Field->getType());
434}
435
Anders Carlsson697f6592009-07-23 03:43:54 +0000436void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
437 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000438
Anders Carlsson697f6592009-07-23 03:43:54 +0000439 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000440
Anders Carlsson697f6592009-07-23 03:43:54 +0000441 const llvm::Type *Ty = 0;
442 uint64_t Size = 0;
443 unsigned Align = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000444
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000445 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000446
Anders Carlsson697f6592009-07-23 03:43:54 +0000447 unsigned FieldNo = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000448 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson697f6592009-07-23 03:43:54 +0000449 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump11289f42009-09-09 15:08:12 +0000450 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000451 "Union field offset did not start at the beginning of record!");
Anders Carlsson1de2f572010-04-17 20:49:27 +0000452 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000453
Anders Carlsson1de2f572010-04-17 20:49:27 +0000454 if (!FieldTy)
455 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000456
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000457 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000458
Anders Carlssone2accf42009-07-23 21:52:03 +0000459 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
460 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump11289f42009-09-09 15:08:12 +0000461
Anders Carlsson697f6592009-07-23 03:43:54 +0000462 if (FieldAlign < Align)
463 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000464
Anders Carlsson697f6592009-07-23 03:43:54 +0000465 if (FieldAlign > Align || FieldSize > Size) {
466 Ty = FieldTy;
467 Align = FieldAlign;
468 Size = FieldSize;
469 }
470 }
Mike Stump11289f42009-09-09 15:08:12 +0000471
Anders Carlsson697f6592009-07-23 03:43:54 +0000472 // Now add our field.
Anders Carlsson0e912752009-09-03 22:56:02 +0000473 if (Ty) {
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000474 AppendField(0, Ty);
Anders Carlsson0e912752009-09-03 22:56:02 +0000475
476 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
477 // We need a packed struct.
478 Packed = true;
479 Align = 1;
480 }
481 }
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000482 if (!Align) {
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000483 assert(HasOnlyZeroSizedBitFields &&
484 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000485 Align = 1;
486 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000487
Anders Carlsson697f6592009-07-23 03:43:54 +0000488 // Append tail padding.
489 if (Layout.getSize() / 8 > Size)
490 AppendPadding(Layout.getSize() / 8, Align);
491}
492
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000493void
494CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *BaseDecl,
495 uint64_t BaseOffset) {
496 // Ignore empty bases.
497 if (BaseDecl->isEmpty())
498 return;
499
500 CheckZeroInitializable(BaseDecl);
501
502 const ASTRecordLayout &Layout =
503 Types.getContext().getASTRecordLayout(BaseDecl);
504
505 uint64_t NonVirtualSize = Layout.getNonVirtualSize();
506
507 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
508 AppendPadding(BaseOffset / 8, 1);
509
510 // FIXME: Add the vbase field info.
511
512 AppendBytes(NonVirtualSize / 8);
513
514}
515
Anders Carlssona459adb2010-11-28 19:18:44 +0000516/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
517void
518CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
519 const ASTRecordLayout &Layout) {
520 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
521 E = RD->bases_end(); I != E; ++I) {
522 const CXXRecordDecl *BaseDecl =
523 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
524
525 // We only want to lay out virtual bases that aren't indirect primary bases
526 // of some other base.
527 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
528 // Only lay out the base once.
529 if (!LaidOutVirtualBases.insert(BaseDecl))
530 continue;
531
532 uint64_t VBaseOffset = Layout.getVBaseClassOffsetInBits(BaseDecl);
533 LayoutVirtualBase(BaseDecl, VBaseOffset);
534 }
535
536 if (!BaseDecl->getNumVBases()) {
537 // This base isn't interesting since it doesn't have any virtual bases.
538 continue;
539 }
540
541 LayoutVirtualBases(BaseDecl, Layout);
542 }
543}
544
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000545void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
546 uint64_t BaseOffset) {
Anders Carlsson39a6b222010-11-22 00:03:08 +0000547 // Ignore empty bases.
548 if (BaseDecl->isEmpty())
549 return;
550
551 CheckZeroInitializable(BaseDecl);
552
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000553 const ASTRecordLayout &Layout =
554 Types.getContext().getASTRecordLayout(BaseDecl);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000555
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000556 uint64_t NonVirtualSize = Layout.getNonVirtualSize();
557
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000558 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
559 AppendPadding(BaseOffset / 8, 1);
Anders Carlsson061ca522010-05-18 05:22:06 +0000560
561 // Append the base field.
562 LLVMNonVirtualBases.push_back(LLVMBaseInfo(BaseDecl, FieldTypes.size()));
563
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000564 AppendBytes(NonVirtualSize / 8);
565}
566
567void
568CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
569 const ASTRecordLayout &Layout) {
570 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
571
572 // Check if we need to add a vtable pointer.
573 if (RD->isDynamicClass()) {
574 if (!PrimaryBase) {
575 const llvm::Type *FunctionType =
576 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
577 /*isVarArg=*/true);
578 const llvm::Type *VTableTy = FunctionType->getPointerTo();
579
580 assert(NextFieldOffsetInBytes == 0 &&
581 "VTable pointer must come first!");
582 AppendField(NextFieldOffsetInBytes, VTableTy->getPointerTo());
583 } else {
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000584 if (!Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000585 LayoutNonVirtualBase(PrimaryBase, 0);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000586 else
587 LayoutVirtualBase(PrimaryBase, 0);
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000588 }
589 }
590
591 // Layout the non-virtual bases.
592 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
593 E = RD->bases_end(); I != E; ++I) {
594 if (I->isVirtual())
595 continue;
596
597 const CXXRecordDecl *BaseDecl =
598 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
599
600 // We've already laid out the primary base.
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000601 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000602 continue;
603
Anders Carlssonfd88a612010-10-31 23:22:37 +0000604 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffsetInBits(BaseDecl));
Anders Carlssond681a292009-12-16 17:27:20 +0000605 }
606}
607
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000608void
609CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
610 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
611
612 uint64_t AlignedNonVirtualTypeSize =
613 llvm::RoundUpToAlignment(Layout.getNonVirtualSize(),
614 Layout.getNonVirtualAlign()) / 8;
615
616
617 // First check if we can use the same fields as for the complete class.
618 if (AlignedNonVirtualTypeSize == Layout.getSize() / 8) {
619 NonVirtualBaseTypeIsSameAsCompleteType = true;
620 return;
621 }
622
623 NonVirtualBaseFieldTypes = FieldTypes;
624
625 // Check if we need padding.
626 uint64_t AlignedNextFieldOffset =
Anders Carlssonacf877b2010-11-28 23:06:23 +0000627 llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
628 getAlignmentAsLLVMStruct());
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000629
630 assert(AlignedNextFieldOffset <= AlignedNonVirtualTypeSize &&
631 "Size mismatch!");
632
633 if (AlignedNonVirtualTypeSize == AlignedNextFieldOffset) {
634 // We don't need any padding.
635 return;
636 }
637
638 uint64_t NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
639 NonVirtualBaseFieldTypes.push_back(getByteArrayType(NumBytes));
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000640}
641
Anders Carlsson307846f2009-07-23 03:17:50 +0000642bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
643 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000644 assert(Alignment && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000645
Anders Carlsson697f6592009-07-23 03:43:54 +0000646 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000647
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000648 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
649 if (RD)
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000650 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000651
Anders Carlsson307846f2009-07-23 03:17:50 +0000652 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000653
Mike Stump11289f42009-09-09 15:08:12 +0000654 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000655 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
656 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000657 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000658 "Could not layout fields even with a packed LLVM struct!");
659 return false;
660 }
661 }
662
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000663 if (RD) {
Anders Carlssona459adb2010-11-28 19:18:44 +0000664 // We've laid out the non-virtual bases and the fields, now compute the
665 // non-virtual base field types.
666 ComputeNonVirtualBaseType(RD);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000667
Anders Carlssona459adb2010-11-28 19:18:44 +0000668 // And lay out the virtual bases.
669 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
670 if (Layout.isPrimaryBaseVirtual())
671 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
672 LayoutVirtualBases(RD, Layout);
673 }
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000674
Anders Carlsson307846f2009-07-23 03:17:50 +0000675 // Append tail padding if necessary.
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000676 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000677
Anders Carlsson307846f2009-07-23 03:17:50 +0000678 return true;
679}
680
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000681void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
682 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000683
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000684 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000685 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000686
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000687 uint64_t AlignedNextFieldOffset =
Anders Carlssonacf877b2010-11-28 23:06:23 +0000688 llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
689 getAlignmentAsLLVMStruct());
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000690
691 if (AlignedNextFieldOffset == RecordSizeInBytes) {
692 // We don't need any padding.
693 return;
694 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000695
Anders Carlssond5d64132009-07-28 17:56:36 +0000696 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000697 AppendBytes(NumPadBytes);
698}
699
Mike Stump11289f42009-09-09 15:08:12 +0000700void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000701 const llvm::Type *FieldTy) {
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000702 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000703
Anders Carlsson307846f2009-07-23 03:17:50 +0000704 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000705
Anders Carlssond5d64132009-07-28 17:56:36 +0000706 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000707 BitsAvailableInLastField = 0;
708}
709
Mike Stump11289f42009-09-09 15:08:12 +0000710void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlssond74cad82010-12-04 23:53:18 +0000711 unsigned FieldAlignmentInBytes) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000712 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
713 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000714
Anders Carlsson307846f2009-07-23 03:17:50 +0000715 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000716 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlssond74cad82010-12-04 23:53:18 +0000717 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignmentInBytes);
Anders Carlsson307846f2009-07-23 03:17:50 +0000718
719 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
720 // Even with alignment, the field offset is not at the right place,
721 // insert padding.
722 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
723
724 AppendBytes(PaddingInBytes);
725 }
726}
727
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000728const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(uint64_t NumBytes) {
729 assert(NumBytes != 0 && "Empty byte array's aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000730
Owen Anderson41a75022009-08-13 21:57:51 +0000731 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000732 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000733 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000734
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000735 return Ty;
736}
737
738void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
739 if (NumBytes == 0)
740 return;
741
Anders Carlsson307846f2009-07-23 03:17:50 +0000742 // Append the padding field
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000743 AppendField(NextFieldOffsetInBytes, getByteArrayType(NumBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000744}
745
746unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000747 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000748 return 1;
Mike Stump11289f42009-09-09 15:08:12 +0000749
Anders Carlsson307846f2009-07-23 03:17:50 +0000750 return Types.getTargetData().getABITypeAlignment(Ty);
751}
752
Anders Carlssonacf877b2010-11-28 23:06:23 +0000753unsigned CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
754 if (Packed)
755 return 1;
756
757 unsigned MaxAlignment = 1;
758 for (size_t i = 0; i != FieldTypes.size(); ++i)
759 MaxAlignment = std::max(MaxAlignment, getTypeAlignment(FieldTypes[i]));
760
761 return MaxAlignment;
762}
763
John McCall614dbdc2010-08-22 21:01:12 +0000764void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000765 // This record already contains a member pointer.
John McCall614dbdc2010-08-22 21:01:12 +0000766 if (!IsZeroInitializable)
Anders Carlssond606de72009-08-23 01:25:01 +0000767 return;
Mike Stump11289f42009-09-09 15:08:12 +0000768
Anders Carlssond606de72009-08-23 01:25:01 +0000769 // Can only have member pointers if we're compiling C++.
770 if (!Types.getContext().getLangOptions().CPlusPlus)
771 return;
Mike Stump11289f42009-09-09 15:08:12 +0000772
Anders Carlssone8bfe412010-02-02 05:17:25 +0000773 T = Types.getContext().getBaseElementType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000774
Anders Carlssone8bfe412010-02-02 05:17:25 +0000775 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000776 if (!Types.getCXXABI().isZeroInitializable(MPT))
777 IsZeroInitializable = false;
Anders Carlssone8bfe412010-02-02 05:17:25 +0000778 } else if (const RecordType *RT = T->getAs<RecordType>()) {
779 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall614dbdc2010-08-22 21:01:12 +0000780 CheckZeroInitializable(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000781 }
Anders Carlssond606de72009-08-23 01:25:01 +0000782}
783
John McCall614dbdc2010-08-22 21:01:12 +0000784void CGRecordLayoutBuilder::CheckZeroInitializable(const CXXRecordDecl *RD) {
Anders Carlssonbe48c542010-05-18 16:51:41 +0000785 // This record already contains a member pointer.
John McCall614dbdc2010-08-22 21:01:12 +0000786 if (!IsZeroInitializable)
Anders Carlssonbe48c542010-05-18 16:51:41 +0000787 return;
788
Anders Carlsson62776152010-11-24 19:57:04 +0000789 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
John McCall614dbdc2010-08-22 21:01:12 +0000790 if (!Layout.isZeroInitializable())
791 IsZeroInitializable = false;
Anders Carlssonbe48c542010-05-18 16:51:41 +0000792}
793
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000794CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
795 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000796
Anders Carlsson307846f2009-07-23 03:17:50 +0000797 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000798
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000799 const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
800 Builder.FieldTypes,
801 Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000802
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000803 const llvm::StructType *BaseTy = 0;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000804 if (isa<CXXRecordDecl>(D)) {
805 if (Builder.NonVirtualBaseTypeIsSameAsCompleteType)
806 BaseTy = Ty;
807 else if (!Builder.NonVirtualBaseFieldTypes.empty())
808 BaseTy = llvm::StructType::get(getLLVMContext(),
809 Builder.NonVirtualBaseFieldTypes,
810 Builder.Packed);
811 }
812
Daniel Dunbar034299e2010-03-31 01:09:11 +0000813 CGRecordLayout *RL =
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000814 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable);
Daniel Dunbar034299e2010-03-31 01:09:11 +0000815
Anders Carlsson061ca522010-05-18 05:22:06 +0000816 // Add all the non-virtual base field numbers.
817 RL->NonVirtualBaseFields.insert(Builder.LLVMNonVirtualBases.begin(),
818 Builder.LLVMNonVirtualBases.end());
819
Anders Carlsson307846f2009-07-23 03:17:50 +0000820 // Add all the field numbers.
Anders Carlsson061ca522010-05-18 05:22:06 +0000821 RL->FieldInfo.insert(Builder.LLVMFields.begin(),
822 Builder.LLVMFields.end());
Anders Carlsson307846f2009-07-23 03:17:50 +0000823
824 // Add bitfield info.
Anders Carlsson061ca522010-05-18 05:22:06 +0000825 RL->BitFields.insert(Builder.LLVMBitFields.begin(),
826 Builder.LLVMBitFields.end());
Mike Stump11289f42009-09-09 15:08:12 +0000827
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000828 // Dump the layout, if requested.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000829 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000830 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000831 llvm::errs() << "Record: ";
832 D->dump();
833 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000834 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000835 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000836
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000837#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000838 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000839 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
840
841 uint64_t TypeSizeInBits = Layout.getSize();
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000842 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000843 "Type size mismatch!");
844
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000845 if (BaseTy) {
846 uint64_t AlignedNonVirtualTypeSizeInBits =
847 llvm::RoundUpToAlignment(Layout.getNonVirtualSize(),
848 Layout.getNonVirtualAlign());
849
850 assert(AlignedNonVirtualTypeSizeInBits ==
851 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
852 "Type size mismatch!");
853 }
854
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000855 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000856 const llvm::StructType *ST =
857 dyn_cast<llvm::StructType>(RL->getLLVMType());
858 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
859
860 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
861 RecordDecl::field_iterator it = D->field_begin();
862 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
863 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000864
865 // For non-bit-fields, just check that the LLVM struct offset matches the
866 // AST offset.
867 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000868 unsigned FieldNo = RL->getLLVMFieldNo(FD);
869 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
870 "Invalid field offset!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000871 continue;
872 }
873
874 // Ignore unnamed bit-fields.
875 if (!FD->getDeclName())
876 continue;
877
878 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
879 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
880 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
881
882 // Verify that every component access is within the structure.
883 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
884 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
885 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
886 "Invalid bit-field access (out of range)!");
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000887 }
888 }
889#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000890
Daniel Dunbar034299e2010-03-31 01:09:11 +0000891 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +0000892}
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000893
894void CGRecordLayout::print(llvm::raw_ostream &OS) const {
895 OS << "<CGRecordLayout\n";
896 OS << " LLVMType:" << *LLVMType << "\n";
Anders Carlssona7dd96c2010-11-21 23:59:45 +0000897 if (NonVirtualBaseLLVMType)
898 OS << " NonVirtualBaseLLVMType:" << *NonVirtualBaseLLVMType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +0000899 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000900 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000901
902 // Print bit-field infos in declaration order.
903 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000904 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
905 it = BitFields.begin(), ie = BitFields.end();
906 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000907 const RecordDecl *RD = it->first->getParent();
908 unsigned Index = 0;
909 for (RecordDecl::field_iterator
910 it2 = RD->field_begin(); *it2 != it->first; ++it2)
911 ++Index;
912 BFIs.push_back(std::make_pair(Index, &it->second));
913 }
914 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
915 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +0000916 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000917 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000918 OS << "\n";
919 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000920
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000921 OS << "]>\n";
922}
923
924void CGRecordLayout::dump() const {
925 print(llvm::errs());
926}
927
928void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
929 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000930 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000931 OS << " IsSigned:" << IsSigned << "\n";
932
933 OS.indent(4 + strlen("<CGBitFieldInfo"));
934 OS << " NumComponents:" << getNumComponents();
935 OS << " Components: [";
936 if (getNumComponents()) {
937 OS << "\n";
938 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
939 const AccessInfo &AI = getComponent(i);
940 OS.indent(8);
941 OS << "<AccessInfo"
942 << " FieldIndex:" << AI.FieldIndex
943 << " FieldByteOffset:" << AI.FieldByteOffset
944 << " FieldBitStart:" << AI.FieldBitStart
945 << " AccessWidth:" << AI.AccessWidth << "\n";
946 OS.indent(8 + strlen("<AccessInfo"));
947 OS << " AccessAlignment:" << AI.AccessAlignment
948 << " TargetBitOffset:" << AI.TargetBitOffset
949 << " TargetBitWidth:" << AI.TargetBitWidth
950 << ">\n";
951 }
952 OS.indent(4);
953 }
954 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000955}
956
957void CGBitFieldInfo::dump() const {
958 print(llvm::errs());
959}