blob: e158eed9878e3084e23d9423ce28bf21afe69224 [file] [log] [blame]
Daniel Dunbar270e2032010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson45372a62009-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 Dunbar270e2032010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson45372a62009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2924ade2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
Anders Carlsson46170f92010-11-24 22:50:27 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlsson45372a62009-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 McCallf16aa102010-08-22 21:01:12 +000022#include "CGCXXABI.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000023#include "llvm/DerivedTypes.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000024#include "llvm/Type.h"
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +000025#include "llvm/Support/Debug.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000026#include "llvm/Support/raw_ostream.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000027#include "llvm/Target/TargetData.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000028using namespace clang;
29using namespace CodeGen;
30
John McCalld0de0ce2010-11-30 23:17:27 +000031namespace {
Daniel Dunbar270e2032010-03-31 00:11:27 +000032
33class CGRecordLayoutBuilder {
34public:
35 /// FieldTypes - Holds the LLVM types that the struct is created from.
36 std::vector<const llvm::Type *> FieldTypes;
37
Anders Carlsson3d155e62010-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 Dunbar270e2032010-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 Dunbarc7a984a2010-04-06 01:07:41 +000061 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar270e2032010-03-31 00:11:27 +000062 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
63
Anders Carlssonc6772ce2010-05-18 05:22:06 +000064 typedef std::pair<const CXXRecordDecl *, unsigned> LLVMBaseInfo;
65 llvm::SmallVector<LLVMBaseInfo, 16> LLVMNonVirtualBases;
Anders Carlsson46170f92010-11-24 22:50:27 +000066
67 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
68 /// primary base classes for some other direct or indirect base class.
69 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
70
Anders Carlsson1d7dc222010-11-28 19:18:44 +000071 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
72 /// avoid laying out virtual bases more than once.
73 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
74
John McCallf16aa102010-08-22 21:01:12 +000075 /// IsZeroInitializable - Whether this struct can be C++
76 /// zero-initialized with an LLVM zeroinitializer.
77 bool IsZeroInitializable;
Daniel Dunbar270e2032010-03-31 00:11:27 +000078
79 /// Packed - Whether the resulting LLVM struct will be packed or not.
80 bool Packed;
81
82private:
83 CodeGenTypes &Types;
84
85 /// Alignment - Contains the alignment of the RecordDecl.
86 //
87 // FIXME: This is not needed and should be removed.
88 unsigned Alignment;
89
Daniel Dunbar270e2032010-03-31 00:11:27 +000090 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
91 /// this will have the number of bits still available in the field.
92 char BitsAvailableInLastField;
93
94 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
95 uint64_t NextFieldOffsetInBytes;
96
Anders Carlsson86664462010-04-17 20:49:27 +000097 /// LayoutUnionField - Will layout a field in an union and return the type
98 /// that the field will have.
99 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
100 const ASTRecordLayout &Layout);
101
Daniel Dunbar270e2032010-03-31 00:11:27 +0000102 /// LayoutUnion - Will layout a union RecordDecl.
103 void LayoutUnion(const RecordDecl *D);
104
105 /// LayoutField - try to layout all fields in the record decl.
106 /// Returns false if the operation failed because the struct is not packed.
107 bool LayoutFields(const RecordDecl *D);
108
Anders Carlsson860453c2010-12-04 23:59:48 +0000109 /// Layout a single base, virtual or non-virtual
110 void LayoutBase(const CXXRecordDecl *BaseDecl, uint64_t BaseOffset);
111
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000112 /// LayoutVirtualBase - layout a single virtual base.
113 void LayoutVirtualBase(const CXXRecordDecl *BaseDecl, uint64_t BaseOffset);
114
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000115 /// LayoutVirtualBases - layout the virtual bases of a record decl.
116 void LayoutVirtualBases(const CXXRecordDecl *RD,
117 const ASTRecordLayout &Layout);
118
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000119 /// LayoutNonVirtualBase - layout a single non-virtual base.
120 void LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
121 uint64_t BaseOffset);
122
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000123 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000124 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
125 const ASTRecordLayout &Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000126
Anders Carlsson3d155e62010-11-09 05:25:47 +0000127 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000128 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000129
Daniel Dunbar270e2032010-03-31 00:11:27 +0000130 /// LayoutField - layout a single field. Returns false if the operation failed
131 /// because the current struct is not packed.
132 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
133
134 /// LayoutBitField - layout a single bit field.
135 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
136
137 /// AppendField - Appends a field with the given offset and type.
138 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
139
Daniel Dunbar270e2032010-03-31 00:11:27 +0000140 /// AppendPadding - Appends enough padding bytes so that the total
141 /// struct size is a multiple of the field alignment.
Anders Carlsson57d2d232010-12-04 23:53:18 +0000142 void AppendPadding(uint64_t FieldOffsetInBytes,
143 unsigned FieldAlignmentInBytes);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000144
Anders Carlsson3d155e62010-11-09 05:25:47 +0000145 /// getByteArrayType - Returns a byte array type with the given number of
146 /// elements.
147 const llvm::Type *getByteArrayType(uint64_t NumBytes);
148
Daniel Dunbar270e2032010-03-31 00:11:27 +0000149 /// AppendBytes - Append a given number of bytes to the record.
150 void AppendBytes(uint64_t NumBytes);
151
152 /// AppendTailPadding - Append enough tail padding so that the type will have
153 /// the passed size.
154 void AppendTailPadding(uint64_t RecordSize);
155
156 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000157
Anders Carlssonfc86d552010-11-28 23:06:23 +0000158 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
159 /// LLVM element types.
160 unsigned getAlignmentAsLLVMStruct() const;
161
John McCallf16aa102010-08-22 21:01:12 +0000162 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar270e2032010-03-31 00:11:27 +0000163 /// to data member.
John McCallf16aa102010-08-22 21:01:12 +0000164 void CheckZeroInitializable(QualType T);
165 void CheckZeroInitializable(const CXXRecordDecl *RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000166
167public:
168 CGRecordLayoutBuilder(CodeGenTypes &Types)
Anders Carlsson3d155e62010-11-09 05:25:47 +0000169 : NonVirtualBaseTypeIsSameAsCompleteType(false), IsZeroInitializable(true),
Anders Carlssonfc86d552010-11-28 23:06:23 +0000170 Packed(false), Types(Types), Alignment(0), BitsAvailableInLastField(0),
171 NextFieldOffsetInBytes(0) { }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000172
173 /// Layout - Will layout a RecordDecl.
174 void Layout(const RecordDecl *D);
175};
176
177}
Daniel Dunbar270e2032010-03-31 00:11:27 +0000178
Anders Carlsson45372a62009-07-23 03:17:50 +0000179void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000180 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000181 Packed = D->hasAttr<PackedAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000182
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000183 if (D->isUnion()) {
184 LayoutUnion(D);
185 return;
186 }
Anders Carlssona860e752009-08-08 18:23:56 +0000187
Anders Carlsson45372a62009-07-23 03:17:50 +0000188 if (LayoutFields(D))
189 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Anders Carlsson45372a62009-07-23 03:17:50 +0000191 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000192 Packed = true;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000193 NextFieldOffsetInBytes = 0;
Anders Carlsson45372a62009-07-23 03:17:50 +0000194 FieldTypes.clear();
Anders Carlsson45372a62009-07-23 03:17:50 +0000195 LLVMFields.clear();
196 LLVMBitFields.clear();
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000197 LLVMNonVirtualBases.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Anders Carlsson45372a62009-07-23 03:17:50 +0000199 LayoutFields(D);
200}
201
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000202CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
203 const FieldDecl *FD,
204 uint64_t FieldOffset,
205 uint64_t FieldSize,
206 uint64_t ContainingTypeSizeInBits,
207 unsigned ContainingTypeAlign) {
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000208 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarab970f92010-04-13 20:58:55 +0000209 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
210 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000211
212 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000213
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000214 if (FieldSize > TypeSizeInBits) {
Anders Carlsson6ba38152010-04-17 22:54:57 +0000215 // We have a wide bit-field. The extra bits are only used for padding, so
216 // if we have a bitfield of type T, with size N:
217 //
218 // T t : N;
219 //
220 // We can just assume that it's:
221 //
222 // T t : sizeof(T);
223 //
224 FieldSize = TypeSizeInBits;
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000225 }
226
Daniel Dunbare1467a42010-04-22 02:35:46 +0000227 // Compute the access components. The policy we use is to start by attempting
228 // to access using the width of the bit-field type itself and to always access
229 // at aligned indices of that type. If such an access would fail because it
230 // extends past the bound of the type, then we reduce size to the next smaller
231 // power of two and retry. The current algorithm assumes pow2 sized types,
232 // although this is easy to fix.
233 //
234 // FIXME: This algorithm is wrong on big-endian systems, I think.
235 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
236 CGBitFieldInfo::AccessInfo Components[3];
237 unsigned NumComponents = 0;
238 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
239 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000240
Daniel Dunbare1467a42010-04-22 02:35:46 +0000241 // Round down from the field offset to find the first access position that is
242 // at an aligned offset of the initial access type.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000243 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
244
245 // Adjust initial access size to fit within record.
246 while (AccessWidth > 8 &&
247 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
248 AccessWidth >>= 1;
249 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
250 }
Daniel Dunbar2df25692010-04-15 05:09:32 +0000251
Daniel Dunbare1467a42010-04-22 02:35:46 +0000252 while (AccessedTargetBits < FieldSize) {
253 // Check that we can access using a type of this size, without reading off
254 // the end of the structure. This can occur with packed structures and
255 // -fno-bitfield-type-align, for example.
256 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
257 // If so, reduce access size to the next smaller power-of-two and retry.
258 AccessWidth >>= 1;
259 assert(AccessWidth >= 8 && "Cannot access under byte size!");
260 continue;
261 }
Daniel Dunbarab970f92010-04-13 20:58:55 +0000262
Daniel Dunbare1467a42010-04-22 02:35:46 +0000263 // Otherwise, add an access component.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000264
Daniel Dunbare1467a42010-04-22 02:35:46 +0000265 // First, compute the bits inside this access which are part of the
266 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
267 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
268 // in the target that we are reading.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000269 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
270 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000271 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
272 uint64_t AccessBitsInFieldSize =
Daniel Dunbar52968a12010-04-22 15:22:33 +0000273 std::min(AccessWidth + AccessStart,
274 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar4651efb2010-04-22 14:56:10 +0000275
Daniel Dunbare1467a42010-04-22 02:35:46 +0000276 assert(NumComponents < 3 && "Unexpected number of components!");
277 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
278 AI.FieldIndex = 0;
279 // FIXME: We still follow the old access pattern of only using the field
280 // byte offset. We should switch this once we fix the struct layout to be
281 // pretty.
282 AI.FieldByteOffset = AccessStart / 8;
283 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
284 AI.AccessWidth = AccessWidth;
Daniel Dunbar89da8742010-04-22 03:17:04 +0000285 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000286 AI.TargetBitOffset = AccessedTargetBits;
287 AI.TargetBitWidth = AccessBitsInFieldSize;
288
289 AccessStart += AccessWidth;
290 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000291 }
292
Daniel Dunbare1467a42010-04-22 02:35:46 +0000293 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar2df25692010-04-15 05:09:32 +0000294 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000295}
296
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000297CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
298 const FieldDecl *FD,
299 uint64_t FieldOffset,
300 uint64_t FieldSize) {
301 const RecordDecl *RD = FD->getParent();
302 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
303 uint64_t ContainingTypeSizeInBits = RL.getSize();
304 unsigned ContainingTypeAlign = RL.getAlignment();
305
306 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
307 ContainingTypeAlign);
308}
309
Anders Carlsson45372a62009-07-23 03:17:50 +0000310void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
311 uint64_t FieldOffset) {
Mike Stump1eb44332009-09-09 15:08:12 +0000312 uint64_t FieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000313 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Anders Carlsson45372a62009-07-23 03:17:50 +0000315 if (FieldSize == 0)
316 return;
317
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000318 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson45372a62009-07-23 03:17:50 +0000319 unsigned NumBytesToAppend;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Anders Carlsson45372a62009-07-23 03:17:50 +0000321 if (FieldOffset < NextFieldOffset) {
322 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000323 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Anders Carlsson45372a62009-07-23 03:17:50 +0000325 // The bitfield begins in the previous bit-field.
Mike Stump1eb44332009-09-09 15:08:12 +0000326 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000327 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
328 } else {
329 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
330
331 // Append padding if necessary.
Anders Carlsson57d2d232010-12-04 23:53:18 +0000332 AppendPadding(FieldOffset / 8, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
334 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000335 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Anders Carlsson45372a62009-07-23 03:17:50 +0000337 assert(NumBytesToAppend && "No bytes to append!");
338 }
339
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000340 // Add the bit field info.
341 LLVMBitFields.push_back(
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000342 LLVMBitFieldInfo(D, CGBitFieldInfo::MakeInfo(Types, D, FieldOffset,
343 FieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Anders Carlsson45372a62009-07-23 03:17:50 +0000345 AppendBytes(NumBytesToAppend);
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Mike Stump1eb44332009-09-09 15:08:12 +0000347 BitsAvailableInLastField =
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000348 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000349}
350
351bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
352 uint64_t FieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000353 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000354 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000355 return false;
356
357 if (D->isBitField()) {
358 // We must use packed structs for unnamed bit fields since they
359 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000360 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000361 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Anders Carlsson45372a62009-07-23 03:17:50 +0000363 LayoutBitField(D, FieldOffset);
364 return true;
365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
John McCallf16aa102010-08-22 21:01:12 +0000367 CheckZeroInitializable(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000368
Anders Carlsson45372a62009-07-23 03:17:50 +0000369 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000370 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000372 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
373 unsigned TypeAlignment = getTypeAlignment(Ty);
374
Anders Carlssona5dd7222009-08-08 19:38:24 +0000375 // If the type alignment is larger then the struct alignment, we must use
376 // a packed struct.
377 if (TypeAlignment > Alignment) {
378 assert(!Packed && "Alignment is wrong even with packed struct!");
379 return false;
380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Anders Carlssona5dd7222009-08-08 19:38:24 +0000382 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
383 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +0000384 if (const MaxFieldAlignmentAttr *MFAA =
385 RD->getAttr<MaxFieldAlignmentAttr>()) {
386 if (MFAA->getAlignment() != TypeAlignment * 8 && !Packed)
Anders Carlssona5dd7222009-08-08 19:38:24 +0000387 return false;
388 }
389 }
390
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000391 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000392 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000393 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
394
395 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
396 assert(!Packed && "Could not place field even with packed struct!");
397 return false;
398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Anders Carlsson57d2d232010-12-04 23:53:18 +0000400 AppendPadding(FieldOffsetInBytes, TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Anders Carlsson45372a62009-07-23 03:17:50 +0000402 // Now append the field.
403 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000404 AppendField(FieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Anders Carlsson45372a62009-07-23 03:17:50 +0000406 return true;
407}
408
Anders Carlsson86664462010-04-17 20:49:27 +0000409const llvm::Type *
410CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
411 const ASTRecordLayout &Layout) {
412 if (Field->isBitField()) {
413 uint64_t FieldSize =
414 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
415
416 // Ignore zero sized bit fields.
417 if (FieldSize == 0)
418 return 0;
419
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000420 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
421 unsigned NumBytesToAppend =
422 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlssond62328e2010-04-17 21:04:52 +0000423
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000424 if (NumBytesToAppend > 1)
425 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlssond62328e2010-04-17 21:04:52 +0000426
Anders Carlsson86664462010-04-17 20:49:27 +0000427 // Add the bit field info.
428 LLVMBitFields.push_back(
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000429 LLVMBitFieldInfo(Field, CGBitFieldInfo::MakeInfo(Types, Field,
430 0, FieldSize)));
Anders Carlssond62328e2010-04-17 21:04:52 +0000431 return FieldTy;
Anders Carlsson86664462010-04-17 20:49:27 +0000432 }
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000433
Anders Carlsson86664462010-04-17 20:49:27 +0000434 // This is a regular union field.
435 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
436 return Types.ConvertTypeForMemRecursive(Field->getType());
437}
438
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000439void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
440 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000442 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000444 const llvm::Type *Ty = 0;
445 uint64_t Size = 0;
446 unsigned Align = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000448 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000449
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000450 unsigned FieldNo = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000451 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000452 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump1eb44332009-09-09 15:08:12 +0000453 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000454 "Union field offset did not start at the beginning of record!");
Anders Carlsson86664462010-04-17 20:49:27 +0000455 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000456
Anders Carlsson86664462010-04-17 20:49:27 +0000457 if (!FieldTy)
458 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000460 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000461
Anders Carlsson177d4d82009-07-23 21:52:03 +0000462 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
463 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000465 if (FieldAlign < Align)
466 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000468 if (FieldAlign > Align || FieldSize > Size) {
469 Ty = FieldTy;
470 Align = FieldAlign;
471 Size = FieldSize;
472 }
473 }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000475 // Now add our field.
Anders Carlsson36620002009-09-03 22:56:02 +0000476 if (Ty) {
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000477 AppendField(0, Ty);
Anders Carlsson36620002009-09-03 22:56:02 +0000478
479 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
480 // We need a packed struct.
481 Packed = true;
482 Align = 1;
483 }
484 }
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000485 if (!Align) {
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000486 assert(HasOnlyZeroSizedBitFields &&
487 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000488 Align = 1;
489 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000490
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000491 // Append tail padding.
492 if (Layout.getSize() / 8 > Size)
493 AppendPadding(Layout.getSize() / 8, Align);
494}
495
Anders Carlsson860453c2010-12-04 23:59:48 +0000496void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *BaseDecl,
497 uint64_t BaseOffset) {
498 CheckZeroInitializable(BaseDecl);
499
500 const ASTRecordLayout &Layout =
501 Types.getContext().getASTRecordLayout(BaseDecl);
502
Ken Dyck5c3633f2011-02-01 01:52:10 +0000503 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
Anders Carlsson860453c2010-12-04 23:59:48 +0000504
505 AppendPadding(BaseOffset / 8, 1);
506
507 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
Ken Dyck5c3633f2011-02-01 01:52:10 +0000508 AppendBytes(NonVirtualSize.getQuantity());
Anders Carlsson860453c2010-12-04 23:59:48 +0000509}
510
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000511void
512CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *BaseDecl,
513 uint64_t BaseOffset) {
514 // Ignore empty bases.
515 if (BaseDecl->isEmpty())
516 return;
517
518 CheckZeroInitializable(BaseDecl);
519
520 const ASTRecordLayout &Layout =
521 Types.getContext().getASTRecordLayout(BaseDecl);
522
Ken Dyck5c3633f2011-02-01 01:52:10 +0000523 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000524
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000525 AppendPadding(BaseOffset / 8, 1);
526
Anders Carlsson860453c2010-12-04 23:59:48 +0000527 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
Ken Dyck5c3633f2011-02-01 01:52:10 +0000528 AppendBytes(NonVirtualSize.getQuantity());
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000529
Anders Carlsson860453c2010-12-04 23:59:48 +0000530 // FIXME: Add the vbase field info.
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000531}
532
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000533/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
534void
535CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
536 const ASTRecordLayout &Layout) {
537 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
538 E = RD->bases_end(); I != E; ++I) {
539 const CXXRecordDecl *BaseDecl =
540 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
541
542 // We only want to lay out virtual bases that aren't indirect primary bases
543 // of some other base.
544 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
545 // Only lay out the base once.
546 if (!LaidOutVirtualBases.insert(BaseDecl))
547 continue;
548
549 uint64_t VBaseOffset = Layout.getVBaseClassOffsetInBits(BaseDecl);
550 LayoutVirtualBase(BaseDecl, VBaseOffset);
551 }
552
553 if (!BaseDecl->getNumVBases()) {
554 // This base isn't interesting since it doesn't have any virtual bases.
555 continue;
556 }
557
558 LayoutVirtualBases(BaseDecl, Layout);
559 }
560}
561
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000562void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
563 uint64_t BaseOffset) {
Anders Carlssona03613d2010-11-22 00:03:08 +0000564 // Ignore empty bases.
565 if (BaseDecl->isEmpty())
566 return;
567
Anders Carlsson860453c2010-12-04 23:59:48 +0000568 LayoutBase(BaseDecl, BaseOffset);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000569
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000570 // Append the base field.
Anders Carlsson860453c2010-12-04 23:59:48 +0000571 LLVMNonVirtualBases.push_back(LLVMBaseInfo(BaseDecl, FieldTypes.size() - 1));
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000572}
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 Carlssonc9e814b2010-11-24 23:12:57 +0000591 if (!Layout.isPrimaryBaseVirtual())
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000592 LayoutNonVirtualBase(PrimaryBase, 0);
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000593 else
594 LayoutVirtualBase(PrimaryBase, 0);
Anders Carlsson15ddfdc2010-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 Carlssonc9e814b2010-11-24 23:12:57 +0000608 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000609 continue;
610
Anders Carlssona14f5972010-10-31 23:22:37 +0000611 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffsetInBits(BaseDecl));
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000612 }
613}
614
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000615bool
Anders Carlsson3d155e62010-11-09 05:25:47 +0000616CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
617 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
618
Ken Dyck5c3633f2011-02-01 01:52:10 +0000619
Ken Dyck68cf1a52011-02-08 02:02:47 +0000620 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
621 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
Anders Carlsson3d155e62010-11-09 05:25:47 +0000622 uint64_t AlignedNonVirtualTypeSize =
Ken Dyck68cf1a52011-02-08 02:02:47 +0000623 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign).getQuantity();
Anders Carlsson3d155e62010-11-09 05:25:47 +0000624
625
626 // First check if we can use the same fields as for the complete class.
627 if (AlignedNonVirtualTypeSize == Layout.getSize() / 8) {
628 NonVirtualBaseTypeIsSameAsCompleteType = true;
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000629 return true;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000630 }
631
Anders Carlsson3d155e62010-11-09 05:25:47 +0000632 // Check if we need padding.
633 uint64_t AlignedNextFieldOffset =
Anders Carlssonfc86d552010-11-28 23:06:23 +0000634 llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
635 getAlignmentAsLLVMStruct());
Anders Carlsson3d155e62010-11-09 05:25:47 +0000636
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000637 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize)
638 return false; // Needs packing.
639
640 NonVirtualBaseFieldTypes = FieldTypes;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000641
642 if (AlignedNonVirtualTypeSize == AlignedNextFieldOffset) {
643 // We don't need any padding.
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000644 return true;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000645 }
646
647 uint64_t NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
648 NonVirtualBaseFieldTypes.push_back(getByteArrayType(NumBytes));
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000649 return true;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000650}
651
Anders Carlsson45372a62009-07-23 03:17:50 +0000652bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
653 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlssona5dd7222009-08-08 19:38:24 +0000654 assert(Alignment && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000656 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Anders Carlsson3d155e62010-11-09 05:25:47 +0000658 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
659 if (RD)
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000660 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000661
Anders Carlsson45372a62009-07-23 03:17:50 +0000662 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000663
Mike Stump1eb44332009-09-09 15:08:12 +0000664 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000665 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
666 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000667 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000668 "Could not layout fields even with a packed LLVM struct!");
669 return false;
670 }
671 }
672
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000673 if (RD) {
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000674 // We've laid out the non-virtual bases and the fields, now compute the
675 // non-virtual base field types.
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000676 if (!ComputeNonVirtualBaseType(RD)) {
677 assert(!Packed && "Could not layout even with a packed LLVM struct!");
678 return false;
679 }
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000680
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000681 // And lay out the virtual bases.
682 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
683 if (Layout.isPrimaryBaseVirtual())
684 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
685 LayoutVirtualBases(RD, Layout);
686 }
Anders Carlsson3d155e62010-11-09 05:25:47 +0000687
Anders Carlsson45372a62009-07-23 03:17:50 +0000688 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000689 AppendTailPadding(Layout.getSize());
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Anders Carlsson45372a62009-07-23 03:17:50 +0000691 return true;
692}
693
Anders Carlssonc1efe362009-07-27 14:55:54 +0000694void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
695 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Anders Carlssonc1efe362009-07-27 14:55:54 +0000697 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000698 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Daniel Dunbar270e2032010-03-31 00:11:27 +0000700 uint64_t AlignedNextFieldOffset =
Anders Carlssonfc86d552010-11-28 23:06:23 +0000701 llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
702 getAlignmentAsLLVMStruct());
Anders Carlssonc2456822009-12-08 01:24:23 +0000703
704 if (AlignedNextFieldOffset == RecordSizeInBytes) {
705 // We don't need any padding.
706 return;
707 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000708
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000709 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000710 AppendBytes(NumPadBytes);
711}
712
Mike Stump1eb44332009-09-09 15:08:12 +0000713void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000714 const llvm::Type *FieldTy) {
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000715 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000716
Anders Carlsson45372a62009-07-23 03:17:50 +0000717 FieldTypes.push_back(FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000718
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000719 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson45372a62009-07-23 03:17:50 +0000720 BitsAvailableInLastField = 0;
721}
722
Mike Stump1eb44332009-09-09 15:08:12 +0000723void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson57d2d232010-12-04 23:53:18 +0000724 unsigned FieldAlignmentInBytes) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000725 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
726 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Anders Carlsson45372a62009-07-23 03:17:50 +0000728 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000729 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson57d2d232010-12-04 23:53:18 +0000730 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignmentInBytes);
Anders Carlsson45372a62009-07-23 03:17:50 +0000731
732 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
733 // Even with alignment, the field offset is not at the right place,
734 // insert padding.
735 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
736
737 AppendBytes(PaddingInBytes);
738 }
739}
740
Anders Carlsson3d155e62010-11-09 05:25:47 +0000741const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(uint64_t NumBytes) {
742 assert(NumBytes != 0 && "Empty byte array's aren't allowed.");
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Owen Anderson0032b272009-08-13 21:57:51 +0000744 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonc1efe362009-07-27 14:55:54 +0000745 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000746 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Anders Carlsson3d155e62010-11-09 05:25:47 +0000748 return Ty;
749}
750
751void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
752 if (NumBytes == 0)
753 return;
754
Anders Carlsson45372a62009-07-23 03:17:50 +0000755 // Append the padding field
Anders Carlsson3d155e62010-11-09 05:25:47 +0000756 AppendField(NextFieldOffsetInBytes, getByteArrayType(NumBytes));
Anders Carlsson45372a62009-07-23 03:17:50 +0000757}
758
759unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000760 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000761 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Anders Carlsson45372a62009-07-23 03:17:50 +0000763 return Types.getTargetData().getABITypeAlignment(Ty);
764}
765
Anders Carlssonfc86d552010-11-28 23:06:23 +0000766unsigned CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
767 if (Packed)
768 return 1;
769
770 unsigned MaxAlignment = 1;
771 for (size_t i = 0; i != FieldTypes.size(); ++i)
772 MaxAlignment = std::max(MaxAlignment, getTypeAlignment(FieldTypes[i]));
773
774 return MaxAlignment;
775}
776
John McCallf16aa102010-08-22 21:01:12 +0000777void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000778 // This record already contains a member pointer.
John McCallf16aa102010-08-22 21:01:12 +0000779 if (!IsZeroInitializable)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000780 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000782 // Can only have member pointers if we're compiling C++.
783 if (!Types.getContext().getLangOptions().CPlusPlus)
784 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Anders Carlsson2c12d032010-02-02 05:17:25 +0000786 T = Types.getContext().getBaseElementType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Anders Carlsson2c12d032010-02-02 05:17:25 +0000788 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
John McCallf16aa102010-08-22 21:01:12 +0000789 if (!Types.getCXXABI().isZeroInitializable(MPT))
790 IsZeroInitializable = false;
Anders Carlsson2c12d032010-02-02 05:17:25 +0000791 } else if (const RecordType *RT = T->getAs<RecordType>()) {
792 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCallf16aa102010-08-22 21:01:12 +0000793 CheckZeroInitializable(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000794 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000795}
796
John McCallf16aa102010-08-22 21:01:12 +0000797void CGRecordLayoutBuilder::CheckZeroInitializable(const CXXRecordDecl *RD) {
Anders Carlssona83fb4b2010-05-18 16:51:41 +0000798 // This record already contains a member pointer.
John McCallf16aa102010-08-22 21:01:12 +0000799 if (!IsZeroInitializable)
Anders Carlssona83fb4b2010-05-18 16:51:41 +0000800 return;
801
Anders Carlsson3379e9b2010-11-24 19:57:04 +0000802 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
John McCallf16aa102010-08-22 21:01:12 +0000803 if (!Layout.isZeroInitializable())
804 IsZeroInitializable = false;
Anders Carlssona83fb4b2010-05-18 16:51:41 +0000805}
806
Daniel Dunbar270e2032010-03-31 00:11:27 +0000807CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
808 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Anders Carlsson45372a62009-07-23 03:17:50 +0000810 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000811
Anders Carlssonba2c2ee2010-11-24 19:37:16 +0000812 const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
813 Builder.FieldTypes,
814 Builder.Packed);
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Anders Carlssonba2c2ee2010-11-24 19:37:16 +0000816 const llvm::StructType *BaseTy = 0;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000817 if (isa<CXXRecordDecl>(D)) {
818 if (Builder.NonVirtualBaseTypeIsSameAsCompleteType)
819 BaseTy = Ty;
820 else if (!Builder.NonVirtualBaseFieldTypes.empty())
821 BaseTy = llvm::StructType::get(getLLVMContext(),
822 Builder.NonVirtualBaseFieldTypes,
823 Builder.Packed);
824 }
825
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000826 CGRecordLayout *RL =
Anders Carlsson3d155e62010-11-09 05:25:47 +0000827 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable);
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000828
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000829 // Add all the non-virtual base field numbers.
830 RL->NonVirtualBaseFields.insert(Builder.LLVMNonVirtualBases.begin(),
831 Builder.LLVMNonVirtualBases.end());
832
Anders Carlsson45372a62009-07-23 03:17:50 +0000833 // Add all the field numbers.
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000834 RL->FieldInfo.insert(Builder.LLVMFields.begin(),
835 Builder.LLVMFields.end());
Anders Carlsson45372a62009-07-23 03:17:50 +0000836
837 // Add bitfield info.
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000838 RL->BitFields.insert(Builder.LLVMBitFields.begin(),
839 Builder.LLVMBitFields.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000841 // Dump the layout, if requested.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000842 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +0000843 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarab970f92010-04-13 20:58:55 +0000844 llvm::errs() << "Record: ";
845 D->dump();
846 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000847 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000848 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000849
Daniel Dunbare1467a42010-04-22 02:35:46 +0000850#ifndef NDEBUG
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000851 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlsson3d155e62010-11-09 05:25:47 +0000852 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
853
854 uint64_t TypeSizeInBits = Layout.getSize();
Daniel Dunbare1467a42010-04-22 02:35:46 +0000855 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000856 "Type size mismatch!");
857
Anders Carlsson3d155e62010-11-09 05:25:47 +0000858 if (BaseTy) {
Ken Dyck68cf1a52011-02-08 02:02:47 +0000859 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
860 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
861 CharUnits AlignedNonVirtualTypeSize =
862 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
863
864 uint64_t AlignedNonVirtualTypeSizeInBits =
865 AlignedNonVirtualTypeSize.getQuantity() * getContext().getCharWidth();
Anders Carlsson3d155e62010-11-09 05:25:47 +0000866
867 assert(AlignedNonVirtualTypeSizeInBits ==
868 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
869 "Type size mismatch!");
870 }
871
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000872 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000873 const llvm::StructType *ST =
874 dyn_cast<llvm::StructType>(RL->getLLVMType());
875 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
876
877 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
878 RecordDecl::field_iterator it = D->field_begin();
879 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
880 const FieldDecl *FD = *it;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000881
882 // For non-bit-fields, just check that the LLVM struct offset matches the
883 // AST offset.
884 if (!FD->isBitField()) {
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000885 unsigned FieldNo = RL->getLLVMFieldNo(FD);
886 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
887 "Invalid field offset!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000888 continue;
889 }
890
891 // Ignore unnamed bit-fields.
892 if (!FD->getDeclName())
893 continue;
894
895 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
896 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
897 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
898
899 // Verify that every component access is within the structure.
900 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
901 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
902 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
903 "Invalid bit-field access (out of range)!");
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000904 }
905 }
906#endif
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000907
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000908 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +0000909}
Daniel Dunbar93c62962010-04-12 18:14:18 +0000910
911void CGRecordLayout::print(llvm::raw_ostream &OS) const {
912 OS << "<CGRecordLayout\n";
913 OS << " LLVMType:" << *LLVMType << "\n";
Anders Carlsson9a5a3f22010-11-21 23:59:45 +0000914 if (NonVirtualBaseLLVMType)
915 OS << " NonVirtualBaseLLVMType:" << *NonVirtualBaseLLVMType << "\n";
John McCallf16aa102010-08-22 21:01:12 +0000916 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000917 OS << " BitFields:[\n";
Daniel Dunbarad759532010-04-22 02:35:36 +0000918
919 // Print bit-field infos in declaration order.
920 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbar93c62962010-04-12 18:14:18 +0000921 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
922 it = BitFields.begin(), ie = BitFields.end();
923 it != ie; ++it) {
Daniel Dunbarad759532010-04-22 02:35:36 +0000924 const RecordDecl *RD = it->first->getParent();
925 unsigned Index = 0;
926 for (RecordDecl::field_iterator
927 it2 = RD->field_begin(); *it2 != it->first; ++it2)
928 ++Index;
929 BFIs.push_back(std::make_pair(Index, &it->second));
930 }
931 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
932 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarab970f92010-04-13 20:58:55 +0000933 OS.indent(4);
Daniel Dunbarad759532010-04-22 02:35:36 +0000934 BFIs[i].second->print(OS);
Daniel Dunbar93c62962010-04-12 18:14:18 +0000935 OS << "\n";
936 }
Daniel Dunbarad759532010-04-22 02:35:36 +0000937
Daniel Dunbar93c62962010-04-12 18:14:18 +0000938 OS << "]>\n";
939}
940
941void CGRecordLayout::dump() const {
942 print(llvm::errs());
943}
944
945void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
946 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000947 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000948 OS << " IsSigned:" << IsSigned << "\n";
949
950 OS.indent(4 + strlen("<CGBitFieldInfo"));
951 OS << " NumComponents:" << getNumComponents();
952 OS << " Components: [";
953 if (getNumComponents()) {
954 OS << "\n";
955 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
956 const AccessInfo &AI = getComponent(i);
957 OS.indent(8);
958 OS << "<AccessInfo"
959 << " FieldIndex:" << AI.FieldIndex
960 << " FieldByteOffset:" << AI.FieldByteOffset
961 << " FieldBitStart:" << AI.FieldBitStart
962 << " AccessWidth:" << AI.AccessWidth << "\n";
963 OS.indent(8 + strlen("<AccessInfo"));
964 OS << " AccessAlignment:" << AI.AccessAlignment
965 << " TargetBitOffset:" << AI.TargetBitOffset
966 << " TargetBitWidth:" << AI.TargetBitWidth
967 << ">\n";
968 }
969 OS.indent(4);
970 }
971 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000972}
973
974void CGBitFieldInfo::dump() const {
975 print(llvm::errs());
976}