blob: d5f50281ab903779e4f2a641566c26b4f9a26b55 [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
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000031namespace clang {
32namespace CodeGen {
33
34class CGRecordLayoutBuilder {
35public:
36 /// FieldTypes - Holds the LLVM types that the struct is created from.
37 std::vector<const llvm::Type *> FieldTypes;
38
Anders Carlssonc1351ca2010-11-09 05:25:47 +000039 /// NonVirtualBaseFieldTypes - Holds the LLVM types for the non-virtual part
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 }
51 std::vector<const llvm::Type *> NonVirtualBaseFieldTypes;
52
53 /// NonVirtualBaseTypeIsSameAsCompleteType - Whether the non-virtual part of
54 /// the struct is equivalent to the complete struct.
55 bool NonVirtualBaseTypeIsSameAsCompleteType;
56
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000057 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
58 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
59 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
60
61 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
Daniel Dunbard4549102010-04-06 01:07:41 +000062 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000063 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
64
Anders Carlsson061ca522010-05-18 05:22:06 +000065 typedef std::pair<const CXXRecordDecl *, unsigned> LLVMBaseInfo;
66 llvm::SmallVector<LLVMBaseInfo, 16> LLVMNonVirtualBases;
Anders Carlsson4131f002010-11-24 22:50:27 +000067
68 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
69 /// primary base classes for some other direct or indirect base class.
70 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
71
Anders Carlssona459adb2010-11-28 19:18:44 +000072 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
73 /// avoid laying out virtual bases more than once.
74 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
75
John McCall614dbdc2010-08-22 21:01:12 +000076 /// IsZeroInitializable - Whether this struct can be C++
77 /// zero-initialized with an LLVM zeroinitializer.
78 bool IsZeroInitializable;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000079
80 /// Packed - Whether the resulting LLVM struct will be packed or not.
81 bool Packed;
82
83private:
84 CodeGenTypes &Types;
85
86 /// Alignment - Contains the alignment of the RecordDecl.
87 //
88 // FIXME: This is not needed and should be removed.
89 unsigned Alignment;
90
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000091 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
92 /// this will have the number of bits still available in the field.
93 char BitsAvailableInLastField;
94
95 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
96 uint64_t NextFieldOffsetInBytes;
97
Anders Carlsson1de2f572010-04-17 20:49:27 +000098 /// LayoutUnionField - Will layout a field in an union and return the type
99 /// that the field will have.
100 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
101 const ASTRecordLayout &Layout);
102
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000103 /// LayoutUnion - Will layout a union RecordDecl.
104 void LayoutUnion(const RecordDecl *D);
105
106 /// LayoutField - try to layout all fields in the record decl.
107 /// Returns false if the operation failed because the struct is not packed.
108 bool LayoutFields(const RecordDecl *D);
109
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000110 /// LayoutVirtualBase - layout a single virtual base.
111 void LayoutVirtualBase(const CXXRecordDecl *BaseDecl, uint64_t BaseOffset);
112
Anders Carlssona459adb2010-11-28 19:18:44 +0000113 /// LayoutVirtualBases - layout the virtual bases of a record decl.
114 void LayoutVirtualBases(const CXXRecordDecl *RD,
115 const ASTRecordLayout &Layout);
116
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000117 /// LayoutNonVirtualBase - layout a single non-virtual base.
118 void LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
119 uint64_t BaseOffset);
120
Anders Carlssona459adb2010-11-28 19:18:44 +0000121 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000122 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
123 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000124
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000125 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
126 void ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
127
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000128 /// LayoutField - layout a single field. Returns false if the operation failed
129 /// because the current struct is not packed.
130 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
131
132 /// LayoutBitField - layout a single bit field.
133 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
134
135 /// AppendField - Appends a field with the given offset and type.
136 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
137
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000138 /// AppendPadding - Appends enough padding bytes so that the total
139 /// struct size is a multiple of the field alignment.
140 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
141
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}
175}
176
Anders Carlsson307846f2009-07-23 03:17:50 +0000177void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000178 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlsson09a37742009-09-02 17:51:33 +0000179 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000180
Anders Carlsson697f6592009-07-23 03:43:54 +0000181 if (D->isUnion()) {
182 LayoutUnion(D);
183 return;
184 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000185
Anders Carlsson307846f2009-07-23 03:17:50 +0000186 if (LayoutFields(D))
187 return;
Mike Stump11289f42009-09-09 15:08:12 +0000188
Anders Carlsson307846f2009-07-23 03:17:50 +0000189 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000190 Packed = true;
Anders Carlssond5d64132009-07-28 17:56:36 +0000191 NextFieldOffsetInBytes = 0;
Anders Carlsson307846f2009-07-23 03:17:50 +0000192 FieldTypes.clear();
Anders Carlsson307846f2009-07-23 03:17:50 +0000193 LLVMFields.clear();
194 LLVMBitFields.clear();
Anders Carlsson061ca522010-05-18 05:22:06 +0000195 LLVMNonVirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000196
Anders Carlsson307846f2009-07-23 03:17:50 +0000197 LayoutFields(D);
198}
199
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000200CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
201 const FieldDecl *FD,
202 uint64_t FieldOffset,
203 uint64_t FieldSize,
204 uint64_t ContainingTypeSizeInBits,
205 unsigned ContainingTypeAlign) {
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000206 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarb935b932010-04-13 20:58:55 +0000207 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
208 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000209
210 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000211
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000212 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000213 // We have a wide bit-field. The extra bits are only used for padding, so
214 // if we have a bitfield of type T, with size N:
215 //
216 // T t : N;
217 //
218 // We can just assume that it's:
219 //
220 // T t : sizeof(T);
221 //
222 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000223 }
224
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000225 // Compute the access components. The policy we use is to start by attempting
226 // to access using the width of the bit-field type itself and to always access
227 // at aligned indices of that type. If such an access would fail because it
228 // extends past the bound of the type, then we reduce size to the next smaller
229 // power of two and retry. The current algorithm assumes pow2 sized types,
230 // although this is easy to fix.
231 //
232 // FIXME: This algorithm is wrong on big-endian systems, I think.
233 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
234 CGBitFieldInfo::AccessInfo Components[3];
235 unsigned NumComponents = 0;
236 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
237 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000238
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000239 // Round down from the field offset to find the first access position that is
240 // at an aligned offset of the initial access type.
Daniel Dunbar59813772010-04-22 15:22:33 +0000241 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
242
243 // Adjust initial access size to fit within record.
244 while (AccessWidth > 8 &&
245 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
246 AccessWidth >>= 1;
247 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
248 }
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000249
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000250 while (AccessedTargetBits < FieldSize) {
251 // Check that we can access using a type of this size, without reading off
252 // the end of the structure. This can occur with packed structures and
253 // -fno-bitfield-type-align, for example.
254 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
255 // If so, reduce access size to the next smaller power-of-two and retry.
256 AccessWidth >>= 1;
257 assert(AccessWidth >= 8 && "Cannot access under byte size!");
258 continue;
259 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000260
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000261 // Otherwise, add an access component.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000262
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000263 // First, compute the bits inside this access which are part of the
264 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
265 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
266 // in the target that we are reading.
Daniel Dunbar59813772010-04-22 15:22:33 +0000267 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
268 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000269 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
270 uint64_t AccessBitsInFieldSize =
Daniel Dunbar59813772010-04-22 15:22:33 +0000271 std::min(AccessWidth + AccessStart,
272 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar5d6c07e2010-04-22 14:56:10 +0000273
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000274 assert(NumComponents < 3 && "Unexpected number of components!");
275 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
276 AI.FieldIndex = 0;
277 // FIXME: We still follow the old access pattern of only using the field
278 // byte offset. We should switch this once we fix the struct layout to be
279 // pretty.
280 AI.FieldByteOffset = AccessStart / 8;
281 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
282 AI.AccessWidth = AccessWidth;
Daniel Dunbarfc66e0e2010-04-22 03:17:04 +0000283 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000284 AI.TargetBitOffset = AccessedTargetBits;
285 AI.TargetBitWidth = AccessBitsInFieldSize;
286
287 AccessStart += AccessWidth;
288 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000289 }
290
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000291 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000292 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000293}
294
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000295CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
296 const FieldDecl *FD,
297 uint64_t FieldOffset,
298 uint64_t FieldSize) {
299 const RecordDecl *RD = FD->getParent();
300 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
301 uint64_t ContainingTypeSizeInBits = RL.getSize();
302 unsigned ContainingTypeAlign = RL.getAlignment();
303
304 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
305 ContainingTypeAlign);
306}
307
Anders Carlsson307846f2009-07-23 03:17:50 +0000308void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
309 uint64_t FieldOffset) {
Mike Stump11289f42009-09-09 15:08:12 +0000310 uint64_t FieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000311 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000312
Anders Carlsson307846f2009-07-23 03:17:50 +0000313 if (FieldSize == 0)
314 return;
315
Anders Carlssond5d64132009-07-28 17:56:36 +0000316 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000317 unsigned NumBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000318
Anders Carlsson307846f2009-07-23 03:17:50 +0000319 if (FieldOffset < NextFieldOffset) {
320 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +0000321 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump11289f42009-09-09 15:08:12 +0000322
Anders Carlsson307846f2009-07-23 03:17:50 +0000323 // The bitfield begins in the previous bit-field.
Mike Stump11289f42009-09-09 15:08:12 +0000324 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000325 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
326 } else {
327 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
328
329 // Append padding if necessary.
330 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump11289f42009-09-09 15:08:12 +0000331
332 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000333 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000334
Anders Carlsson307846f2009-07-23 03:17:50 +0000335 assert(NumBytesToAppend && "No bytes to append!");
336 }
337
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000338 // Add the bit field info.
339 LLVMBitFields.push_back(
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000340 LLVMBitFieldInfo(D, CGBitFieldInfo::MakeInfo(Types, D, FieldOffset,
341 FieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000342
Anders Carlsson307846f2009-07-23 03:17:50 +0000343 AppendBytes(NumBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000344
Mike Stump11289f42009-09-09 15:08:12 +0000345 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +0000346 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000347}
348
349bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
350 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000351 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000352 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000353 return false;
354
355 if (D->isBitField()) {
356 // We must use packed structs for unnamed bit fields since they
357 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000358 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000359 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000360
Anders Carlsson307846f2009-07-23 03:17:50 +0000361 LayoutBitField(D, FieldOffset);
362 return true;
363 }
Mike Stump11289f42009-09-09 15:08:12 +0000364
John McCall614dbdc2010-08-22 21:01:12 +0000365 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000366
Anders Carlsson307846f2009-07-23 03:17:50 +0000367 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000368 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000369
Anders Carlsson19702bb2009-08-04 16:29:15 +0000370 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
371 unsigned TypeAlignment = getTypeAlignment(Ty);
372
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000373 // If the type alignment is larger then the struct alignment, we must use
374 // a packed struct.
375 if (TypeAlignment > Alignment) {
376 assert(!Packed && "Alignment is wrong even with packed struct!");
377 return false;
378 }
Mike Stump11289f42009-09-09 15:08:12 +0000379
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000380 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
381 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
Daniel Dunbar40130442010-05-27 01:12:46 +0000382 if (const MaxFieldAlignmentAttr *MFAA =
383 RD->getAttr<MaxFieldAlignmentAttr>()) {
384 if (MFAA->getAlignment() != TypeAlignment * 8 && !Packed)
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000385 return false;
386 }
387 }
388
Anders Carlsson19702bb2009-08-04 16:29:15 +0000389 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000390 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson19702bb2009-08-04 16:29:15 +0000391 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
392
393 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
394 assert(!Packed && "Could not place field even with packed struct!");
395 return false;
396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Anders Carlsson19702bb2009-08-04 16:29:15 +0000398 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
399 // Even with alignment, the field offset is not at the right place,
400 // insert padding.
401 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump11289f42009-09-09 15:08:12 +0000402
Anders Carlsson19702bb2009-08-04 16:29:15 +0000403 AppendBytes(PaddingInBytes);
404 }
Mike Stump11289f42009-09-09 15:08:12 +0000405
Anders Carlsson307846f2009-07-23 03:17:50 +0000406 // Now append the field.
407 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000408 AppendField(FieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000409
Anders Carlsson307846f2009-07-23 03:17:50 +0000410 return true;
411}
412
Anders Carlsson1de2f572010-04-17 20:49:27 +0000413const llvm::Type *
414CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
415 const ASTRecordLayout &Layout) {
416 if (Field->isBitField()) {
417 uint64_t FieldSize =
418 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
419
420 // Ignore zero sized bit fields.
421 if (FieldSize == 0)
422 return 0;
423
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000424 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
425 unsigned NumBytesToAppend =
426 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlsson2295f132010-04-17 21:04:52 +0000427
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000428 if (NumBytesToAppend > 1)
429 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlsson2295f132010-04-17 21:04:52 +0000430
Anders Carlsson1de2f572010-04-17 20:49:27 +0000431 // Add the bit field info.
432 LLVMBitFields.push_back(
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000433 LLVMBitFieldInfo(Field, CGBitFieldInfo::MakeInfo(Types, Field,
434 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000435 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000436 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000437
Anders Carlsson1de2f572010-04-17 20:49:27 +0000438 // This is a regular union field.
439 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
440 return Types.ConvertTypeForMemRecursive(Field->getType());
441}
442
Anders Carlsson697f6592009-07-23 03:43:54 +0000443void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
444 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000445
Anders Carlsson697f6592009-07-23 03:43:54 +0000446 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000447
Anders Carlsson697f6592009-07-23 03:43:54 +0000448 const llvm::Type *Ty = 0;
449 uint64_t Size = 0;
450 unsigned Align = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000451
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000452 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000453
Anders Carlsson697f6592009-07-23 03:43:54 +0000454 unsigned FieldNo = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000455 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson697f6592009-07-23 03:43:54 +0000456 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump11289f42009-09-09 15:08:12 +0000457 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000458 "Union field offset did not start at the beginning of record!");
Anders Carlsson1de2f572010-04-17 20:49:27 +0000459 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000460
Anders Carlsson1de2f572010-04-17 20:49:27 +0000461 if (!FieldTy)
462 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000463
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000464 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000465
Anders Carlssone2accf42009-07-23 21:52:03 +0000466 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
467 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump11289f42009-09-09 15:08:12 +0000468
Anders Carlsson697f6592009-07-23 03:43:54 +0000469 if (FieldAlign < Align)
470 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000471
Anders Carlsson697f6592009-07-23 03:43:54 +0000472 if (FieldAlign > Align || FieldSize > Size) {
473 Ty = FieldTy;
474 Align = FieldAlign;
475 Size = FieldSize;
476 }
477 }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Anders Carlsson697f6592009-07-23 03:43:54 +0000479 // Now add our field.
Anders Carlsson0e912752009-09-03 22:56:02 +0000480 if (Ty) {
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000481 AppendField(0, Ty);
Anders Carlsson0e912752009-09-03 22:56:02 +0000482
483 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
484 // We need a packed struct.
485 Packed = true;
486 Align = 1;
487 }
488 }
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000489 if (!Align) {
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000490 assert(HasOnlyZeroSizedBitFields &&
491 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000492 Align = 1;
493 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000494
Anders Carlsson697f6592009-07-23 03:43:54 +0000495 // Append tail padding.
496 if (Layout.getSize() / 8 > Size)
497 AppendPadding(Layout.getSize() / 8, Align);
498}
499
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000500void
501CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *BaseDecl,
502 uint64_t BaseOffset) {
503 // Ignore empty bases.
504 if (BaseDecl->isEmpty())
505 return;
506
507 CheckZeroInitializable(BaseDecl);
508
509 const ASTRecordLayout &Layout =
510 Types.getContext().getASTRecordLayout(BaseDecl);
511
512 uint64_t NonVirtualSize = Layout.getNonVirtualSize();
513
514 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
515 AppendPadding(BaseOffset / 8, 1);
516
517 // FIXME: Add the vbase field info.
518
519 AppendBytes(NonVirtualSize / 8);
520
521}
522
Anders Carlssona459adb2010-11-28 19:18:44 +0000523/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
524void
525CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
526 const ASTRecordLayout &Layout) {
527 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
528 E = RD->bases_end(); I != E; ++I) {
529 const CXXRecordDecl *BaseDecl =
530 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
531
532 // We only want to lay out virtual bases that aren't indirect primary bases
533 // of some other base.
534 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
535 // Only lay out the base once.
536 if (!LaidOutVirtualBases.insert(BaseDecl))
537 continue;
538
539 uint64_t VBaseOffset = Layout.getVBaseClassOffsetInBits(BaseDecl);
540 LayoutVirtualBase(BaseDecl, VBaseOffset);
541 }
542
543 if (!BaseDecl->getNumVBases()) {
544 // This base isn't interesting since it doesn't have any virtual bases.
545 continue;
546 }
547
548 LayoutVirtualBases(BaseDecl, Layout);
549 }
550}
551
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000552void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
553 uint64_t BaseOffset) {
Anders Carlsson39a6b222010-11-22 00:03:08 +0000554 // Ignore empty bases.
555 if (BaseDecl->isEmpty())
556 return;
557
558 CheckZeroInitializable(BaseDecl);
559
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000560 const ASTRecordLayout &Layout =
561 Types.getContext().getASTRecordLayout(BaseDecl);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000562
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000563 uint64_t NonVirtualSize = Layout.getNonVirtualSize();
564
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000565 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
566 AppendPadding(BaseOffset / 8, 1);
Anders Carlsson061ca522010-05-18 05:22:06 +0000567
568 // Append the base field.
569 LLVMNonVirtualBases.push_back(LLVMBaseInfo(BaseDecl, FieldTypes.size()));
570
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000571 AppendBytes(NonVirtualSize / 8);
572}
573
574void
575CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
576 const ASTRecordLayout &Layout) {
577 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
578
579 // Check if we need to add a vtable pointer.
580 if (RD->isDynamicClass()) {
581 if (!PrimaryBase) {
582 const llvm::Type *FunctionType =
583 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
584 /*isVarArg=*/true);
585 const llvm::Type *VTableTy = FunctionType->getPointerTo();
586
587 assert(NextFieldOffsetInBytes == 0 &&
588 "VTable pointer must come first!");
589 AppendField(NextFieldOffsetInBytes, VTableTy->getPointerTo());
590 } else {
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000591 if (!Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000592 LayoutNonVirtualBase(PrimaryBase, 0);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000593 else
594 LayoutVirtualBase(PrimaryBase, 0);
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000595 }
596 }
597
598 // Layout the non-virtual bases.
599 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
600 E = RD->bases_end(); I != E; ++I) {
601 if (I->isVirtual())
602 continue;
603
604 const CXXRecordDecl *BaseDecl =
605 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
606
607 // We've already laid out the primary base.
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000608 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000609 continue;
610
Anders Carlssonfd88a612010-10-31 23:22:37 +0000611 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffsetInBits(BaseDecl));
Anders Carlssond681a292009-12-16 17:27:20 +0000612 }
613}
614
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000615void
616CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
617 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
618
619 uint64_t AlignedNonVirtualTypeSize =
620 llvm::RoundUpToAlignment(Layout.getNonVirtualSize(),
621 Layout.getNonVirtualAlign()) / 8;
622
623
624 // First check if we can use the same fields as for the complete class.
625 if (AlignedNonVirtualTypeSize == Layout.getSize() / 8) {
626 NonVirtualBaseTypeIsSameAsCompleteType = true;
627 return;
628 }
629
630 NonVirtualBaseFieldTypes = FieldTypes;
631
632 // Check if we need padding.
633 uint64_t AlignedNextFieldOffset =
Anders Carlssonacf877b2010-11-28 23:06:23 +0000634 llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
635 getAlignmentAsLLVMStruct());
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000636
637 assert(AlignedNextFieldOffset <= AlignedNonVirtualTypeSize &&
638 "Size mismatch!");
639
640 if (AlignedNonVirtualTypeSize == AlignedNextFieldOffset) {
641 // We don't need any padding.
642 return;
643 }
644
645 uint64_t NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
646 NonVirtualBaseFieldTypes.push_back(getByteArrayType(NumBytes));
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000647}
648
Anders Carlsson307846f2009-07-23 03:17:50 +0000649bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
650 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000651 assert(Alignment && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000652
Anders Carlsson697f6592009-07-23 03:43:54 +0000653 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000654
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000655 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
656 if (RD)
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000657 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000658
Anders Carlsson307846f2009-07-23 03:17:50 +0000659 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000660
Mike Stump11289f42009-09-09 15:08:12 +0000661 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000662 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
663 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000664 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000665 "Could not layout fields even with a packed LLVM struct!");
666 return false;
667 }
668 }
669
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000670 if (RD) {
Anders Carlssona459adb2010-11-28 19:18:44 +0000671 // We've laid out the non-virtual bases and the fields, now compute the
672 // non-virtual base field types.
673 ComputeNonVirtualBaseType(RD);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000674
Anders Carlssona459adb2010-11-28 19:18:44 +0000675 // And lay out the virtual bases.
676 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
677 if (Layout.isPrimaryBaseVirtual())
678 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
679 LayoutVirtualBases(RD, Layout);
680 }
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000681
Anders Carlsson307846f2009-07-23 03:17:50 +0000682 // Append tail padding if necessary.
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000683 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000684
Anders Carlsson307846f2009-07-23 03:17:50 +0000685 return true;
686}
687
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000688void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
689 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000690
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000691 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000692 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000693
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000694 uint64_t AlignedNextFieldOffset =
Anders Carlssonacf877b2010-11-28 23:06:23 +0000695 llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
696 getAlignmentAsLLVMStruct());
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000697
698 if (AlignedNextFieldOffset == RecordSizeInBytes) {
699 // We don't need any padding.
700 return;
701 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000702
Anders Carlssond5d64132009-07-28 17:56:36 +0000703 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000704 AppendBytes(NumPadBytes);
705}
706
Mike Stump11289f42009-09-09 15:08:12 +0000707void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000708 const llvm::Type *FieldTy) {
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000709 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000710
Anders Carlsson307846f2009-07-23 03:17:50 +0000711 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000712
Anders Carlssond5d64132009-07-28 17:56:36 +0000713 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000714 BitsAvailableInLastField = 0;
715}
716
Mike Stump11289f42009-09-09 15:08:12 +0000717void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000718 unsigned FieldAlignment) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000719 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
720 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000721
Anders Carlsson307846f2009-07-23 03:17:50 +0000722 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000723 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson307846f2009-07-23 03:17:50 +0000724 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
725
726 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
727 // Even with alignment, the field offset is not at the right place,
728 // insert padding.
729 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
730
731 AppendBytes(PaddingInBytes);
732 }
733}
734
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000735const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(uint64_t NumBytes) {
736 assert(NumBytes != 0 && "Empty byte array's aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000737
Owen Anderson41a75022009-08-13 21:57:51 +0000738 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000739 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000740 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000741
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000742 return Ty;
743}
744
745void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
746 if (NumBytes == 0)
747 return;
748
Anders Carlsson307846f2009-07-23 03:17:50 +0000749 // Append the padding field
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000750 AppendField(NextFieldOffsetInBytes, getByteArrayType(NumBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000751}
752
753unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000754 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000755 return 1;
Mike Stump11289f42009-09-09 15:08:12 +0000756
Anders Carlsson307846f2009-07-23 03:17:50 +0000757 return Types.getTargetData().getABITypeAlignment(Ty);
758}
759
Anders Carlssonacf877b2010-11-28 23:06:23 +0000760unsigned CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
761 if (Packed)
762 return 1;
763
764 unsigned MaxAlignment = 1;
765 for (size_t i = 0; i != FieldTypes.size(); ++i)
766 MaxAlignment = std::max(MaxAlignment, getTypeAlignment(FieldTypes[i]));
767
768 return MaxAlignment;
769}
770
John McCall614dbdc2010-08-22 21:01:12 +0000771void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000772 // This record already contains a member pointer.
John McCall614dbdc2010-08-22 21:01:12 +0000773 if (!IsZeroInitializable)
Anders Carlssond606de72009-08-23 01:25:01 +0000774 return;
Mike Stump11289f42009-09-09 15:08:12 +0000775
Anders Carlssond606de72009-08-23 01:25:01 +0000776 // Can only have member pointers if we're compiling C++.
777 if (!Types.getContext().getLangOptions().CPlusPlus)
778 return;
Mike Stump11289f42009-09-09 15:08:12 +0000779
Anders Carlssone8bfe412010-02-02 05:17:25 +0000780 T = Types.getContext().getBaseElementType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000781
Anders Carlssone8bfe412010-02-02 05:17:25 +0000782 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000783 if (!Types.getCXXABI().isZeroInitializable(MPT))
784 IsZeroInitializable = false;
Anders Carlssone8bfe412010-02-02 05:17:25 +0000785 } else if (const RecordType *RT = T->getAs<RecordType>()) {
786 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall614dbdc2010-08-22 21:01:12 +0000787 CheckZeroInitializable(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000788 }
Anders Carlssond606de72009-08-23 01:25:01 +0000789}
790
John McCall614dbdc2010-08-22 21:01:12 +0000791void CGRecordLayoutBuilder::CheckZeroInitializable(const CXXRecordDecl *RD) {
Anders Carlssonbe48c542010-05-18 16:51:41 +0000792 // This record already contains a member pointer.
John McCall614dbdc2010-08-22 21:01:12 +0000793 if (!IsZeroInitializable)
Anders Carlssonbe48c542010-05-18 16:51:41 +0000794 return;
795
Anders Carlsson62776152010-11-24 19:57:04 +0000796 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
John McCall614dbdc2010-08-22 21:01:12 +0000797 if (!Layout.isZeroInitializable())
798 IsZeroInitializable = false;
Anders Carlssonbe48c542010-05-18 16:51:41 +0000799}
800
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000801CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
802 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000803
Anders Carlsson307846f2009-07-23 03:17:50 +0000804 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000805
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000806 const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
807 Builder.FieldTypes,
808 Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000809
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000810 const llvm::StructType *BaseTy = 0;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000811 if (isa<CXXRecordDecl>(D)) {
812 if (Builder.NonVirtualBaseTypeIsSameAsCompleteType)
813 BaseTy = Ty;
814 else if (!Builder.NonVirtualBaseFieldTypes.empty())
815 BaseTy = llvm::StructType::get(getLLVMContext(),
816 Builder.NonVirtualBaseFieldTypes,
817 Builder.Packed);
818 }
819
Daniel Dunbar034299e2010-03-31 01:09:11 +0000820 CGRecordLayout *RL =
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000821 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable);
Daniel Dunbar034299e2010-03-31 01:09:11 +0000822
Anders Carlsson061ca522010-05-18 05:22:06 +0000823 // Add all the non-virtual base field numbers.
824 RL->NonVirtualBaseFields.insert(Builder.LLVMNonVirtualBases.begin(),
825 Builder.LLVMNonVirtualBases.end());
826
Anders Carlsson307846f2009-07-23 03:17:50 +0000827 // Add all the field numbers.
Anders Carlsson061ca522010-05-18 05:22:06 +0000828 RL->FieldInfo.insert(Builder.LLVMFields.begin(),
829 Builder.LLVMFields.end());
Anders Carlsson307846f2009-07-23 03:17:50 +0000830
831 // Add bitfield info.
Anders Carlsson061ca522010-05-18 05:22:06 +0000832 RL->BitFields.insert(Builder.LLVMBitFields.begin(),
833 Builder.LLVMBitFields.end());
Mike Stump11289f42009-09-09 15:08:12 +0000834
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000835 // Dump the layout, if requested.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000836 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000837 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000838 llvm::errs() << "Record: ";
839 D->dump();
840 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000841 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000842 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000843
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000844#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000845 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000846 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
847
848 uint64_t TypeSizeInBits = Layout.getSize();
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000849 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000850 "Type size mismatch!");
851
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000852 if (BaseTy) {
853 uint64_t AlignedNonVirtualTypeSizeInBits =
854 llvm::RoundUpToAlignment(Layout.getNonVirtualSize(),
855 Layout.getNonVirtualAlign());
856
857 assert(AlignedNonVirtualTypeSizeInBits ==
858 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
859 "Type size mismatch!");
860 }
861
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000862 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000863 const llvm::StructType *ST =
864 dyn_cast<llvm::StructType>(RL->getLLVMType());
865 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
866
867 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
868 RecordDecl::field_iterator it = D->field_begin();
869 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
870 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000871
872 // For non-bit-fields, just check that the LLVM struct offset matches the
873 // AST offset.
874 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000875 unsigned FieldNo = RL->getLLVMFieldNo(FD);
876 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
877 "Invalid field offset!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000878 continue;
879 }
880
881 // Ignore unnamed bit-fields.
882 if (!FD->getDeclName())
883 continue;
884
885 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
886 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
887 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
888
889 // Verify that every component access is within the structure.
890 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
891 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
892 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
893 "Invalid bit-field access (out of range)!");
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000894 }
895 }
896#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000897
Daniel Dunbar034299e2010-03-31 01:09:11 +0000898 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +0000899}
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000900
901void CGRecordLayout::print(llvm::raw_ostream &OS) const {
902 OS << "<CGRecordLayout\n";
903 OS << " LLVMType:" << *LLVMType << "\n";
Anders Carlssona7dd96c2010-11-21 23:59:45 +0000904 if (NonVirtualBaseLLVMType)
905 OS << " NonVirtualBaseLLVMType:" << *NonVirtualBaseLLVMType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +0000906 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000907 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000908
909 // Print bit-field infos in declaration order.
910 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000911 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
912 it = BitFields.begin(), ie = BitFields.end();
913 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000914 const RecordDecl *RD = it->first->getParent();
915 unsigned Index = 0;
916 for (RecordDecl::field_iterator
917 it2 = RD->field_begin(); *it2 != it->first; ++it2)
918 ++Index;
919 BFIs.push_back(std::make_pair(Index, &it->second));
920 }
921 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
922 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +0000923 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000924 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000925 OS << "\n";
926 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000927
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000928 OS << "]>\n";
929}
930
931void CGRecordLayout::dump() const {
932 print(llvm::errs());
933}
934
935void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
936 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000937 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000938 OS << " IsSigned:" << IsSigned << "\n";
939
940 OS.indent(4 + strlen("<CGBitFieldInfo"));
941 OS << " NumComponents:" << getNumComponents();
942 OS << " Components: [";
943 if (getNumComponents()) {
944 OS << "\n";
945 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
946 const AccessInfo &AI = getComponent(i);
947 OS.indent(8);
948 OS << "<AccessInfo"
949 << " FieldIndex:" << AI.FieldIndex
950 << " FieldByteOffset:" << AI.FieldByteOffset
951 << " FieldBitStart:" << AI.FieldBitStart
952 << " AccessWidth:" << AI.AccessWidth << "\n";
953 OS.indent(8 + strlen("<AccessInfo"));
954 OS << " AccessAlignment:" << AI.AccessAlignment
955 << " TargetBitOffset:" << AI.TargetBitOffset
956 << " TargetBitWidth:" << AI.TargetBitWidth
957 << ">\n";
958 }
959 OS.indent(4);
960 }
961 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000962}
963
964void CGBitFieldInfo::dump() const {
965 print(llvm::errs());
966}