blob: c3f45a35fb4d2e6e86b4c414b14d2e22badc53e6 [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"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
20#include "CodeGenTypes.h"
John McCall614dbdc2010-08-22 21:01:12 +000021#include "CGCXXABI.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000022#include "llvm/DerivedTypes.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000023#include "llvm/Type.h"
Daniel Dunbar2ba67442010-04-21 19:10:49 +000024#include "llvm/Support/Debug.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000025#include "llvm/Support/raw_ostream.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000026#include "llvm/Target/TargetData.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000027using namespace clang;
28using namespace CodeGen;
29
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000030namespace clang {
31namespace CodeGen {
32
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;
66
John McCall614dbdc2010-08-22 21:01:12 +000067 /// IsZeroInitializable - Whether this struct can be C++
68 /// zero-initialized with an LLVM zeroinitializer.
69 bool IsZeroInitializable;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000070
71 /// Packed - Whether the resulting LLVM struct will be packed or not.
72 bool Packed;
73
74private:
75 CodeGenTypes &Types;
76
77 /// Alignment - Contains the alignment of the RecordDecl.
78 //
79 // FIXME: This is not needed and should be removed.
80 unsigned Alignment;
81
82 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the
83 /// LLVM types.
84 unsigned AlignmentAsLLVMStruct;
85
86 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
87 /// this will have the number of bits still available in the field.
88 char BitsAvailableInLastField;
89
90 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
91 uint64_t NextFieldOffsetInBytes;
92
Anders Carlsson1de2f572010-04-17 20:49:27 +000093 /// LayoutUnionField - Will layout a field in an union and return the type
94 /// that the field will have.
95 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
96 const ASTRecordLayout &Layout);
97
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000098 /// LayoutUnion - Will layout a union RecordDecl.
99 void LayoutUnion(const RecordDecl *D);
100
101 /// LayoutField - try to layout all fields in the record decl.
102 /// Returns false if the operation failed because the struct is not packed.
103 bool LayoutFields(const RecordDecl *D);
104
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000105 /// LayoutNonVirtualBase - layout a single non-virtual base.
106 void LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
107 uint64_t BaseOffset);
108
109 /// LayoutNonVirtualBases - layout the non-virtual bases of a record decl.
110 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
111 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000112
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000113 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
114 void ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
115
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000116 /// LayoutField - layout a single field. Returns false if the operation failed
117 /// because the current struct is not packed.
118 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
119
120 /// LayoutBitField - layout a single bit field.
121 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
122
123 /// AppendField - Appends a field with the given offset and type.
124 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
125
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000126 /// AppendPadding - Appends enough padding bytes so that the total
127 /// struct size is a multiple of the field alignment.
128 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
129
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000130 /// getByteArrayType - Returns a byte array type with the given number of
131 /// elements.
132 const llvm::Type *getByteArrayType(uint64_t NumBytes);
133
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000134 /// AppendBytes - Append a given number of bytes to the record.
135 void AppendBytes(uint64_t NumBytes);
136
137 /// AppendTailPadding - Append enough tail padding so that the type will have
138 /// the passed size.
139 void AppendTailPadding(uint64_t RecordSize);
140
141 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000142
John McCall614dbdc2010-08-22 21:01:12 +0000143 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000144 /// to data member.
John McCall614dbdc2010-08-22 21:01:12 +0000145 void CheckZeroInitializable(QualType T);
146 void CheckZeroInitializable(const CXXRecordDecl *RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000147
148public:
149 CGRecordLayoutBuilder(CodeGenTypes &Types)
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000150 : NonVirtualBaseTypeIsSameAsCompleteType(false), IsZeroInitializable(true),
151 Packed(false), Types(Types), Alignment(0), AlignmentAsLLVMStruct(1),
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000152 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
153
154 /// Layout - Will layout a RecordDecl.
155 void Layout(const RecordDecl *D);
156};
157
158}
159}
160
Anders Carlsson307846f2009-07-23 03:17:50 +0000161void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000162 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlsson09a37742009-09-02 17:51:33 +0000163 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000164
Anders Carlsson697f6592009-07-23 03:43:54 +0000165 if (D->isUnion()) {
166 LayoutUnion(D);
167 return;
168 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000169
Anders Carlsson307846f2009-07-23 03:17:50 +0000170 if (LayoutFields(D))
171 return;
Mike Stump11289f42009-09-09 15:08:12 +0000172
Anders Carlsson307846f2009-07-23 03:17:50 +0000173 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000174 Packed = true;
Anders Carlsson307846f2009-07-23 03:17:50 +0000175 AlignmentAsLLVMStruct = 1;
Anders Carlssond5d64132009-07-28 17:56:36 +0000176 NextFieldOffsetInBytes = 0;
Anders Carlsson307846f2009-07-23 03:17:50 +0000177 FieldTypes.clear();
Anders Carlsson307846f2009-07-23 03:17:50 +0000178 LLVMFields.clear();
179 LLVMBitFields.clear();
Anders Carlsson061ca522010-05-18 05:22:06 +0000180 LLVMNonVirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000181
Anders Carlsson307846f2009-07-23 03:17:50 +0000182 LayoutFields(D);
183}
184
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000185CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
186 const FieldDecl *FD,
187 uint64_t FieldOffset,
188 uint64_t FieldSize,
189 uint64_t ContainingTypeSizeInBits,
190 unsigned ContainingTypeAlign) {
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000191 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarb935b932010-04-13 20:58:55 +0000192 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
193 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000194
195 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000196
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000197 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000198 // We have a wide bit-field. The extra bits are only used for padding, so
199 // if we have a bitfield of type T, with size N:
200 //
201 // T t : N;
202 //
203 // We can just assume that it's:
204 //
205 // T t : sizeof(T);
206 //
207 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000208 }
209
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000210 // Compute the access components. The policy we use is to start by attempting
211 // to access using the width of the bit-field type itself and to always access
212 // at aligned indices of that type. If such an access would fail because it
213 // extends past the bound of the type, then we reduce size to the next smaller
214 // power of two and retry. The current algorithm assumes pow2 sized types,
215 // although this is easy to fix.
216 //
217 // FIXME: This algorithm is wrong on big-endian systems, I think.
218 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
219 CGBitFieldInfo::AccessInfo Components[3];
220 unsigned NumComponents = 0;
221 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
222 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000223
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000224 // Round down from the field offset to find the first access position that is
225 // at an aligned offset of the initial access type.
Daniel Dunbar59813772010-04-22 15:22:33 +0000226 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
227
228 // Adjust initial access size to fit within record.
229 while (AccessWidth > 8 &&
230 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
231 AccessWidth >>= 1;
232 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
233 }
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000234
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000235 while (AccessedTargetBits < FieldSize) {
236 // Check that we can access using a type of this size, without reading off
237 // the end of the structure. This can occur with packed structures and
238 // -fno-bitfield-type-align, for example.
239 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
240 // If so, reduce access size to the next smaller power-of-two and retry.
241 AccessWidth >>= 1;
242 assert(AccessWidth >= 8 && "Cannot access under byte size!");
243 continue;
244 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000245
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000246 // Otherwise, add an access component.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000247
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000248 // First, compute the bits inside this access which are part of the
249 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
250 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
251 // in the target that we are reading.
Daniel Dunbar59813772010-04-22 15:22:33 +0000252 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
253 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000254 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
255 uint64_t AccessBitsInFieldSize =
Daniel Dunbar59813772010-04-22 15:22:33 +0000256 std::min(AccessWidth + AccessStart,
257 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar5d6c07e2010-04-22 14:56:10 +0000258
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000259 assert(NumComponents < 3 && "Unexpected number of components!");
260 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
261 AI.FieldIndex = 0;
262 // FIXME: We still follow the old access pattern of only using the field
263 // byte offset. We should switch this once we fix the struct layout to be
264 // pretty.
265 AI.FieldByteOffset = AccessStart / 8;
266 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
267 AI.AccessWidth = AccessWidth;
Daniel Dunbarfc66e0e2010-04-22 03:17:04 +0000268 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000269 AI.TargetBitOffset = AccessedTargetBits;
270 AI.TargetBitWidth = AccessBitsInFieldSize;
271
272 AccessStart += AccessWidth;
273 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000274 }
275
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000276 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000277 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000278}
279
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000280CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
281 const FieldDecl *FD,
282 uint64_t FieldOffset,
283 uint64_t FieldSize) {
284 const RecordDecl *RD = FD->getParent();
285 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
286 uint64_t ContainingTypeSizeInBits = RL.getSize();
287 unsigned ContainingTypeAlign = RL.getAlignment();
288
289 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
290 ContainingTypeAlign);
291}
292
Anders Carlsson307846f2009-07-23 03:17:50 +0000293void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
294 uint64_t FieldOffset) {
Mike Stump11289f42009-09-09 15:08:12 +0000295 uint64_t FieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000296 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000297
Anders Carlsson307846f2009-07-23 03:17:50 +0000298 if (FieldSize == 0)
299 return;
300
Anders Carlssond5d64132009-07-28 17:56:36 +0000301 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000302 unsigned NumBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000303
Anders Carlsson307846f2009-07-23 03:17:50 +0000304 if (FieldOffset < NextFieldOffset) {
305 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +0000306 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump11289f42009-09-09 15:08:12 +0000307
Anders Carlsson307846f2009-07-23 03:17:50 +0000308 // The bitfield begins in the previous bit-field.
Mike Stump11289f42009-09-09 15:08:12 +0000309 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000310 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
311 } else {
312 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
313
314 // Append padding if necessary.
315 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump11289f42009-09-09 15:08:12 +0000316
317 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000318 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000319
Anders Carlsson307846f2009-07-23 03:17:50 +0000320 assert(NumBytesToAppend && "No bytes to append!");
321 }
322
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000323 // Add the bit field info.
324 LLVMBitFields.push_back(
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000325 LLVMBitFieldInfo(D, CGBitFieldInfo::MakeInfo(Types, D, FieldOffset,
326 FieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000327
Anders Carlsson307846f2009-07-23 03:17:50 +0000328 AppendBytes(NumBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000329
Mike Stump11289f42009-09-09 15:08:12 +0000330 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +0000331 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000332}
333
334bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
335 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000336 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000337 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000338 return false;
339
340 if (D->isBitField()) {
341 // We must use packed structs for unnamed bit fields since they
342 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000343 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000344 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000345
Anders Carlsson307846f2009-07-23 03:17:50 +0000346 LayoutBitField(D, FieldOffset);
347 return true;
348 }
Mike Stump11289f42009-09-09 15:08:12 +0000349
John McCall614dbdc2010-08-22 21:01:12 +0000350 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000351
Anders Carlsson307846f2009-07-23 03:17:50 +0000352 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000353 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Anders Carlsson19702bb2009-08-04 16:29:15 +0000355 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
356 unsigned TypeAlignment = getTypeAlignment(Ty);
357
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000358 // If the type alignment is larger then the struct alignment, we must use
359 // a packed struct.
360 if (TypeAlignment > Alignment) {
361 assert(!Packed && "Alignment is wrong even with packed struct!");
362 return false;
363 }
Mike Stump11289f42009-09-09 15:08:12 +0000364
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000365 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
366 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
Daniel Dunbar40130442010-05-27 01:12:46 +0000367 if (const MaxFieldAlignmentAttr *MFAA =
368 RD->getAttr<MaxFieldAlignmentAttr>()) {
369 if (MFAA->getAlignment() != TypeAlignment * 8 && !Packed)
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000370 return false;
371 }
372 }
373
Anders Carlsson19702bb2009-08-04 16:29:15 +0000374 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000375 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson19702bb2009-08-04 16:29:15 +0000376 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
377
378 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
379 assert(!Packed && "Could not place field even with packed struct!");
380 return false;
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Anders Carlsson19702bb2009-08-04 16:29:15 +0000383 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
384 // Even with alignment, the field offset is not at the right place,
385 // insert padding.
386 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump11289f42009-09-09 15:08:12 +0000387
Anders Carlsson19702bb2009-08-04 16:29:15 +0000388 AppendBytes(PaddingInBytes);
389 }
Mike Stump11289f42009-09-09 15:08:12 +0000390
Anders Carlsson307846f2009-07-23 03:17:50 +0000391 // Now append the field.
392 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000393 AppendField(FieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000394
Anders Carlsson307846f2009-07-23 03:17:50 +0000395 return true;
396}
397
Anders Carlsson1de2f572010-04-17 20:49:27 +0000398const llvm::Type *
399CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
400 const ASTRecordLayout &Layout) {
401 if (Field->isBitField()) {
402 uint64_t FieldSize =
403 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
404
405 // Ignore zero sized bit fields.
406 if (FieldSize == 0)
407 return 0;
408
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000409 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
410 unsigned NumBytesToAppend =
411 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlsson2295f132010-04-17 21:04:52 +0000412
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000413 if (NumBytesToAppend > 1)
414 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlsson2295f132010-04-17 21:04:52 +0000415
Anders Carlsson1de2f572010-04-17 20:49:27 +0000416 // Add the bit field info.
417 LLVMBitFields.push_back(
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000418 LLVMBitFieldInfo(Field, CGBitFieldInfo::MakeInfo(Types, Field,
419 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000420 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000421 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000422
Anders Carlsson1de2f572010-04-17 20:49:27 +0000423 // This is a regular union field.
424 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
425 return Types.ConvertTypeForMemRecursive(Field->getType());
426}
427
Anders Carlsson697f6592009-07-23 03:43:54 +0000428void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
429 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000430
Anders Carlsson697f6592009-07-23 03:43:54 +0000431 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000432
Anders Carlsson697f6592009-07-23 03:43:54 +0000433 const llvm::Type *Ty = 0;
434 uint64_t Size = 0;
435 unsigned Align = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000436
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000437 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000438
Anders Carlsson697f6592009-07-23 03:43:54 +0000439 unsigned FieldNo = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000440 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson697f6592009-07-23 03:43:54 +0000441 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump11289f42009-09-09 15:08:12 +0000442 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000443 "Union field offset did not start at the beginning of record!");
Anders Carlsson1de2f572010-04-17 20:49:27 +0000444 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000445
Anders Carlsson1de2f572010-04-17 20:49:27 +0000446 if (!FieldTy)
447 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000448
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000449 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000450
Anders Carlssone2accf42009-07-23 21:52:03 +0000451 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
452 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump11289f42009-09-09 15:08:12 +0000453
Anders Carlsson697f6592009-07-23 03:43:54 +0000454 if (FieldAlign < Align)
455 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000456
Anders Carlsson697f6592009-07-23 03:43:54 +0000457 if (FieldAlign > Align || FieldSize > Size) {
458 Ty = FieldTy;
459 Align = FieldAlign;
460 Size = FieldSize;
461 }
462 }
Mike Stump11289f42009-09-09 15:08:12 +0000463
Anders Carlsson697f6592009-07-23 03:43:54 +0000464 // Now add our field.
Anders Carlsson0e912752009-09-03 22:56:02 +0000465 if (Ty) {
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000466 AppendField(0, Ty);
Anders Carlsson0e912752009-09-03 22:56:02 +0000467
468 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
469 // We need a packed struct.
470 Packed = true;
471 Align = 1;
472 }
473 }
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000474 if (!Align) {
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000475 assert(HasOnlyZeroSizedBitFields &&
476 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000477 Align = 1;
478 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000479
Anders Carlsson697f6592009-07-23 03:43:54 +0000480 // Append tail padding.
481 if (Layout.getSize() / 8 > Size)
482 AppendPadding(Layout.getSize() / 8, Align);
483}
484
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000485void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
486 uint64_t BaseOffset) {
487 const ASTRecordLayout &Layout =
488 Types.getContext().getASTRecordLayout(BaseDecl);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000489
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000490 uint64_t NonVirtualSize = Layout.getNonVirtualSize();
491
492 if (BaseDecl->isEmpty()) {
493 // FIXME: Lay out empty bases.
494 return;
495 }
496
John McCall614dbdc2010-08-22 21:01:12 +0000497 CheckZeroInitializable(BaseDecl);
Anders Carlssonbe48c542010-05-18 16:51:41 +0000498
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000499 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
500 AppendPadding(BaseOffset / 8, 1);
Anders Carlsson061ca522010-05-18 05:22:06 +0000501
502 // Append the base field.
503 LLVMNonVirtualBases.push_back(LLVMBaseInfo(BaseDecl, FieldTypes.size()));
504
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000505 AppendBytes(NonVirtualSize / 8);
506}
507
508void
509CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
510 const ASTRecordLayout &Layout) {
511 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
512
513 // Check if we need to add a vtable pointer.
514 if (RD->isDynamicClass()) {
515 if (!PrimaryBase) {
516 const llvm::Type *FunctionType =
517 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
518 /*isVarArg=*/true);
519 const llvm::Type *VTableTy = FunctionType->getPointerTo();
520
521 assert(NextFieldOffsetInBytes == 0 &&
522 "VTable pointer must come first!");
523 AppendField(NextFieldOffsetInBytes, VTableTy->getPointerTo());
524 } else {
525 // FIXME: Handle a virtual primary base.
526 if (!Layout.getPrimaryBaseWasVirtual())
527 LayoutNonVirtualBase(PrimaryBase, 0);
528 }
529 }
530
531 // Layout the non-virtual bases.
532 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
533 E = RD->bases_end(); I != E; ++I) {
534 if (I->isVirtual())
535 continue;
536
537 const CXXRecordDecl *BaseDecl =
538 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
539
540 // We've already laid out the primary base.
541 if (BaseDecl == PrimaryBase && !Layout.getPrimaryBaseWasVirtual())
542 continue;
543
Anders Carlssonfd88a612010-10-31 23:22:37 +0000544 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffsetInBits(BaseDecl));
Anders Carlssond681a292009-12-16 17:27:20 +0000545 }
546}
547
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000548void
549CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
550 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
551
552 uint64_t AlignedNonVirtualTypeSize =
553 llvm::RoundUpToAlignment(Layout.getNonVirtualSize(),
554 Layout.getNonVirtualAlign()) / 8;
555
556
557 // First check if we can use the same fields as for the complete class.
558 if (AlignedNonVirtualTypeSize == Layout.getSize() / 8) {
559 NonVirtualBaseTypeIsSameAsCompleteType = true;
560 return;
561 }
562
563 NonVirtualBaseFieldTypes = FieldTypes;
564
565 // Check if we need padding.
566 uint64_t AlignedNextFieldOffset =
567 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
568
569 assert(AlignedNextFieldOffset <= AlignedNonVirtualTypeSize &&
570 "Size mismatch!");
571
572 if (AlignedNonVirtualTypeSize == AlignedNextFieldOffset) {
573 // We don't need any padding.
574 return;
575 }
576
577 uint64_t NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
578 NonVirtualBaseFieldTypes.push_back(getByteArrayType(NumBytes));
579
580 printf("nvts: %llu, aligned nfo: %llu\n",
581 AlignedNonVirtualTypeSize, AlignedNextFieldOffset);
582}
583
Anders Carlsson307846f2009-07-23 03:17:50 +0000584bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
585 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000586 assert(Alignment && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000587
Anders Carlsson697f6592009-07-23 03:43:54 +0000588 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000589
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000590 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
591 if (RD)
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000592 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000593
Anders Carlsson307846f2009-07-23 03:17:50 +0000594 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000595
Mike Stump11289f42009-09-09 15:08:12 +0000596 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000597 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
598 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000599 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000600 "Could not layout fields even with a packed LLVM struct!");
601 return false;
602 }
603 }
604
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000605 // We've laid out the non-virtual bases and the fields, now compute the
606 // non-virtual base field types.
607 if (RD)
608 ComputeNonVirtualBaseType(RD);
609
610 // FIXME: Lay out the virtual bases instead of just treating them as tail
611 // padding.
612
Anders Carlsson307846f2009-07-23 03:17:50 +0000613 // Append tail padding if necessary.
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000614 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000615
Anders Carlsson307846f2009-07-23 03:17:50 +0000616 return true;
617}
618
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000619void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
620 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000621
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000622 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000623 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000624
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000625 uint64_t AlignedNextFieldOffset =
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000626 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
627
628 if (AlignedNextFieldOffset == RecordSizeInBytes) {
629 // We don't need any padding.
630 return;
631 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000632
Anders Carlssond5d64132009-07-28 17:56:36 +0000633 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000634 AppendBytes(NumPadBytes);
635}
636
Mike Stump11289f42009-09-09 15:08:12 +0000637void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000638 const llvm::Type *FieldTy) {
639 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
640 getTypeAlignment(FieldTy));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000641
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000642 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000643
Anders Carlsson307846f2009-07-23 03:17:50 +0000644 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000645
Anders Carlssond5d64132009-07-28 17:56:36 +0000646 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000647 BitsAvailableInLastField = 0;
648}
649
Mike Stump11289f42009-09-09 15:08:12 +0000650void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000651 unsigned FieldAlignment) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000652 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
653 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000654
Anders Carlsson307846f2009-07-23 03:17:50 +0000655 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000656 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson307846f2009-07-23 03:17:50 +0000657 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
658
659 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
660 // Even with alignment, the field offset is not at the right place,
661 // insert padding.
662 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
663
664 AppendBytes(PaddingInBytes);
665 }
666}
667
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000668const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(uint64_t NumBytes) {
669 assert(NumBytes != 0 && "Empty byte array's aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000670
Owen Anderson41a75022009-08-13 21:57:51 +0000671 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000672 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000673 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000675 return Ty;
676}
677
678void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
679 if (NumBytes == 0)
680 return;
681
Anders Carlsson307846f2009-07-23 03:17:50 +0000682 // Append the padding field
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000683 AppendField(NextFieldOffsetInBytes, getByteArrayType(NumBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000684}
685
686unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000687 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000688 return 1;
Mike Stump11289f42009-09-09 15:08:12 +0000689
Anders Carlsson307846f2009-07-23 03:17:50 +0000690 return Types.getTargetData().getABITypeAlignment(Ty);
691}
692
John McCall614dbdc2010-08-22 21:01:12 +0000693void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000694 // This record already contains a member pointer.
John McCall614dbdc2010-08-22 21:01:12 +0000695 if (!IsZeroInitializable)
Anders Carlssond606de72009-08-23 01:25:01 +0000696 return;
Mike Stump11289f42009-09-09 15:08:12 +0000697
Anders Carlssond606de72009-08-23 01:25:01 +0000698 // Can only have member pointers if we're compiling C++.
699 if (!Types.getContext().getLangOptions().CPlusPlus)
700 return;
Mike Stump11289f42009-09-09 15:08:12 +0000701
Anders Carlssone8bfe412010-02-02 05:17:25 +0000702 T = Types.getContext().getBaseElementType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000703
Anders Carlssone8bfe412010-02-02 05:17:25 +0000704 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000705 if (!Types.getCXXABI().isZeroInitializable(MPT))
706 IsZeroInitializable = false;
Anders Carlssone8bfe412010-02-02 05:17:25 +0000707 } else if (const RecordType *RT = T->getAs<RecordType>()) {
708 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall614dbdc2010-08-22 21:01:12 +0000709 CheckZeroInitializable(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000710 }
Anders Carlssond606de72009-08-23 01:25:01 +0000711}
712
John McCall614dbdc2010-08-22 21:01:12 +0000713void CGRecordLayoutBuilder::CheckZeroInitializable(const CXXRecordDecl *RD) {
Anders Carlssonbe48c542010-05-18 16:51:41 +0000714 // This record already contains a member pointer.
John McCall614dbdc2010-08-22 21:01:12 +0000715 if (!IsZeroInitializable)
Anders Carlssonbe48c542010-05-18 16:51:41 +0000716 return;
717
718 // FIXME: It would be better if there was a way to explicitly compute the
719 // record layout instead of converting to a type.
720 Types.ConvertTagDeclType(RD);
721
722 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
723
John McCall614dbdc2010-08-22 21:01:12 +0000724 if (!Layout.isZeroInitializable())
725 IsZeroInitializable = false;
Anders Carlssonbe48c542010-05-18 16:51:41 +0000726}
727
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000728CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
729 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000730
Anders Carlsson307846f2009-07-23 03:17:50 +0000731 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000732
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000733 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson758428f2009-08-05 23:18:46 +0000734 Builder.FieldTypes,
Anders Carlssond78fc892009-07-23 17:24:40 +0000735 Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000736
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000737 const llvm::Type *BaseTy = 0;
738 if (isa<CXXRecordDecl>(D)) {
739 if (Builder.NonVirtualBaseTypeIsSameAsCompleteType)
740 BaseTy = Ty;
741 else if (!Builder.NonVirtualBaseFieldTypes.empty())
742 BaseTy = llvm::StructType::get(getLLVMContext(),
743 Builder.NonVirtualBaseFieldTypes,
744 Builder.Packed);
745 }
746
Daniel Dunbar034299e2010-03-31 01:09:11 +0000747 CGRecordLayout *RL =
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000748 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable);
Daniel Dunbar034299e2010-03-31 01:09:11 +0000749
Anders Carlsson061ca522010-05-18 05:22:06 +0000750 // Add all the non-virtual base field numbers.
751 RL->NonVirtualBaseFields.insert(Builder.LLVMNonVirtualBases.begin(),
752 Builder.LLVMNonVirtualBases.end());
753
Anders Carlsson307846f2009-07-23 03:17:50 +0000754 // Add all the field numbers.
Anders Carlsson061ca522010-05-18 05:22:06 +0000755 RL->FieldInfo.insert(Builder.LLVMFields.begin(),
756 Builder.LLVMFields.end());
Anders Carlsson307846f2009-07-23 03:17:50 +0000757
758 // Add bitfield info.
Anders Carlsson061ca522010-05-18 05:22:06 +0000759 RL->BitFields.insert(Builder.LLVMBitFields.begin(),
760 Builder.LLVMBitFields.end());
Mike Stump11289f42009-09-09 15:08:12 +0000761
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000762 // Dump the layout, if requested.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000763 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000764 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000765 llvm::errs() << "Record: ";
766 D->dump();
767 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000768 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000769 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000770
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000771#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000772 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000773 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
774
775 uint64_t TypeSizeInBits = Layout.getSize();
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000776 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000777 "Type size mismatch!");
778
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000779 if (BaseTy) {
780 uint64_t AlignedNonVirtualTypeSizeInBits =
781 llvm::RoundUpToAlignment(Layout.getNonVirtualSize(),
782 Layout.getNonVirtualAlign());
783
784 assert(AlignedNonVirtualTypeSizeInBits ==
785 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
786 "Type size mismatch!");
787 }
788
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000789 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000790 const llvm::StructType *ST =
791 dyn_cast<llvm::StructType>(RL->getLLVMType());
792 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
793
794 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
795 RecordDecl::field_iterator it = D->field_begin();
796 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
797 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000798
799 // For non-bit-fields, just check that the LLVM struct offset matches the
800 // AST offset.
801 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000802 unsigned FieldNo = RL->getLLVMFieldNo(FD);
803 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
804 "Invalid field offset!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000805 continue;
806 }
807
808 // Ignore unnamed bit-fields.
809 if (!FD->getDeclName())
810 continue;
811
812 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
813 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
814 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
815
816 // Verify that every component access is within the structure.
817 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
818 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
819 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
820 "Invalid bit-field access (out of range)!");
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000821 }
822 }
823#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000824
Daniel Dunbar034299e2010-03-31 01:09:11 +0000825 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +0000826}
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000827
828void CGRecordLayout::print(llvm::raw_ostream &OS) const {
829 OS << "<CGRecordLayout\n";
830 OS << " LLVMType:" << *LLVMType << "\n";
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000831 if (BaseLLVMType)
832 OS << " BaseLLVMType:" << *BaseLLVMType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +0000833 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000834 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000835
836 // Print bit-field infos in declaration order.
837 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000838 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
839 it = BitFields.begin(), ie = BitFields.end();
840 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000841 const RecordDecl *RD = it->first->getParent();
842 unsigned Index = 0;
843 for (RecordDecl::field_iterator
844 it2 = RD->field_begin(); *it2 != it->first; ++it2)
845 ++Index;
846 BFIs.push_back(std::make_pair(Index, &it->second));
847 }
848 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
849 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +0000850 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000851 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000852 OS << "\n";
853 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000854
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000855 OS << "]>\n";
856}
857
858void CGRecordLayout::dump() const {
859 print(llvm::errs());
860}
861
862void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
863 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000864 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000865 OS << " IsSigned:" << IsSigned << "\n";
866
867 OS.indent(4 + strlen("<CGBitFieldInfo"));
868 OS << " NumComponents:" << getNumComponents();
869 OS << " Components: [";
870 if (getNumComponents()) {
871 OS << "\n";
872 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
873 const AccessInfo &AI = getComponent(i);
874 OS.indent(8);
875 OS << "<AccessInfo"
876 << " FieldIndex:" << AI.FieldIndex
877 << " FieldByteOffset:" << AI.FieldByteOffset
878 << " FieldBitStart:" << AI.FieldBitStart
879 << " AccessWidth:" << AI.AccessWidth << "\n";
880 OS.indent(8 + strlen("<AccessInfo"));
881 OS << " AccessAlignment:" << AI.AccessAlignment
882 << " TargetBitOffset:" << AI.TargetBitOffset
883 << " TargetBitWidth:" << AI.TargetBitWidth
884 << ">\n";
885 }
886 OS.indent(4);
887 }
888 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000889}
890
891void CGBitFieldInfo::dump() const {
892 print(llvm::errs());
893}