blob: cfdae2445e6fc4aff1ddb1c1f738fc2966b3798f [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"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
20#include "CodeGenTypes.h"
21#include "llvm/DerivedTypes.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000022#include "llvm/Type.h"
23#include "llvm/Support/raw_ostream.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000024#include "llvm/Target/TargetData.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000025using namespace clang;
26using namespace CodeGen;
27
Daniel Dunbar270e2032010-03-31 00:11:27 +000028namespace clang {
29namespace CodeGen {
30
31class CGRecordLayoutBuilder {
32public:
33 /// FieldTypes - Holds the LLVM types that the struct is created from.
34 std::vector<const llvm::Type *> FieldTypes;
35
36 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
37 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
38 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
39
40 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +000041 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar270e2032010-03-31 00:11:27 +000042 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
43
44 /// ContainsPointerToDataMember - Whether one of the fields in this record
45 /// layout is a pointer to data member, or a struct that contains pointer to
46 /// data member.
47 bool ContainsPointerToDataMember;
48
49 /// Packed - Whether the resulting LLVM struct will be packed or not.
50 bool Packed;
51
52private:
53 CodeGenTypes &Types;
54
55 /// Alignment - Contains the alignment of the RecordDecl.
56 //
57 // FIXME: This is not needed and should be removed.
58 unsigned Alignment;
59
60 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the
61 /// LLVM types.
62 unsigned AlignmentAsLLVMStruct;
63
64 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
65 /// this will have the number of bits still available in the field.
66 char BitsAvailableInLastField;
67
68 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
69 uint64_t NextFieldOffsetInBytes;
70
71 /// LayoutUnion - Will layout a union RecordDecl.
72 void LayoutUnion(const RecordDecl *D);
73
74 /// LayoutField - try to layout all fields in the record decl.
75 /// Returns false if the operation failed because the struct is not packed.
76 bool LayoutFields(const RecordDecl *D);
77
78 /// LayoutBases - layout the bases and vtable pointer of a record decl.
79 void LayoutBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout);
80
81 /// LayoutField - layout a single field. Returns false if the operation failed
82 /// because the current struct is not packed.
83 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
84
85 /// LayoutBitField - layout a single bit field.
86 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
87
88 /// AppendField - Appends a field with the given offset and type.
89 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
90
91 /// AppendPadding - Appends enough padding bytes so that the total struct
92 /// size matches the alignment of the passed in type.
93 void AppendPadding(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
94
95 /// AppendPadding - Appends enough padding bytes so that the total
96 /// struct size is a multiple of the field alignment.
97 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
98
99 /// AppendBytes - Append a given number of bytes to the record.
100 void AppendBytes(uint64_t NumBytes);
101
102 /// AppendTailPadding - Append enough tail padding so that the type will have
103 /// the passed size.
104 void AppendTailPadding(uint64_t RecordSize);
105
106 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000107
108 /// CheckForPointerToDataMember - Check if the given type contains a pointer
109 /// to data member.
110 void CheckForPointerToDataMember(QualType T);
111
112public:
113 CGRecordLayoutBuilder(CodeGenTypes &Types)
114 : ContainsPointerToDataMember(false), Packed(false), Types(Types),
115 Alignment(0), AlignmentAsLLVMStruct(1),
116 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
117
118 /// Layout - Will layout a RecordDecl.
119 void Layout(const RecordDecl *D);
120};
121
122}
123}
124
Anders Carlsson45372a62009-07-23 03:17:50 +0000125void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000126 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000127 Packed = D->hasAttr<PackedAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000128
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000129 if (D->isUnion()) {
130 LayoutUnion(D);
131 return;
132 }
Anders Carlssona860e752009-08-08 18:23:56 +0000133
Anders Carlsson45372a62009-07-23 03:17:50 +0000134 if (LayoutFields(D))
135 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Anders Carlsson45372a62009-07-23 03:17:50 +0000137 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000138 Packed = true;
Anders Carlsson45372a62009-07-23 03:17:50 +0000139 AlignmentAsLLVMStruct = 1;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000140 NextFieldOffsetInBytes = 0;
Anders Carlsson45372a62009-07-23 03:17:50 +0000141 FieldTypes.clear();
Anders Carlsson45372a62009-07-23 03:17:50 +0000142 LLVMFields.clear();
143 LLVMBitFields.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Anders Carlsson45372a62009-07-23 03:17:50 +0000145 LayoutFields(D);
146}
147
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000148static CGBitFieldInfo ComputeBitFieldInfo(CodeGenTypes &Types,
149 const FieldDecl *FD,
150 uint64_t FieldOffset,
151 uint64_t FieldSize) {
152 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarab970f92010-04-13 20:58:55 +0000153 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
154 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000155
Daniel Dunbar7fb61952010-04-15 05:09:28 +0000156 unsigned StartBit = FieldOffset % TypeSizeInBits;
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000157 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbar7fb61952010-04-15 05:09:28 +0000158 CGBitFieldInfo BFI(FieldSize, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000159
Daniel Dunbarab970f92010-04-13 20:58:55 +0000160 // The current policy is to always access the bit-field using the source type
161 // of the bit-field. With the C bit-field rules, this implies that we always
162 // use either one or two accesses, and two accesses can only occur with a
163 // packed structure when the bit-field straddles an alignment boundary.
Daniel Dunbar7fb61952010-04-15 05:09:28 +0000164 unsigned LowBits = std::min(FieldSize, TypeSizeInBits - StartBit);
Daniel Dunbarab970f92010-04-13 20:58:55 +0000165 bool NeedsHighAccess = LowBits != FieldSize;
166 BFI.setNumComponents(1 + NeedsHighAccess);
167
168 // FIXME: This access policy is probably wrong on big-endian systems.
169 CGBitFieldInfo::AccessInfo &LowAccess = BFI.getComponent(0);
170 LowAccess.FieldIndex = 0;
171 LowAccess.FieldByteOffset =
172 TypeSizeInBytes * ((FieldOffset / 8) / TypeSizeInBytes);
Daniel Dunbar7fb61952010-04-15 05:09:28 +0000173 LowAccess.FieldBitStart = StartBit;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000174 LowAccess.AccessWidth = TypeSizeInBits;
175 // FIXME: This might be wrong!
176 LowAccess.AccessAlignment = 0;
177 LowAccess.TargetBitOffset = 0;
178 LowAccess.TargetBitWidth = LowBits;
179
180 if (NeedsHighAccess) {
181 CGBitFieldInfo::AccessInfo &HighAccess = BFI.getComponent(1);
182 HighAccess.FieldIndex = 0;
183 HighAccess.FieldByteOffset = LowAccess.FieldByteOffset + TypeSizeInBytes;
184 HighAccess.FieldBitStart = 0;
185 HighAccess.AccessWidth = TypeSizeInBits;
186 // FIXME: This might be wrong!
187 HighAccess.AccessAlignment = 0;
188 HighAccess.TargetBitOffset = LowBits;
189 HighAccess.TargetBitWidth = FieldSize - LowBits;
190 }
191
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000192 return BFI;
193}
194
Anders Carlsson45372a62009-07-23 03:17:50 +0000195void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
196 uint64_t FieldOffset) {
Mike Stump1eb44332009-09-09 15:08:12 +0000197 uint64_t FieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000198 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Anders Carlsson45372a62009-07-23 03:17:50 +0000200 if (FieldSize == 0)
201 return;
202
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000203 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson45372a62009-07-23 03:17:50 +0000204 unsigned NumBytesToAppend;
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Anders Carlsson45372a62009-07-23 03:17:50 +0000206 if (FieldOffset < NextFieldOffset) {
207 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000208 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Anders Carlsson45372a62009-07-23 03:17:50 +0000210 // The bitfield begins in the previous bit-field.
Mike Stump1eb44332009-09-09 15:08:12 +0000211 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000212 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
213 } else {
214 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
215
216 // Append padding if necessary.
217 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
219 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000220 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Anders Carlsson45372a62009-07-23 03:17:50 +0000222 assert(NumBytesToAppend && "No bytes to append!");
223 }
224
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000225 // Add the bit field info.
226 LLVMBitFields.push_back(
227 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Anders Carlsson45372a62009-07-23 03:17:50 +0000229 AppendBytes(NumBytesToAppend);
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Mike Stump1eb44332009-09-09 15:08:12 +0000231 BitsAvailableInLastField =
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000232 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000233}
234
235bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
236 uint64_t FieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000237 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000238 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000239 return false;
240
241 if (D->isBitField()) {
242 // We must use packed structs for unnamed bit fields since they
243 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000244 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000245 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Anders Carlsson45372a62009-07-23 03:17:50 +0000247 LayoutBitField(D, FieldOffset);
248 return true;
249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Anders Carlsson2c12d032010-02-02 05:17:25 +0000251 // Check if we have a pointer to data member in this field.
252 CheckForPointerToDataMember(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000253
Anders Carlsson45372a62009-07-23 03:17:50 +0000254 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000255 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000257 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
258 unsigned TypeAlignment = getTypeAlignment(Ty);
259
Anders Carlssona5dd7222009-08-08 19:38:24 +0000260 // If the type alignment is larger then the struct alignment, we must use
261 // a packed struct.
262 if (TypeAlignment > Alignment) {
263 assert(!Packed && "Alignment is wrong even with packed struct!");
264 return false;
265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Anders Carlssona5dd7222009-08-08 19:38:24 +0000267 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
268 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
269 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
270 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
271 return false;
272 }
273 }
274
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000275 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000276 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000277 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
278
279 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
280 assert(!Packed && "Could not place field even with packed struct!");
281 return false;
282 }
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000284 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
285 // Even with alignment, the field offset is not at the right place,
286 // insert padding.
287 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000289 AppendBytes(PaddingInBytes);
290 }
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Anders Carlsson45372a62009-07-23 03:17:50 +0000292 // Now append the field.
293 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000294 AppendField(FieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Anders Carlsson45372a62009-07-23 03:17:50 +0000296 return true;
297}
298
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000299void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
300 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000302 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000304 const llvm::Type *Ty = 0;
305 uint64_t Size = 0;
306 unsigned Align = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000308 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000309
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000310 unsigned FieldNo = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000311 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000312 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump1eb44332009-09-09 15:08:12 +0000313 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000314 "Union field offset did not start at the beginning of record!");
Daniel Dunbar7f289642010-04-08 02:59:45 +0000315 const llvm::Type *FieldTy =
316 Types.ConvertTypeForMemRecursive(Field->getType());
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000317
318 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000319 uint64_t FieldSize =
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000320 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000322 // Ignore zero sized bit fields.
323 if (FieldSize == 0)
324 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Anders Carlsson94ae95f2009-07-23 22:52:34 +0000326 // Add the bit field info.
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000327 LLVMBitFields.push_back(
328 LLVMBitFieldInfo(*Field, ComputeBitFieldInfo(Types, *Field,
329 0, FieldSize)));
Daniel Dunbar490fc902010-03-31 00:55:13 +0000330 } else {
331 LLVMFields.push_back(LLVMFieldInfo(*Field, 0));
332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000334 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000335
Anders Carlsson177d4d82009-07-23 21:52:03 +0000336 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
337 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000339 if (FieldAlign < Align)
340 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000342 if (FieldAlign > Align || FieldSize > Size) {
343 Ty = FieldTy;
344 Align = FieldAlign;
345 Size = FieldSize;
346 }
347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000349 // Now add our field.
Anders Carlsson36620002009-09-03 22:56:02 +0000350 if (Ty) {
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000351 AppendField(0, Ty);
Anders Carlsson36620002009-09-03 22:56:02 +0000352
353 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
354 // We need a packed struct.
355 Packed = true;
356 Align = 1;
357 }
358 }
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000359 if (!Align) {
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000360 assert(HasOnlyZeroSizedBitFields &&
361 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000362 Align = 1;
363 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000364
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000365 // Append tail padding.
366 if (Layout.getSize() / 8 > Size)
367 AppendPadding(Layout.getSize() / 8, Align);
368}
369
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000370void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
371 const ASTRecordLayout &Layout) {
372 // Check if we need to add a vtable pointer.
373 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
Daniel Dunbar270e2032010-03-31 00:11:27 +0000374 const llvm::Type *Int8PtrTy =
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000375 llvm::Type::getInt8PtrTy(Types.getLLVMContext());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000376
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000377 assert(NextFieldOffsetInBytes == 0 &&
378 "Vtable pointer must come first!");
379 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
380 }
381}
382
Anders Carlsson45372a62009-07-23 03:17:50 +0000383bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
384 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlssona5dd7222009-08-08 19:38:24 +0000385 assert(Alignment && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000387 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000389 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
390 LayoutBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000391
Anders Carlsson45372a62009-07-23 03:17:50 +0000392 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000393
Mike Stump1eb44332009-09-09 15:08:12 +0000394 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000395 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
396 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000397 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000398 "Could not layout fields even with a packed LLVM struct!");
399 return false;
400 }
401 }
402
403 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000404 AppendTailPadding(Layout.getSize());
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Anders Carlsson45372a62009-07-23 03:17:50 +0000406 return true;
407}
408
Anders Carlssonc1efe362009-07-27 14:55:54 +0000409void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
410 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Anders Carlssonc1efe362009-07-27 14:55:54 +0000412 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000413 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Daniel Dunbar270e2032010-03-31 00:11:27 +0000415 uint64_t AlignedNextFieldOffset =
Anders Carlssonc2456822009-12-08 01:24:23 +0000416 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
417
418 if (AlignedNextFieldOffset == RecordSizeInBytes) {
419 // We don't need any padding.
420 return;
421 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000422
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000423 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000424 AppendBytes(NumPadBytes);
425}
426
Mike Stump1eb44332009-09-09 15:08:12 +0000427void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000428 const llvm::Type *FieldTy) {
429 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
430 getTypeAlignment(FieldTy));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000431
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000432 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000433
Anders Carlsson45372a62009-07-23 03:17:50 +0000434 FieldTypes.push_back(FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000435
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000436 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson45372a62009-07-23 03:17:50 +0000437 BitsAvailableInLastField = 0;
438}
439
Mike Stump1eb44332009-09-09 15:08:12 +0000440void
Anders Carlsson45372a62009-07-23 03:17:50 +0000441CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
442 const llvm::Type *FieldTy) {
443 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
444}
445
Mike Stump1eb44332009-09-09 15:08:12 +0000446void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000447 unsigned FieldAlignment) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000448 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
449 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Anders Carlsson45372a62009-07-23 03:17:50 +0000451 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000452 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson45372a62009-07-23 03:17:50 +0000453 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
454
455 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
456 // Even with alignment, the field offset is not at the right place,
457 // insert padding.
458 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
459
460 AppendBytes(PaddingInBytes);
461 }
462}
463
464void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
465 if (NumBytes == 0)
466 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Owen Anderson0032b272009-08-13 21:57:51 +0000468 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonc1efe362009-07-27 14:55:54 +0000469 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000470 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Anders Carlsson45372a62009-07-23 03:17:50 +0000472 // Append the padding field
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000473 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000474}
475
476unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000477 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000478 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Anders Carlsson45372a62009-07-23 03:17:50 +0000480 return Types.getTargetData().getABITypeAlignment(Ty);
481}
482
Anders Carlsson2c12d032010-02-02 05:17:25 +0000483void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000484 // This record already contains a member pointer.
Anders Carlsson2c12d032010-02-02 05:17:25 +0000485 if (ContainsPointerToDataMember)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000486 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000488 // Can only have member pointers if we're compiling C++.
489 if (!Types.getContext().getLangOptions().CPlusPlus)
490 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Anders Carlsson2c12d032010-02-02 05:17:25 +0000492 T = Types.getContext().getBaseElementType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Anders Carlsson2c12d032010-02-02 05:17:25 +0000494 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
495 if (!MPT->getPointeeType()->isFunctionType()) {
496 // We have a pointer to data member.
497 ContainsPointerToDataMember = true;
498 }
499 } else if (const RecordType *RT = T->getAs<RecordType>()) {
500 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000501
Anders Carlsson2c12d032010-02-02 05:17:25 +0000502 // FIXME: It would be better if there was a way to explicitly compute the
503 // record layout instead of converting to a type.
504 Types.ConvertTagDeclType(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000505
Anders Carlsson2c12d032010-02-02 05:17:25 +0000506 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000507
Anders Carlsson2c12d032010-02-02 05:17:25 +0000508 if (Layout.containsPointerToDataMember())
509 ContainsPointerToDataMember = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000510 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000511}
512
Daniel Dunbar270e2032010-03-31 00:11:27 +0000513CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
514 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Anders Carlsson45372a62009-07-23 03:17:50 +0000516 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000517
Daniel Dunbar270e2032010-03-31 00:11:27 +0000518 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000519 Builder.FieldTypes,
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000520 Builder.Packed);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000521 assert(getContext().getASTRecordLayout(D).getSize() / 8 ==
522 getTargetData().getTypeAllocSize(Ty) &&
Anders Carlssondf31e092009-08-08 18:01:57 +0000523 "Type size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000525 CGRecordLayout *RL =
526 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
527
Anders Carlsson45372a62009-07-23 03:17:50 +0000528 // Add all the field numbers.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +0000529 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i)
530 RL->FieldInfo.insert(Builder.LLVMFields[i]);
Anders Carlsson45372a62009-07-23 03:17:50 +0000531
532 // Add bitfield info.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +0000533 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i)
534 RL->BitFields.insert(Builder.LLVMBitFields[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Daniel Dunbarab970f92010-04-13 20:58:55 +0000536 if (getContext().getLangOptions().DumpRecordLayouts) {
537 llvm::errs() << "\n*** Dumping Record Layout\n";
538 llvm::errs() << "Record: ";
539 D->dump();
540 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000541 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000542 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000543
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000544 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +0000545}
Daniel Dunbar93c62962010-04-12 18:14:18 +0000546
547void CGRecordLayout::print(llvm::raw_ostream &OS) const {
548 OS << "<CGRecordLayout\n";
549 OS << " LLVMType:" << *LLVMType << "\n";
550 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n";
551 OS << " BitFields:[\n";
552 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
553 it = BitFields.begin(), ie = BitFields.end();
554 it != ie; ++it) {
Daniel Dunbarab970f92010-04-13 20:58:55 +0000555 OS.indent(4);
Daniel Dunbar93c62962010-04-12 18:14:18 +0000556 it->second.print(OS);
557 OS << "\n";
558 }
559 OS << "]>\n";
560}
561
562void CGRecordLayout::dump() const {
563 print(llvm::errs());
564}
565
566void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
567 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000568 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000569 OS << " IsSigned:" << IsSigned << "\n";
570
571 OS.indent(4 + strlen("<CGBitFieldInfo"));
572 OS << " NumComponents:" << getNumComponents();
573 OS << " Components: [";
574 if (getNumComponents()) {
575 OS << "\n";
576 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
577 const AccessInfo &AI = getComponent(i);
578 OS.indent(8);
579 OS << "<AccessInfo"
580 << " FieldIndex:" << AI.FieldIndex
581 << " FieldByteOffset:" << AI.FieldByteOffset
582 << " FieldBitStart:" << AI.FieldBitStart
583 << " AccessWidth:" << AI.AccessWidth << "\n";
584 OS.indent(8 + strlen("<AccessInfo"));
585 OS << " AccessAlignment:" << AI.AccessAlignment
586 << " TargetBitOffset:" << AI.TargetBitOffset
587 << " TargetBitWidth:" << AI.TargetBitWidth
588 << ">\n";
589 }
590 OS.indent(4);
591 }
592 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000593}
594
595void CGBitFieldInfo::dump() const {
596 print(llvm::errs());
597}